pygame,在屏幕上移动一个正方形,但不能擦除以前的移动 [英] Pygame, move a square on the screen, but can not erase the previous movements

查看:439
本文介绍了pygame,在屏幕上移动一个正方形,但不能擦除以前的移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在StackOverflow上的第一篇文章,希望你们可以帮助新手程序员.这是Pygame Python的简单询问.

This is my first post in StackOverflow, hope you guys can help a newbie programmer. It's Pygame Python simple ask.

我正在尝试在屏幕上移动一个正方形,但无法擦除以前的移动.

I am trying to move a square on the screen, but can not erase the previous movements.

import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
x_speed = 0
y_speed = 0
cordX = 10
cordY = 100


def desenha():
    quadrado = pygame.Rect(cordX, cordY ,50, 52)

    pygame.draw.rect(screen, (255, 0, 0), quadrado)
    pygame.display.flip()



while JogoAtivo:
    for evento in pygame.event.get():
        print(evento);
    #verifica se o evento que veio eh para fechar a janela
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if evento.type == pygame.KEYDOWN:     
            if evento.key == pygame.K_SPACE:
                   print('GAME BEGIN')
                   desenha()
                   GAME_BEGIN = True;
            if evento.key == pygame.K_LEFT and GAME_BEGIN:   
                   speedX=-3
                   cordX+=speedX
                   desenha()


            if evento.key == pygame.K_RIGHT and GAME_BEGIN:
                   speedX=3
                   cordX+=speedX
                   desenha()   

推荐答案

您必须绘制上一张图像.最简单的方法是在游戏循环开始时用背景色填充屏幕,如下所示:

You have to draw over the previous image. Easiest is to fill the screen with a background color in the beginning of the game loop, like so:

screen.fill((0, 0, 0))

这篇关于pygame,在屏幕上移动一个正方形,但不能擦除以前的移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆