我开始制作一个游戏,结果我遇到了一个小问题:
*当按下“空格键”时,红色方块会随机显示
问题
我怎样才能把红色方块变成障碍?在
我是个彻头彻尾的乞丐,我很抱歉问这么简单的问题。。:/
代码可能会给你一个想法:import pygame, sys
from random import randint
from pygame.locals import*
"List the Stores the Squares"
red_square_list = []
gameDisplay_width = 800
gameDisplay_height = 600
pygame.init()
gameDisplay = pygame.display.set_mode((gameDisplay_width, gameDisplay_height))
pygame.display.set_caption("Square-it")
clock = pygame.time.Clock()
red_color = pygame.Color("red")
"White Square"
white_x = 400
white_y = 300
white_width = 10
white_height = 10
white_color = pygame.Color("white")
white = pygame.Rect(white_x, white_y, white_width, white_height)
"Red Squares"
def create_red_square(x, y):
red_width = 10
red_height = 10
red = pygame.Rect(x, y, red_width, red_height)
return red
while True:
clock.tick(60)
gameDisplay.fill((0, 20, 5))
gameDisplay.fill(white_color, white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for each in red_square_list:
gameDisplay.fill(red_color, each)
pygame.display.update()
'''White Square Movement'''
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
white.left = white.left - 4
if keys[K_RIGHT]:
white.right = white.right + 4
if keys[K_UP]:
white.top = white.top - 4
if keys[K_DOWN]:
white.bottom = white.bottom + 4
"when Space key Pressed, Spawns red Squares"
if keys[K_SPACE]:
x = randint(0, gameDisplay_width)
y = randint(0, gameDisplay_height)
red_square_list.append(create_red_square(x, y))