我有一个pygame的问题:当我按下空格键时我想要一个正方形,但我肯定犯了一个错误,因为每次按下键时它都不会显示,它出现在一个框架中。我的想法是,当到达屏幕顶部时,方块上升然后下降。我可以用while循环吗?每次更正都很有用,因为我很少编写代码,而且我只阅读了有关pygame的指南。
import pygame
pygame.init()
bianco=(255,255,255)
nero=(0,0,0)
rosso=(255,0,0)
verde=(0,255,0)
blu=(0,0,255)
display=pygame.display.set_mode((800,600))
pygame.display.set_caption("Prova")
px = 400
py = 360
h=40
lead_x_change = 0
lead_y_change = 0
axx = px
axy = py-30
uscita = False
while not uscita:
display.fill((86,170,239))
main = pygame.draw.rect(display,rosso,[px,py,30,h])
pygame.draw.rect(display, verde,[0,400,800,800])
px += lead_x_change
py += lead_y_change
for event in pygame.event.get():
if event.type == pygame.QUIT:
uscita = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
lead_x_change +=1
if event.key == pygame.K_LEFT:
lead_x_change +=-1
if event.key == pygame.K_SPACE:
pygame.draw.rect(display,rosso,[100,100,100,100])
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
lead_x_change = 0
#if event.key == pygame.K_SPACE:
# pygame.draw.rect(display, blu,[0,400,800,800])
pygame.display.update()
clock = pygame.time.Clock()
clock.tick(175)
pygame.quit()
quit()
--------------编辑----------------
# - updates -
player_rect.x += lead_x_change
player_rect.y += lead_y_change
if display_rect:
red_rect.y -= 5
if red_rect.y == 0:
go_down = True
if go_down:
red_rect.y += 5
为了让red_square失效我尝试了这个,但我可能误解了这个建议......
我在对象部分go_down = False
中写道,而在更新部分,我在这里添加了代码。结果与之前没有go_down
变量
答案 0 :(得分:0)
在主循环之前创建变量
display_rect = False
按下空格更改值
if event.key == pygame.K_SPACE:
display_rect = True
以后使用此值绘制矩形
if display_rect:
pygame.draw.rect(display, RED, [100, 100, 100, 100])
如果您想使用空格切换(显示/隐藏),请使用
if event.key == pygame.K_SPACE:
display_rect = not display_rect
如果要在按空格键时添加更多矩形,请创建列表
tracks = []
并将新的rect添加到此列表
if event.key == pygame.K_SPACE:
tracks.append(player_rect.copy())
以后使用此列表显示矩形
for item in tracks:
pygame.draw.rect(display, BLUE, item)
顺便说一句:我使用pygame.Rect()来保持玩家的大小和位置(以及绘制矩形)以便稍后我可以用它来检查碰撞,即。
player_rect.colliderect(enemy_rect)
player_rect.collidepoint(mouse_position)
代码:
import pygame
# PEP8 - spaces after comma and around =
# PEP8 - english names helps when you have to show code
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Human eye need at least 25 frames per second to see animatioon.
# and it is enough if there is no animation.
# Typical monitors refresh screen with 60 Hz and it means 60 FPS
# so 175 FPS can be useless.
FPS = 25
# --- main ---
# - init -
pygame.init()
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Prova")
# - object -
player_rect = pygame.Rect(400, 360, 30, 40)
lead_x_change = 0
lead_y_change = 0
red_rect = pygame.Rect(100, 100, 100, 100)
display_rect = False # to control rect
go_down = False
tracks = []
# - mainloop -
clock = pygame.time.Clock() # create only once, before loop
uscita = False
while not uscita:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
uscita = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
lead_x_change += 5
if event.key == pygame.K_LEFT:
lead_x_change += -5
if event.key == pygame.K_SPACE:
display_rect = True
#display_rect = not display_rect
#tracks.append(player_rect.copy())
if event.type == pygame.KEYUP:
if event.key in (pygame.K_RIGHT, pygame.K_LEFT):
lead_x_change = 0
#if event.key == pygame.K_SPACE:
# display_rect = False
# - updates -
player_rect.x += lead_x_change
player_rect.y += lead_y_change
if display_rect:
if go_down:
red_rect.y += 5
if red_rect.bottom > 400:
red_rect.bottom = 400
else:
red_rect.y -= 5
if red_rect.top <= 0:
red_rect.top = 0
go_down = True
# - draws -
display.fill((86, 170, 239))
pygame.draw.rect(display, GREEN, [0, 400, 800, 800])
if display_rect:
pygame.draw.rect(display, RED, red_rect)
for item in tracks:
pygame.draw.rect(display, BLUE, item)
pygame.draw.rect(display, RED, player_rect)
pygame.display.update()
clock.tick(FPS)
# - end -
pygame.quit()
quit()