1

我正在制作一个 pygame 程序来动画精灵,但问题不是一个特定的错误:

import sys
import os
import pygame
from pygame.locals import *

pygame.init ()

WHITE = ( 255, 255, 255 )
BLACK = (   0,   0,   0 )

Surface = pygame.display.set_mode ((400, 400), 0, 32)
pygame.display.set_caption ('Animation Test')




class Processes (object):
    @staticmethod
    def load_image (imagefile):
        image = pygame.image.load (imagefile)   #Remember to call imagefile as a string statement

        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')

        self.blindrect = pygame.Rect ((self.rect.top - 289, self.rect.left - 289), (self.rect.width, self.rect.height))




   def animation (self, key_event = None, mouse_event = None):

       blindpass = 0

       if key_event == K_LEFT:
       self.rect.centerx = self.rect.centerx - 10

       if key_event == K_RIGHT:
        self.rect.centerx = self.rect.centerx + 10

       if key_event == K_UP:
       self.rect.centery = self.rect.centery - 10

       if key_event == K_DOWN:
       self.rect.centery = self.rect.centery + 10


       if mouse_event == MOUSEBUTTONDOWN:
        self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()


    if blindpass > 0:

        if key_event == K_LEFT:
            self.blindrect.centerx = self.blindrect.centerx - 10

        if key_event == K_RIGHT:
            self.blindrect.centerx = self.blindrect.centerx + 10

        if key_event == K_UP:
            self.blindrect.centery = self.blindrect.centery - 10

        if key_event == K_DOWN:
            self.blindrect.centery = self.blindrect.centery + 10


        if mouse_event == MOUSEBUTTONDOWN:
            self.rect.centerx, self.rect.centery = pygame.mouse.get_pos () 




mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))



while True:
Surface.fill (WHITE)

for event in pygame.event.get ():
    if event.type == KEYDOWN:
    mecha.animation (key_event = event.key)

    if event.type == MOUSEBUTTONDOWN:
    mecha.animation (mouse_event = event.type)

    if event.type == QUIT:
    pygame.quit ()
    sys.exit (0)



allsprites.draw (Surface)
pygame.display.update ((mecha.rect, mecha.blindrect))

现在,错误有点奇怪。输出是精灵确实出现在屏幕上,当我提供键和鼠标输入时它会移动,但问题是它会留下痕迹。这就是为什么我创建了一个矩形来跟踪精灵矩形,该矩形应该在每次游戏循环循环时用白色填充。但事实并非如此。至少,直到我最小化窗口并再次拉起它,所有的痕迹都消失了,背景变成了白色而不是黑色。但是当我移动图像时,轨迹又开始形成。我最小化,它们按预期消失。

4

2 回答 2

0

如果您的渲染功能这样做:

screen.fill(white)
# blit or sprite group drawing
pygame.display.flip()

它将整个屏幕清除为白色,绘制一次精灵。不会有踪迹的。

  • 您的代码未正确粘贴。f 就像你的 if QUIT 语句没有缩进一样,对于一个多语句。与 Sprite 类相同
  • 如果您使用 Surface 或 Sprite 作为类/变量名称,您可能会遇到命名问题。
于 2012-11-03T01:33:00.510 回答
0
  1. 您没有正确初始化和更新盲区。这就是为什么屏幕的正确部分没有更新并且您的精灵留下痕迹的原因。
  2. 在您的更新循环中,由于您只更新显示更改的部分,因此您需要更新整个屏幕一次以在进入更新循环之前对其进行初始化。这就是为什么屏幕最初不是白色的原因。

请参阅下面的更新源(仅 Sprite 类和后来被修改):

class Sprite (pygame.sprite.Sprite):
    def __init__ (self):
        pygame.sprite.Sprite.__init__ (self)
        self.image, self.rect = Processes.load_image ('frame1.jpg')
        #initialize blindrect to be the same rectangle as rect
        self.blindrect = pygame.Rect ((self.rect.top, self.rect.left), (self.rect.width, self.rect.height))

    def animation (self, key_event = None, mouse_event = None):
        print(self.rect)
        print(self.blindrect)

        #save the current sprite position before moving the sprite
        self.blindrect.centerx = self.rect.centerx
        self.blindrect.centery = self.rect.centery

        if key_event == K_LEFT:
            self.rect.centerx = self.rect.centerx - 10

        if key_event == K_RIGHT:
            self.rect.centerx = self.rect.centerx + 10

        if key_event == K_UP:
            self.rect.centery = self.rect.centery - 10

        if key_event == K_DOWN:
            self.rect.centery = self.rect.centery + 10

        if mouse_event == MOUSEBUTTONDOWN:
            self.rect.centerx, self.rect.centery = pygame.mouse.get_pos ()

        print(self.rect)
        print(self.blindrect)

mecha = Sprite ()
allsprites = pygame.sprite.RenderPlain ((mecha, ))

#need to initially fill the entire surface with white
Surface.fill (WHITE)
pygame.display.update ((pygame.Rect((0,0),(400,400))))

while True:
    Surface.fill (WHITE)

    for event in pygame.event.get ():
        if event.type == KEYDOWN:
            mecha.animation (key_event = event.key)

        if event.type == MOUSEBUTTONDOWN:
            mecha.animation (mouse_event = event.type)

        if event.type == QUIT:
            pygame.quit ()
            sys.exit (0)

    allsprites.draw (Surface)
    #update only the previous sprite position and new sprite position
    pygame.display.update ((mecha.rect, mecha.blindrect))
于 2012-11-03T02:11:52.080 回答