Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';pygame渲染我所有的精灵?_Python_Python 3.x_Pygame - Fatal编程技术网

Python 为什么不是';pygame渲染我所有的精灵?

Python 为什么不是';pygame渲染我所有的精灵?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我试着按照教程学习pygame。我正试图繁殖敌人的物体,但我认为我犯了一个错误,因为没有显示任何东西 class Player(pygame.sprite.Sprite): def __init__(self): super(Player, self).__init__() self.surf = pygame.Surface((75,25)) self.surf.fill((0, 0,0))

我试着按照教程学习pygame。我正试图繁殖敌人的物体,但我认为我犯了一个错误,因为没有显示任何东西

class Player(pygame.sprite.Sprite):
        def __init__(self):
            super(Player, self).__init__()
            self.surf = pygame.Surface((75,25))
            self.surf.fill((0, 0,0))
            self.rect = self.surf.get_rect()

           #Moves sprite based on keypresses(removed for clarity)
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.Surface((20,10))
        self.surf.fill((0,0,0))
        self.rect = self.surf.get_rect(
            center = (
            random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
            random.randint(0, SCREEN_HEIGHT),
            )
        )
        self.speed = random.randint(5, 20)
        # moves sprite based on speed, remove when passes left edge
        def update(self):
            self.rect.move_ip(-self.speed, 0)
            if self.rect.right < 0:
                self.kill()
                #How sad for them


pygame.init()
#create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

#custom events for adding a new Enemy
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)
# Instantiate player
player = Player()
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
#keep the game running!
running = True


#loop time!
while running:
    #look at all the events
    for event in pygame.event.get():
        #did the user hit a key?
        if event.type == KEYDOWN:
            #Was it escape?  uh oh we gotta stop
            if event.key == K_ESCAPE:
                running = False

        elif event.type ==QUIT:
            running = False

        elif event.type == ADDENEMY:
            #Create the new enemy
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)
    #gets the keys that were just pressed
    pressed_keys = pygame.key.get_pressed()
    #updates location based on keys
    player.update(pressed_keys)
    #updates enemy position
    enemies.update()
    #fill screen with white
    screen.fill((255, 255, 255))
    #Draw the sprites
    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)
    # Create a surface and pass in a tuple containing legth and width
    surf = pygame.Surface((50, 50))

    
    pygame.display.flip()
职业玩家(pygame.sprite.sprite):
定义初始化(自):
超级(玩家,自我)。\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()
self.surf=pygame.Surface((75,25))
自表面填充((0,0,0))
self.rect=self.surf.get_rect()
#基于按键移动精灵(为清晰起见已删除)
职业敌人(pygame.sprite.sprite):
定义初始化(自):
超级(敌人,自己)。\uuuuu init\uuuuuuu()
self.surf=pygame.Surface((20,10))
自表面填充((0,0,0))
self.rect=self.surf.get_rect(
中心=(
random.randint(屏幕宽度+20,屏幕宽度+100),
random.randint(0,屏幕高度),
)
)
self.speed=random.randint(5,20)
#根据速度移动精灵,通过左边缘时移除
def更新(自我):
自校正移动ip(-自校正速度,0)
如果self.rect.right<0:
self.kill()
#他们真伤心
pygame.init()
#创建屏幕
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
#用于添加新敌人的自定义事件
ADDENEMY=pygame.USEREVENT+1
pygame.time.set_定时器(ADDENEMY,250)
#实例化播放器
player=player()
敌人=pygame.sprite.Group()
all_sprites=pygame.sprite.Group()
所有精灵。添加(玩家)
#继续游戏!
运行=真
#循环时间!
运行时:
#看看所有的事件
对于pygame.event.get()中的事件:
#用户是否按了键?
如果event.type==KEYDOWN:
#是逃跑吗?哦,我们得停下来
如果event.key==K_转义:
运行=错误
elif event.type==退出:
运行=错误
elif event.type==ADDENEMY:
#制造新的敌人
新敌人
敌人。添加(新敌人)
所有精灵。添加(新敌人)
#获取刚刚按下的键
按下按键=pygame.key.get_pressed()
#基于密钥更新位置
播放机更新(按_键)
#更新敌人的位置
更新(
#用白色填充屏幕
屏幕填充((255、255、255))
#画精灵
对于所有_精灵中的实体:
screen.blit(entity.surf、entity.rect)
#创建曲面并传入包含长度和宽度的元组
surf=pygame.Surface((50,50))
pygame.display.flip()
据我所知,我必须创建一个敌人类,然后添加一个事件来添加敌人(ADDENEMY)。当该事件被调用时(由于计时器的作用,每250毫秒一次),它应该会显示一个向左移动的敌人,直到它到达显示器的末端。

我有一组敌人精灵和一组所有精灵,我使用screen.blit和for循环将所有精灵中的每个实体渲染到屏幕上。我错过了什么?当我运行这个(以及我编辑掉的部分)时,我看到玩家的精灵很好,它移动了,但我没有看到任何敌人。

你的精灵不在窗口中。由于未正确指定范围,所有精灵都在窗口之外:

random.randint(屏幕宽度+20,屏幕宽度+100)

random.randint(20,屏幕宽度+100),
此外,还有一个问题。方法
更新
需要是类
敌人
的范围:

类敌人(pygame.sprite.sprite):
定义初始化(自):
# [...]
#压痕

#非常感谢,事实证明并不是这样,我确实想让它们稍微离开屏幕,但当我用(20,屏幕宽度+100)在屏幕上渲染它们时,我注意到它们没有移动。真正的问题是,我在__init__(self)函数中定义了更新(self)函数