输入这个 pygame 代码进行动画测试,我期待一些错误,但程序根本不会执行:
第一部分导入模块
import sys
import os
import pygame
from pygame.locals import *
初始化 pygame
pygame.init ()
加载精灵图像文件的类,然后加载声音
class Processes (object):
@staticmethod
def load_image (imagefile):
image = pygame.image.load (imagefile)
return image, image.get_rect ()
精灵类
class Sprite (pygame.sprite.Sprite):
def __init__ (self):
pygame.sprite.Sprite.__init__ (self)
self.image, self.rect = Processes.load_image ('frame1.jpg')
简单的动画功能——让生活更轻松
def animation (self, key_event):
if key_event == K_DOWN:
self.rect.centery = self.rect.centery - 10
if key_event == K_UP:
self.rect.centery = self.rect.centery + 10
if key_event == K_RIGHT:
self.rect.centerx = self.rect.centerx + 10
if key_event == K_LEFT:
self.rect.centerx = self.rect.centerx - 10
return self.rect
主功能:
def main ():
mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))
游戏循环:
while True:
Surface = pygame.display.set_mode ((400, 400))
pygame.display.set_caption ('Animation Test')
for event in pygame.event.get ():
if event.type == KEYDOWN:
mecha.animation (event.key)
if event.type == QUIT:
pygame.quit ()
sys.exit (0)
allsprites.draw (Surface)
pygame.display.update ()
这是实际的“错误”
C:/Khan's Family/python/Bunnies Vs. Zombies/BvZ_Animtest.py
现在这里是输出:
(Nothing)
C:/Khan's Family/python/Bunnies Vs. Zombies
没有输出,这是我的问题。