我第一次玩pygame(对于一般的python来说有点蠢),想知道是否有人可以帮我这个......
我正在做一个小小的游戏,并希望能够为坏人创建一个课程。我的想法是这个类应该继承自pygame.Surface,但是这给了我各种各样的问题(def,可能是我搞砸了基本的继承/类的东西)。例如,为什么这不起作用(pygame,屏幕等都可以正常工作并在代码的其他部分使用,我只是试图将已经工作的函数移动到类中):
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
答案 0 :(得分:3)
您没有调用继承的构造函数:
class Zombie(pygame.Surface):
def __init__(self, x, y):
pygame.Surface.__init__(self, size=(w,h))
你正在分配给self
。那不行。
答案 1 :(得分:3)
我看到这个问题是在一个月前被问到的,但也许现在还不晚......
我从您的代码中看到的是,您基本上是在尝试重新创建Sprite类。实际上,你将图像和位置绑定在一起。
我不明白游戏玩法是什么(除了头部拍摄部分:D),但这里有一个关于如何显示僵尸并检测它们何时被射击的例子:
import pygame
TITLE = "Zombie Massacre"
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
class ZombieSprite(pygame.sprite.Sprite):
def __init__(self, image):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = pygame.Rect((0, 0), image.get_size())
self.defineHeadPos()
def defineHeadPos(self):
# To call each time the size of the rect changes.
head_x_center = self.rect.width / 2
head_y_center = self.rect.height / 4
head_width = self.rect.height / 4
head_height = self.rect.height / 4
head_x_min = head_x_center - head_width / 2
head_y_min = head_y_center - head_height / 2
self.head_rect = pygame.Rect((head_x_min, head_y_min),
(head_width, head_height))
def update(self):
# Here we could move the zombie.
pass
def shoot(self, pos):
x, y = pos
x -= self.rect.left
y -= self.rect.top
if self.head_rect.collidepoint(x, y):
print "Head shot !"
else:
print "Shot."
def ActionShoot(zombies, pos):
print "Shot at %s." % (pos,)
sprites = zombies.get_sprites_at(pos)
if not sprites:
print "Missed."
return
sprite = sprites[-1]
sprite.shoot(pos)
def DrawScene(screen, background, zombies):
screen.blit(background, (0, 0))
zombies.draw(screen)
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(TITLE)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((32, 32, 32))
image_zombie = pygame.image.load('zombie.png')
image_zombie.convert()
zombies = pygame.sprite.LayeredUpdates()
zombie = ZombieSprite(image_zombie)
zombie.rect.move_ip(0, 0)
zombies.add(zombie)
zombie = ZombieSprite(image_zombie)
zombie.rect.move_ip(400, 100)
zombies.add(zombie)
zombie = ZombieSprite(image_zombie)
zombie.rect.move_ip(300, 250)
zombies.add(zombie)
clock = pygame.time.Clock()
running = True
while running:
clock.tick(30)
zombies.update()
DrawScene(screen, background, zombies)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
ActionShoot(zombies, pygame.mouse.get_pos())
print "Quitting..."
pygame.quit()
这根本不是一个完整的游戏,但我把它留给你。这里的关键是使用精灵和组在不同的地方显示一堆僵尸。您可以将僵尸的“智能”放在他们的更新方法中(移动它们,如果它们靠近屏幕则缩放它们)。注意:我只为所有僵尸使用了一个图像对象。如果你开始缩放,你希望每个僵尸拥有自己的图像对象,这意味着为每个僵尸加载zombie.png。否则当你缩放它们时它们都会变焦。