我的 pygame 播放器精灵没有移动和/或更新

我正在尝试构建一个简单的“飞扬的小鸟”之类的游戏。我正在尝试将所有代码分类为类和方法。我该如何解决这个问题?是因为过早调用某些方法导致代码无法运行,还是因为缺少某些东西?如果有人愿意向我解释,我真的很喜欢。


sprites.py:


import pygame



class Player(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface((50, 50))

        self.image.fill((255, 255, 0))

        self.rect = self.image.get_rect()

        self.rect.x = 0

        self.rect.y = (700 / 2)

        self.movex = 0

        self.movey = 0


    def control(self, x, y):

        self.movex += x

        self.movey += y


    def update(self):

        self.rect.x += self.movex

        self.rect.y += self.movey


    def animate(self):

        pass



class Obstacle(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):

        pygame.sprite.Sprite.__init__(self)

        self.x = x

        self.y = y

        self.width = width

        self.height = height

main.py:


from sprites import *

import pygame


WIDTH = 700

HEIGHT = 700



class Game:


    def __init__(self):

        pygame.init()

        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))

        self.clock = pygame.time.Clock()

        self.score = 0

        self.running = True


    def new(self):

        pass


    def events(self):

        self.game_on = True

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                self.game_on = False

                self.running = False

            if event.type == pygame.KEYDOWN:

                if event.type == pygame.K_UP:

                    self.croc.control(0, -20)


    def update(self):

        self.croc = Player()

        self.all_sprites = pygame.sprite.Group()

        self.all_sprites.add(self.croc)

        self.all_sprites.update()


    def draw(self):

        self.screen.fill((0, 0, 0))

        self.all_sprites.draw(self.screen)

        pygame.display.flip()



game = Game()


while game.running:

    game.clock.tick(60)

    game.new()

    game.events()

    game.update()

    game.draw()

谢谢


慕婉清6462132
浏览 102回答 1
1回答

qq_笑_17

有2个错误。Player在每一帧中重新创建对象。Game在 的构造函数中而不是在方法中创建播放器update:class Game:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; # [...]&nbsp; &nbsp; &nbsp; &nbsp; self.croc = Player()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# <--- ADD&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites = pygame.sprite.Group() # <--- ADD&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites.add(self.croc)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # <--- ADD&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; # self.croc = Player()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# <--- DELETE&nbsp; &nbsp; &nbsp; &nbsp; # self.all_sprites = pygame.sprite.Group() # <--- DELETE&nbsp; &nbsp; &nbsp; &nbsp; # self.all_sprites.add(self.croc)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # <--- DELETE&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites.update()事件循环中有一个类型。.key您必须从属性而不是属性中获取密钥.type:if event.type == pygame.K_UP:if event.key == pygame.K_UP:完整代码:import pygameclass Player(pygame.sprite.Sprite):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pygame.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.image = pygame.Surface((50, 50))&nbsp; &nbsp; &nbsp; &nbsp; self.image.fill((255, 255, 0))&nbsp; &nbsp; &nbsp; &nbsp; self.rect = self.image.get_rect()&nbsp; &nbsp; &nbsp; &nbsp; self.rect.x = 0&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y = (700 / 2)&nbsp; &nbsp; &nbsp; &nbsp; self.movex = 0&nbsp; &nbsp; &nbsp; &nbsp; self.movey = 0&nbsp; &nbsp; def control(self, x, y):&nbsp; &nbsp; &nbsp; &nbsp; self.movex += x&nbsp; &nbsp; &nbsp; &nbsp; self.movey += y&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; self.rect.x += self.movex&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y += self.movey&nbsp; &nbsp; def animate(self):&nbsp; &nbsp; &nbsp; &nbsp; passclass Obstacle(pygame.sprite.Sprite):&nbsp; &nbsp; def __init__(self, x, y, width, height):&nbsp; &nbsp; &nbsp; &nbsp; pygame.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.x = x&nbsp; &nbsp; &nbsp; &nbsp; self.y = y&nbsp; &nbsp; &nbsp; &nbsp; self.width = width&nbsp; &nbsp; &nbsp; &nbsp; self.height = heightWIDTH = 700HEIGHT = 700class Game:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pygame.init()&nbsp; &nbsp; &nbsp; &nbsp; self.screen = pygame.display.set_mode((WIDTH, HEIGHT))&nbsp; &nbsp; &nbsp; &nbsp; self.clock = pygame.time.Clock()&nbsp; &nbsp; &nbsp; &nbsp; self.score = 0&nbsp; &nbsp; &nbsp; &nbsp; self.running = True&nbsp; &nbsp; &nbsp; &nbsp; self.croc = Player()&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites = pygame.sprite.Group()&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites.add(self.croc)&nbsp; &nbsp; def new(self):&nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; def events(self):&nbsp; &nbsp; &nbsp; &nbsp; self.game_on = True&nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.game_on = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.running = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.key == pygame.K_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.croc.control(0, -20)&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites.update()&nbsp; &nbsp; def draw(self):&nbsp; &nbsp; &nbsp; &nbsp; self.screen.fill((0, 0, 0))&nbsp; &nbsp; &nbsp; &nbsp; self.all_sprites.draw(self.screen)&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.flip()game = Game()while game.running:&nbsp; &nbsp; game.clock.tick(60)&nbsp; &nbsp; game.new()&nbsp; &nbsp; game.events()&nbsp; &nbsp; game.update()&nbsp; &nbsp; game.draw()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python