随着时间的推移,为什么下降的矩形变得越来越多?

时间:2017-05-26 05:04:04

标签: python pygame

我是pygame的新手。 我写了一个避免下降矩形的简单游戏。但是我发现随着时间的推移,下降的矩形变得越来越多,并且当它们遇到播放器时,一些矩形不能让pygame.sprite.spritecollideany产生。我的代码中是否有任何错误?

这是我的代码。

import pygame
import random
from pygame.locals import *

FPS = 25
fpsclock = pygame.time.Clock()

#define the player
class Player(pygame.sprite.Sprite):
    def __init__(self):
          super(Player, self).__init__()
          self.surf = pygame.Surface((30, 30))
          self.surf.fill((123, 123, 123))
          self.rect = self.surf.get_rect()
          self.rect.y = 550
          self.rect.x = 175

    def update(self, pressed_keys):
        """
        if pressed_keys[K_UP]:
            self.rect.move_ip(0, -5)
        if pressed_keys[K_DOWN]:
            self.rect.move_ip(0, 5)
        """
        if pressed_keys[K_LEFT]:
            self.rect.move_ip(-5, 0)
        if pressed_keys[K_RIGHT]:
            self.rect.move_ip(5, 0)

        #keep player on the screen
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > 400:
            self.rect.right = 400

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.Surface((20, 20))
        self.surf.fill((255, 255, 255))
        self.rect = self.surf.get_rect(center = (random.randint(0, 400), 0))
        self.speed = random.randint(5, 10)

    def update(self):
        self.rect.move_ip(0, self.speed)
        if self.rect.bottom < 0:
            self.kill()

class Game():
    def __init__(self):
        #initialization
        pygame.init()

        #create the screen object
        self.screen = pygame.display.set_mode((400, 600))

        #instantiate the player
        self.player = Player()
        self.score = 0

        #create enemies
        self.ADDENEMY = pygame.USEREVENT + 2
        pygame.time.set_timer(self.ADDENEMY, 250)

        self.enemies = pygame.sprite.Group()
        self.all_sprites = pygame.sprite.Group()
        self.all_sprites.add(self.player)

    def run(self):
        running = True

        #main loop
        while running:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        running = False
                elif event.type == QUIT:
                    running = False
                elif (event.type == self.ADDENEMY):
                    new_enemy = Enemy()
                    self.enemies.add(new_enemy)
                    self.score = self.score + 1
                    self.all_sprites.add(new_enemy)

            pressed_keys = pygame.key.get_pressed()
            self.player.update(pressed_keys)
            self.enemies.update()
            self.screen.fill((0,0,0))
            #draw the player to the screen
            for entity in self.all_sprites:
                self.screen.blit(entity.surf, entity.rect)
            if pygame.sprite.spritecollideany(self.player, self.enemies):
                print 'score: ' + str(self.score)
                self.score = 0

            screen_image = pygame.surfarray.array3d(pygame.display.get_surface())        
            #update the display
            pygame.display.update()
            #fpsclock.tick(FPS)
        #return reward, screen_image

game = Game()
game.run()

运行代码大约五分钟后,出现了错误。

1 个答案:

答案 0 :(得分:0)

你必须杀死敌人的精灵,当他们越过屏幕的底部而不是顶部(y = 0是屏幕的顶部,y = 600底部):

if self.rect.top > 600:  # Set it to 550 to see how they disappear.
    self.kill()

编辑:要杀死碰撞的精灵,请使用spritecollideany返回的值,该值可以是None或碰撞的精灵:

collided_sprite = pygame.sprite.spritecollideany(self.player, self.enemies)
if collided_sprite is not None:
    print('score:', self.score)
    self.score = 0
    collided_sprite.kill()

请注意,每帧只删除一个精灵。如果您想同时删除多个精灵,请更好地使用spritecollide并将True作为dokill参数传递。