带有矩形的pygame移动留下一条线

时间:2017-05-10 20:07:47

标签: python pygame

我有这个问题。我有矩形,如果我移动它不会删除它 离开之前的矩形

import pygame as pg
from map import Map
class Main:
    def __init__(self):
        self.gameDisplay = pg.display.set_mode((800,600))
        pg.display.set_caption("Pacman")
        self.x = 360
        self.y = 520
        self.speed = 20
        runGame = True
        pg.draw.rect(self.gameDisplay, (255, 255, 0), [self.x, self.y, 40, 40])
        while runGame:

            map = Map(self.gameDisplay)

            for event in pg.event.get():
                if event.type == pg.QUIT:
                    runGame = False

                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_w:
                        self.y -= self.speed
                    elif event.key == pg.K_a:
                        self.x -= self.speed
                    elif event.key == pg.K_s:
                        self.y += self.speed
                    elif event.key == pg.K_d:
                        self.x += self.speed
            pg.draw.rect(self.gameDisplay, (255, 255, 0), [self.x, self.y, 40, 40])
            map.renderMap()
            pg.display.update()




pg.init()
main = Main()

这是它在图片problem in image

中创建的内容

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了它

import pygame as pg
from map import Map

class Main:
    def __init__(self):
        self.gameDisplay = pg.display.set_mode((800,600))
        pg.display.set_caption("Pacman")
        self.x = 360
        self.y = 520
        self.speed = 20
        runGame = True
        while runGame:

            map = Map(self.gameDisplay)

            for event in pg.event.get():
                if event.type == pg.QUIT:
                    runGame = False

                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_w:
                        self.y -= self.speed
                    elif event.key == pg.K_a:
                        self.x -= self.speed
                    elif event.key == pg.K_s:
                        self.y += self.speed
                    elif event.key == pg.K_d:
                        self.x += self.speed
            self.gameDisplay.fill((0,0,0))
            pg.draw.rect(self.gameDisplay, (255, 255, 0), [self.x, self.y, 40, 40])
            map.renderMap()
            pg.display.update()




pg.init()
main = Main()