精灵图像不在pygame窗口中显示

时间:2013-10-03 19:18:51

标签: python pygame

我的精灵图片没有显示在pygame窗口中。它可能非常简单,但我不明白为什么它没有显示。我对编程很新,所以我可能不明白这一点。这是我在课堂上做错了吗?这是我的代码:

import pygame, sys
from pygame.locals import *

windowwidth = 600
windowheight = 600
WALLWIDTH = 30
WALLHEIGHT = 30
PLAYERWIDTH = 20
PLAYERHEIGHT = 20

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
LIMEGREEN = (50, 205, 50)

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('Green circle png.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def changespeed(self, x, y):
        self.change_x = x
        self.change_y = y

    def update_pos(self, x, y):
        old_x_pos = self.rect.x
        new_x_pos = old_x_pos + self.change_x
        self.rect.x = new_x_pos

        old_y_pos = self.rect.y
        new_y_pos = old_y_pos + self.change_y
        self.rect.x = new_y_pos

def main():

    pygame.init()

    thescreen = pygame.display.set_mode((windowwidth, windowheight))
    pygame.display.set_caption('Maze Game')
    thescreen.fill((BLACK))

    mainclock = pygame.time.Clock()

    running = True
    while running == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            if event.type == KEYDOWN:
                if (event.key == K_UP or event.key == K_w):
                    Player.changespeed = (-3, 0)
                elif (event.key == K_DOWN or event.key == K_s):
                    Player.changespeed = (3, 0)
                elif (event.key == K_LEFT or event.key == K_a):
                    Player.changespeed = (0, -3)
                elif (event.key == K_RIGHT or event.key == K_d):
                    Player.changespeed = (0, 3)
            if event.type == KEYUP:
                if (event.key == K_UP or event.key == K_w):
                    Player.changespeed = (0, 0)
                elif (event.key == K_DOWN or event.key == K_s):
                    Player.changespeed = (0, 0)
                elif (event.key == K_LEFT or event.key == K_a):
                    Player.changespeed = (0, 0)
                elif (event.key == K_RIGHT or event.key == K_d):
                    Player.changespeed = (0, 0)


        mainclock.tick(60)
        pygame.display.update()
    pygame.quit()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

您需要在屏幕上绘制播放器。调用一些代码来绘制它(这在渲染更新组中最容易做,但你也可以简单地将其搞定)。您可能还想调用更新函数,但我不会在我的示例中包含它。您可以这样做:

players = pygame.sprite.RenderUpdates()
players.add(Player(200,200))
def mainloop():
    while running == True:
        pygame.display.update(players.draw(thescreen))
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            if event.type == KEYDOWN:
                if (event.key == K_UP or event.key == K_w):
                    Player.changespeed = (-3, 0)
                elif (event.key == K_DOWN or event.key == K_s):
                    Player.changespeed = (3, 0)
                elif (event.key == K_LEFT or event.key == K_a):
                    Player.changespeed = (0, -3)
                elif (event.key == K_RIGHT or event.key == K_d):
                    Player.changespeed = (0, 3)
            if event.type == KEYUP:
                if (event.key == K_UP or event.key == K_w):
                    Player.changespeed = (0, 0)
                elif (event.key == K_DOWN or event.key == K_s):
                    Player.changespeed = (0, 0)
                elif (event.key == K_LEFT or event.key == K_a):
                    Player.changespeed = (0, 0)
                elif (event.key == K_RIGHT or event.key == K_d):
                    Player.changespeed = (0, 0)