我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我...
我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surface,但这给我带来了各种各样的问题(def,可能是我搞砸了基本的继承/类的东西)。例如,为什么这不起作用(pygame、screen 等都可以正常工作并用于代码的其他部分,我只是想将我已经在使用的函数移动到一个类中):
class Zombie(pygame.Surface):
x_pos = y_pos = 0
def __init__(self, x, y):
#create zombie
self = pygame.image.load('zombie_1.png')
self = pygame.transform.scale(self,(50, 50))
x_pos = x
y_pos = y
zombie = Zombie(screen.get_width()/3, screen.get_height()/3)
screen.blit(zombie, (zombie.x_pos, zombie.y_pos))
上面产生了一个错误:“pygame.error: display Surface quit” 编辑:显然这是在调用 pygame.display.quit() 之后调用 Surface 的结果。任何有 pygame 经验的人都想尝试一下吗?
完整代码:
#Initialize
import pygame, sys
pygame.init()
#classes
class Zombie(pygame.Surface):
x_pos = y_pos = 0
def __init__(self, x, y):
#create zombie
self = pygame.image.load('zombie_1.png')
self = pygame.transform.scale(self,(50, 50))
x_pos = x
y_pos = y
def is_hit(mouse_pos):
#grab variables
(mouseX, mouseY) = mouse_pos
print "\nboxW_x, y = " + str(self.x) + ", " + str(self.y) + "\nmouseX, Y = " + str(mouseX) + ", " + str(mouseY)
headshot_x = self.x + (.5 * zombie.get_width())
headshot_y = self.y + (.25 * zombie.get_height())
margin_of_error_x = (zombie.get_width()/float(1000)) * zombie.get_width()
margin_of_error_y = (zombie.get_height()/float(1000)) * zombie.get_height()
print "Headshot_x: " + str(headshot_x) + ", " + str(headshot_y)
print "diff in headshot and actual shot: " + str(mouseX - headshot_x) + ", " + str(mouseY - headshot_y)
print "margin of error x = " + str(margin_of_error_x) + " y = " + str(margin_of_error_y)
print "zombie size: " + str(zombie.get_size())
valid_x = valid_y = False
if abs(mouseX-headshot_x) < margin_of_error_x:
valid_x = True
print "valid x"
if abs(mouseY-headshot_y) < margin_of_error_y:
valid_y = True
print "valid y"
return (valid_x and valid_y)
#list of bad guys
zombie_list = []
#functions (which should later be moved into classes)
def is_hit():
#grab variables
(mouseX, mouseY) = pygame.mouse.get_pos()
print "\nboxW_x, y = " + str(boxW_x) + ", " + str(boxW_y) + "\nmouseX, Y = " + str(mouseX) + ", " + str(mouseY)
headshot_x = boxW_x + (.5 * boxW.get_width())
headshot_y = boxW_y + (.25 * boxW.get_height())
margin_of_error_x = (boxW.get_width()/float(1000)) * boxW.get_width()
margin_of_error_y = (boxW.get_height()/float(1000)) * boxW.get_height()
print "Headshot_x: " + str(headshot_x) + ", " + str(headshot_y)
print "diff in headshot and actual shot: " + str(mouseX - headshot_x) + ", " + str(mouseY - headshot_y)
print "margin of error x = " + str(margin_of_error_x) + " y = " + str(margin_of_error_y)
print "zombie size: " + str(boxW.get_size())
valid_x = valid_y = False
if abs(mouseX-headshot_x) < margin_of_error_x:
valid_x = True
print "valid x"
if abs(mouseY-headshot_y) < margin_of_error_y:
valid_y = True
print "valid y"
return (valid_x and valid_y)
pygame.mouse.set_visible(True)
pygame.mouse.set_cursor(*pygame.cursors.diamond)
#Display
screen = pygame.display.set_mode((640, 640))
pygame.display.set_caption("Zombie Massacre")
#Entities
#background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
#make a zombie
boxW = pygame.image.load('zombie_1.png')
boxW = pygame.transform.scale(boxW,(50, 50))
#set up some box variables
boxW_x = screen.get_width()/3
boxW_y = screen.get_height()/3
#testing zombie class
zombie = Zombie(screen.get_width()/3, screen.get_height()/3)
#Action
#Assign
clock = pygame.time.Clock()
keepGoing = True
#Loop
count = 0;
rotation_vect = 1.01
while keepGoing:
#setup rotation_vect for this pass
if (count % 3) == 0:
rotation_vect = 0 - rotation_vect
#Time
clock.tick(30)
#Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type is pygame.MOUSEBUTTONDOWN:
#loop through zombie list, using each one's "is_hit()" function
keepGoing = not(is_hit())
#Refresh screen
screen.blit(background, (0,0))
boxW = pygame.transform.rotozoom(pygame.image.load('zombie_1.png'), rotation_vect, 1.01)
boxW = pygame.transform.scale(boxW,(count+50, count+100))
#for zombie in zombies
screen.blit(boxW,(boxW_x+(boxW_x * .1), boxW_y+(boxW_y * .1)))
# error is result of following line
screen.blit(zombie, (zombie.x_pos, zombie.y_pos))
pygame.display.flip()
#increment count
count += 1
最佳答案
您没有调用继承的构造函数:
class Zombie(pygame.Surface):
def __init__(self, x, y):
pygame.Surface.__init__(self, size=(w,h))
并且您正在分配给self
。那是行不通的。
关于python - pygame:自定义类继承自 pygame.Surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4002019/