我起草了一些代码来测试我制作的 pygame 程序上的动画。解释器干净利落地通过它,但是当我尝试运行它时,它保持冻结状态。X按钮不起作用,玩家角色被冻结在角落里,根本不会移动。
终端窗口根本没有输出任何错误,我坚持认为它没有移动。任何帮助或见解将不胜感激。
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
red = ( 255, 0, 0)
faceWhatDirection = 4
keyDown = False
class Player(pygame.sprite.Sprite):
#constructor function
def __init__(self): #what this is doing is declaring a self variable and an x and y varible
#call up the parent's constructor
pygame.sprite.Sprite.__init__(self)
#List of images for different types of movement
self.imagesLeft = []
self.imagesRight = []
self.imagesUp = []
self.imagesDown = []
#load the up images
img = pygame.image.load("man1_bk1.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesUp.append(img) #adds the image to the list
img = pygame.image.load("man1_bk2.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesUp.append(img) #adds the image to the list
#load the down images
img = pygame.image.load("man1_fr1.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesDown.append(img) #adds the image to the list
img = pygame.image.load("man1_fr2.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesDown.append(img) #adds the image to the list
#load the left images
img = pygame.image.load("man1_lf1.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesLeft.append(img) #adds the image to the list
img = pygame.image.load("man1_lf2.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesLeft.append(img) #adds the image to the list
#load the right images
img = pygame.image.load("man1_rt1.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesRight.append(img) #adds the image to the list
img = pygame.image.load("man1_rt2.gif").convert()
img.set_colorkey(white) #sets the color key. any pixel with the same color as the colorkey will be transparent
self.imagesRight.append(img) #adds the image to the list
#the best image to use by default is the one that has the player facing the screen.
self.image=self.imagesDown[0]
#This line of code, as the Programming website showed me, gets the dimensions of the image
#Rect.x and rect.y are the coordinates of the rectangle object, measured at the upper right
#hand corner
self.rect = self.image.get_rect()
def changeImage(self):
##
## FUNCTION THAT UPDATES THE IMAGES AND CYCLES THROUGH THEM
##
#if player is going left
while faceWhatDirection == 1 and keyDown == True:
self.image =self.imagesLeft[0]
self.image =self.imagesLeft[1]
#if player is going right
while faceWhatDirection == 2 and keyDown == True:
self.image =self.imagesRight[0]
self.image =self.imagesRight[1]
#if player is going up
while faceWhatDirection == 3 and keyDown == True:
self.image =self.imagesUp[0]
self.image =self.imagesUp[1]
#if player is going down
while faceWhatDirection == 4 and keyDown == True:
self.image =self.imagesDown[0]
self.image =self.imagesDown[1]
#initialize pygame
pygame.init()
#set the height and width of the screen
width = 800
height = 480
mainScreen = pygame.display.set_mode([width,height])
#A list off all of the sprites in the game
all_sprites_list = pygame.sprite.Group()
#creates a player object
player = Player()
all_sprites_list.add(player)
#a conditional for the loop that keeps the game running until the user Xes out
done = False
#clock for the screen updates
clock = pygame.time.Clock()
while done==False:
for event in pygame.event.get(): #user did something
if event.type == pygame.QUIT: #if the user hit the close button
done=True
if event.type == pygame.KEYDOWN: # if the user pushes down a key
if event.key == pygame.K_LEFT: #if the left arrow key is pressed
player.rect.x -=1
faceWhatDirection = 1
keyDown = True
if event.key == pygame.K_RIGHT: #if the left arrow key is pressed
player.rect.x +=1
faceWhatDirection = 2
keyDown = True
if event.key == pygame.K_UP: #if the left arrow key is pressed
player.rect.y -=1
faceWhatDirection = 3
keyDown = True
if event.key == pygame.K_DOWN: #if the left arrow key is pressed
player.rect.y +=1
faceWhatDirection = 4
keyDown = True
if event.type == pygame.KEYUP: # if the user pushes down a key
if event.key == pygame.K_LEFT: #if the left arrow key is pressed
keyDown = False
if event.key == pygame.K_RIGHT: #if the left arrow key is pressed
keyDown = False
if event.key == pygame.K_UP: #if the left arrow key is pressed
keyDown = False
if event.key == pygame.K_DOWN: #if the left arrow key is pressed
keyDown = False
mainScreen.fill(white)#makes the background white, and thus, the white part of the images will be invisible
player.changeImage()
#draw the sprites
all_sprites_list.draw(mainScreen)
#limit the game to 20 fps
clock.tick(20)
#update the screen on the regular
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()