按空格键时绘制多边形 - Pygame(Python 3.6)

问题描述 投票:0回答:1

我有一个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变量的尝试相同

python-3.x pygame
1个回答
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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.