首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Pygame屏幕不会在每次循环前擦除

Pygame屏幕不会在每次循环前擦除
EN

Stack Overflow用户
提问于 2019-11-01 02:56:14
回答 2查看 56关注 0票数 1

我到处寻找答案,但似乎找不到答案。这是我实际运行pygame和创建car对象的地方的代码。我怀疑这个问题可能与我在创建Car对象时在屏幕上输入的方式有关。任何帮助都将不胜感激。谢谢!

main:

代码语言:javascript
运行
复制
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

Car对象

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-01 03:01:04

我已经解决了!

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

票数 0
EN

Stack Overflow用户

发布于 2019-11-01 03:48:11

将您的显示循环更改为此,以便在其上绘制汽车之前清除背景。由于您是在背景上绘制,并且它与屏幕的大小相同,因此没有必要在屏幕上填充每一帧的WHITE

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58649706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档