屏幕被块化后,如果已经有一个移动的矩形,如何制作一个矩形?
After the screen is blitted how to make a rectangle when there is already an existing moving rectangle?
我正在 PyGame 中制作这个游戏并且我有一个移动的矩形,所以我想制作另一个矩形,当我按下 Space 栏时它会被绘制到屏幕上。所以我尝试添加 if 语句,但它不起作用,因为一旦绘制了矩形,屏幕就会填满。谁能告诉我如何在屏幕填充颜色后绘制矩形?
Here is my code
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()
我正在 PyGame 中制作这个游戏并且我有一个移动的矩形,所以我想制作另一个矩形,当我按下 Space 栏时它会被绘制到屏幕上。所以我尝试添加 if 语句,但它不起作用,因为一旦绘制了矩形,屏幕就会填满。谁能告诉我如何在屏幕填充颜色后绘制矩形?
Here is my code
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()