为了让您的程序不那么复杂,您需要为气球和螺丝设置一些类。假设您只从左向右移动,第一个类将是为您的播放器准备的,看起来像这样:
class Player(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = pygame.image.get_rect()
self.rect.top, self.rect.left = location
这段代码将使您的气球成为一个精灵,准备好检测碰撞。你的螺丝类看起来很相似,只是在类的__init__ 部分中多了一个move 和location 函数和speed:
class Screws(pygame.sprite.Sprite):
def __init__(self, image_file, left, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = pygame.image.get_rect()
self.rect.top = 50
self.rect.left = left
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
这个类使螺丝精灵,也准备好检测。这完成了类部分。现在对这些精灵等进行分组:
balloon = Player('/users/Gaming/Desktop/rsz_baloon.png', [a, b])
screw = Screws('/users/Gaming/Desktop/rsz_screw_png3029.png', random.randint(0, <ScreenSize>), c)
ScrewGroup = pygame.sprite.Group()
再一次,变量是可变的,但c 越高,螺丝掉的越快。 a 和 b 将决定气球的位置,random.randint() 将决定螺丝的位置。 self.rect.top 是一种通过将其用作rect 的“顶部”侧的位置来查找位置的方法。在这种情况下,它保持不变。 self.rect.left 相同,但它是 rect 的“左侧”侧的位置。现在转到气球的移动部分,为了避免过度按下 UP 和 DOWN 键(以及疲劳),在后面添加这行代码:
delay = 100
interval = 50
pygame.key.set_repeat(delay, interval)
on = True
screwy = 0
delay 是每次激活 KEYDOWN 之间的毫秒数,interval 是等待开始重复 KEYDOWN 的毫秒数。这将帮助用户,让他只需按住 LEFT 或 RIGHT 箭头键,气球将继续朝所需方向前进。 on 和 screwy 变量将在下一节中讨论。接下来,while循环就差不多了:
while True:
screen.blit(balloon.image, balloon.rect)
while int(screwy - 1) > -1:
screen.blit(screw.image, screw.rect)
pygame.display.flip()
second while 循环会产生尽可能多的螺丝,只要 screwy - 1 的 值 小于 -1 (负 1)。这也会翻转屏幕以避免屏幕上留下任何“轨迹”。现在到气球的移动部分:
for event in pygame.event.get():
#Remember to do this : from pygame.locals import *#
if event.type == QUIT:
on = False
#Remember to import sys!#
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key = K_LEFT:
balloon.rect.left -= 30
elif event.key == K_RIGHT:
#This can only work if (width_of_the_picture - a) is equal to 30#
balloon.rect.left += int(width_of_the_picture - a)
这将允许您移动气球(您可以按住键保持气球移动,就像在真正的电子游戏中一样)。接下来是螺丝的继续产卵:
if screwy < 10:
ScrewGroup.append(Screws('/users/Gaming/Desktop/rsz_screw_png3029.png', random.randint(0, b), [0, 15]))
screwy += 1
这将在随机位置生成螺丝(相同的self.rect.top 值使其看起来更逼真)。在这种情况下,b 将等于屏幕的宽度。最后是精灵检测:
if pygame.sprite.spritecollide(balloon, ScrewGroup, True):
on = False
sys.exit()
pass
这会检测气球是否与螺钉发生碰撞。如果这是真的,那么你可以决定。您可以退出 while 循环并退出程序,这是一种方法。但是,如果您打算做类似print 'Game Over! 的事情,请在执行on = False/sys.exit() 之前添加该行,这将立即退出循环/程序。您将需要重新 blit 图像并让屏幕退出平滑 (pygame.quit):
screen.fill([255, 255, 255])
screen.blit(balloon.image, balloon.rect)
while int(screwy - 1) > -1:
screen.blit(screw)
pygame.display.flip()
pygame.quit()
记得将pygame.quit() 放在while 循环之外,否则屏幕将立即消失。当然,做一些代码来防止气球退出屏幕。将 KEYDOWN 部分更改为此应该可以:
elif event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
if int(balloon.rect.left) - 30 < 0:
pass
elif int(balloon.rect.left) - 30 >= 0:
balloon.rect.left -= 30
elif event.key == K_RIGHT:
if int(balloon.rect.left) + (<Width_OF_Balloon> - a) > <Width_OF_Screen>:
pass
elif int(balloon.rect.left) + (<Width_OF_Balloon> - a) <= <Width_OF_Screen>:
#This can only work if (<Width_OF_Balloon> - a) is equal to 30#
baloon.rect.left + (<Width_OF_Balloon> - a)
如果向左/向右移动一次使气球部分离开屏幕,则程序将不允许气球向左/向右移动,而不会对pass 执行任何操作。这应该会显着改善您的程序并回答您的问题。希望对你有帮助!