无法弄清楚为什么精灵没有出现在我的屏幕上

时间:2017-02-05 21:39:07

标签: python pygame

我正在玩游戏,在这个游戏中我需要一个黄色的矩形显示在游戏窗口上,但是当我运行代码时,黄色矩形并没有显示出来。我正在Player()班级中绘制矩形。任何人都可以帮助我吗?

main.py

# IMPORTS
import pygame
from config import *
from sprites import *

# GAME
class Game():
    def __init__(self):
        # INIT PYGAME
        pygame.init()
        pygame.mixer.init()

        pygame.display.set_caption(TITLE)

        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        self.clock = pygame.time.Clock()
        self.running = True

    # NEW GAME
    def new(self):
        self.allSprites = pygame.sprite.Group()
        self.player = Player()
        self.allSprites.add(self.player)
        self.run()

    # RUN GAME
    def run(self):
        self.playing = True

        while self.playing:
            self.clock.tick(FPS)

            self.events()
            self.update()
            self.draw()
            self.animate()
            self.collision()

    # DRAW
    def draw(self):
        self.screen.fill(WHITE)

        pygame.display.update()

    # ANIMATE
    def animate(self):
        pass

    # DETECT COLLISION
    def collision(self):
        pass

    # CHECK FOR EVENTS
    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                if self.playing:
                    self.playing = False

                self.running = False

    # UPDATE GAME
    def update(self):
        self.allSprites.update()

    # GAME OVER
    def gameOver(self):
        pass

    # START SCREEN
    def startScreen(self):
        pass

    # END SCREEN
    def endScreen(self):
        pass

game = Game()
game.startScreen()

while game.running:
    game.new()
    game.gameOver()

pygame.quit()
quit()

sprites.py

import pygame
from config import *

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.img = pygame.Surface((30, 40))
        self.img.fill(YELLOW)
        self.rect = self.img.get_rect()
        self.vx = 0;
        self.vy = 0;

        def update(self):
            self.vx = 0

            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                self.vx -= 5
            if keys[pygame.K_RIGHT]:
                self.vx += 5
            if keys[pygame.K_UP]:
                self.vy -= 5
            if keys[pygame.K_DOWN]:
                self.vy += 5

            self.rect.x += self.vx
            self.rect.y += self.vy

config.py

# IMPORTS
import pygame

# ENTIRE GAME VARIABLES
TITLE = "Sky Jumper"
WIDTH = 480
HEIGHT = 600
FPS = 60

# COLORS
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)

1 个答案:

答案 0 :(得分:1)

你根本不是在画画。唯一的绘图是在游戏的绘图方法中发生的,所以尝试添加:

def draw(self):
    self.screen.fill(WHITE)
    self.allSprites.draw(self.screen)
    pygame.display.update()

另外,顺便说一下,你的玩家的update()方法不会运行,因为它是在 init 方法下缩进的。

相关问题