Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 按下空格键时绘制多边形-Pygame(Python 3.6)_Python 3.x_Pygame - Fatal编程技术网

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

Python 3.x 按下空格键时绘制多边形-Pygame(Python 3.6),python-3.x,pygame,Python 3.x,Pygame,我对pygame有一个问题:我希望当我按下空格键时它显示为正方形,但我肯定犯了一个错误,因为它不是每次我按下键时都可见,并且它显示为一帧。我的想法是,当到达屏幕顶部时,正方形上升,然后下降。我可以用while循环来做这个吗?每一个更正都是有帮助的,因为我很少编写代码,而且我只看过一本关于pygame的指南 import pygame pygame.init() bianco=(255,255,255) nero=(0,0,0) rosso=(255,0,0) verde=(0,255,0) bl

我对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()
--------------编辑----------------

为了让红场平静下来,我尝试了这个,但我可能误解了这个建议。。。
我在对象部分写下了
go_down=False
,而在更新部分我在这里添加了代码。结果与前一次尝试相同,在主循环创建变量之前,没有
go_down
variable

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)
顺便说一句:我用它来保持玩家的大小和位置(并绘制矩形),以便以后我可以用它来检查碰撞

player_rect.colliderect(enemy_rect)

player_rect.collidepoint(mouse_position)
代码:

导入pygame
#PEP8-逗号后的空格及其周围=
#PEP8-当您必须显示代码时,英文名称会有所帮助
#---常量---(大写字母\名称)
白色=(255,255,255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
#人眼每秒至少需要25帧才能看到动画。
#如果没有动画就足够了。
#典型的监视器刷新屏幕频率为60 Hz,表示每秒60帧
#因此,175 FPS可能是无用的。
FPS=25
#---梅因---
#-初始化-
pygame.init()
display=pygame.display.set_模式((800600))
pygame.display.set_标题(“Prova”)
#-反对-
player_rect=pygame.rect(400,360,30,40)
lead_x_change=0
潜在客户变更=0
red_rect=pygame.rect(100100100100)
display_rect=False#控制rect
下降=错误
曲目=[]
#-主回路-
clock=pygame.time.clock()#在循环之前只创建一次
乌西塔=假
虽然不是uscita:
#-事件-
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
乌西塔=真
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_RIGHT:
铅x铅变化+=5
如果event.key==pygame.K_左:
lead_x_change+=-5
如果event.key==pygame.K_空间:
display_rect=True
#显示\u rect=不显示\u rect
#tracks.append(player_rect.copy())
如果event.type==pygame.KEYUP:
如果event.key输入(pygame.K_右,pygame.K_左):
lead_x_change=0
#如果event.key==pygame.K_空间:
#display_rect=False
#-更新-
玩家x+=领先者x改变
玩家y+=领先者y改变
如果显示正确:
如果发生以下情况:
红色矩形y+=5
如果红色矩形底部>400:
红色矩形底部=400
其他:
红色矩形y-=5

如果是red_rect.top,您希望只创建一个正方形还是多个正方形?只有在按住空格键的情况下方格才会出现吗?对于移动和弹跳,请阅读。当您按空格键时,然后设置
display_rect=True
,并在
事件之后使用它
循环到
draw.rect()
它工作正常,但我不需要它复制播放器。。。然而,我也学会了这一点。现在我怎样才能让广场倒塌呢?我试过使用while循环:while y>20,display_rect=True,y_change(新红方块的)+=10,然后y_change+=1你不能在while
中使用while
循环while True
。你必须使用
while True
作为你的
循环
,它会改变obejcts在每一帧/循环中的位置。最后一个问题是,如果我想让它在到达y=0(顶部)时,它必须掉下来,这两条指令相互冲突:如果它向下y>0,那么它将向上上升,则使用变量
go\u down=True/False
direction=“down”
来控制方向。必须使用
go\u down
类似于
display\u rect
。您必须在
while
循环之前指定
go\u down=False
,如果go\u down:。。。否则:…
内部,而
循环。但是您不检查
而执行
red\u rect.y-=5
,如果向下执行:
,则始终执行它
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()