屏幕被闪电扫描后,当已经有一个移动的矩形时,如何制作一个矩形



我在PyGame中制作这个游戏,我有一个移动的矩形,所以我想制作另一个矩形,当我按下空格键时,它会被绘制到屏幕上。因此,我尝试添加if语句,但它不起作用,因为只要绘制矩形,屏幕就会填充。有人能告诉我在屏幕上填充颜色后如何绘制矩形吗?

这是我的代码

import pygame
from pygame.locals import *
pygame.init()
screenwidth = 1200
screenheight = 500
screen = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Bullepacito')
def _rect():
run = False
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_SPACE:
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(400, 400, 20, 20))
def gameloop():
run = False
shooterx = 350
shootery = 350
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
shooterx -= 5  
if event.key == pygame.K_RIGHT:
shooterx += 5
screen.fill((30, 30, 30))
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(shooterx, shootery, 50, 50))
# Code to draw the rectangle on key press
pygame.display.update()
gameloop()

按下SPACE时设置布尔状态(draw_rect(,并根据状态绘制矩形:

import pygame
from pygame.locals import *
pygame.init()
screenwidth = 1200
screenheight = 500
screen = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Bullepacito')
def gameloop():
run = False
shooterx = 350
shootery = 350
draw_rect = False
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
shooterx -= 5  
if event.key == pygame.K_RIGHT:
shooterx += 5
if event.key == pygame.K_SPACE:
draw_rect = True
screen.fill((30, 30, 30))
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(shooterx, shootery, 50, 50))
if draw_rect:
pygame.draw.rect(screen, (255, 0, 255), pygame.Rect(400, 400, 20, 20))
pygame.display.update()
gameloop()

相关内容

最新更新