Pygame屏幕不会在每个循环之前消失

时间:2019-10-31 18:56:14

标签: python pygame

我到处都在寻找答案,似乎无法弄清楚。 这是我实际运行pygame以及创建car对象的代码。 我怀疑该问题可能与创建Car对象时如何在屏幕中输入内容有关。 任何帮助,将不胜感激。 谢谢!

主要:

import sys
import pygame
from carObject import Car

# set up display
screen = pygame.display.set_mode(size=(1000, 800))
pygame.display.set_caption("jtest")

# color background
background = pygame.Surface(screen.get_size())

WHITE = (255, 255, 255)

# white background
background.fill(WHITE)
screen.blit(background, (0, 0))

# create car
c = Car(background, 475, 650)

while True:
    for event in pygame.event.get():
        # update screen
        pass

    # draw things that are happening
    screen.fill(WHITE)
    c.drawCar()
    c.driveForward()
    c.driveLeft()
    screen.blit(background, (0, 0))
    pygame.display.flip()

    # if game window is close, end game 
    if event.type == pygame.QUIT:
        pygame.quit()
        break

汽车对象

import pygame


class Car:

    def __init__(self, screen, x, y):

        # takes screen to draw to as input
        # takes x and y coordinates for starting position

        self.X = x
        self.Y = y

        # set car color
        self.BODY_COLOR = (0, 0, 255)

        #set tire color
        self.TIRE_COLOR = (0, 0, 0)

        #set headlight color
        self.HEADLIGHT_COLOR = (255, 255, 0)

        # set screen
        self.SCREEN = screen

        # set speed
        self.SPEED = 1




    def drawCar(self):

        # store l and w as variables
        length = 120
        width = 65

        # draw main body
        pygame.draw.rect(self.SCREEN, self.BODY_COLOR, (self.X, self.Y, width, length))

        # draw tires
        # front left tire
        pygame.draw.rect(self.SCREEN, self.TIRE_COLOR, (self.X-10, self.Y+15, 10, 30))
        # front right tire
        pygame.draw.rect(self.SCREEN, self.TIRE_COLOR, (self.X+width, self.Y+15, 10, 30))
        # back left tire
        pygame.draw.rect(self.SCREEN, self.TIRE_COLOR, (self.X-10, self.Y+75, 10, 30))
        # back right tire
        pygame.draw.rect(self.SCREEN, self.TIRE_COLOR, (self.X+width, self.Y+75, 10, 30))

        # draw headlights
        # left headlight
        pygame.draw.circle(self.SCREEN, self.HEADLIGHT_COLOR, (self.X+15, self.Y+12), 7, 0)
        # right headlight
        pygame.draw.circle(self.SCREEN, self.HEADLIGHT_COLOR, (self.X+50, self.Y+12), 7, 0)


    def driveForward(self):
        self.Y -= self.SPEED

    def driveBackward(self):
        self.Y += self.SPEED

    def driveLeft(self):
        self.X -= self.SPEED

    def driveRight(self):
        self.X += self.SPEED

2 个答案:

答案 0 :(得分:1)

将您的显示循环更改为该循环,以便在拉动汽车之前清除背景。由于您是在背景上绘制图片,并且尺寸与屏幕相同,因此无需在每一帧中WHITE填充屏幕。

while True:
    for event in pygame.event.get():
        # if game window is close, end game
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # draw things that are happening
#    screen.fill(WHITE)  # Not needed.
    background.fill(WHITE)
    c.drawCar()
    c.driveForward()
    c.driveLeft()

    screen.blit(background, (0, 0))

    pygame.display.flip()

答案 1 :(得分:0)

我已经解决了!

必须从Car对象的drawCar()函数内部更新screen.fill