簡體   English   中英

pygame中的重力使我的程序崩潰

[英]Gravity in pygame is crashing my program

因此,我正在pygame中開發側滾動游戲,並且正在嘗試實現引力。 我現在可以在理論上正常工作的代碼,但該程序只是停止響應,我已經關閉了它。 我什至沒有得到任何錯誤,讓我知道什么地方出了錯。

我曾經讓重力起作用,但是只有在沒有按鍵按下時才激活它。 如果按住“跳轉按鈕”(空格鍵),則將無限向上移動,釋放后,最終將開始下降。 因此,我改變了主意,開始着手在按鍵事件內部循環進行引力。 在循環內,我添加了兩個變量gravity和grav_time,並將變量add_grav_time設置為True。 雖然這是真的,並且重力小於一個數字,但我還是使角色跳了起來。 一旦重力超過一定數量,我就使角色跌倒。 然后我重新設置重力。 我的代碼沒有看到任何錯誤,所以我不知道問題出在哪里。

import Level, pygame, sys
import time
pygame.init()
f=open("lvl1.txt")
lvldata=[]
for line in f:
    x=line.split()
    lvldata.append([int(v)for v in x]) 

size=1000,555
screen=pygame.display.set_mode(size)
pygame.key.set_repeat(15,15)
lvl=Level.Level(lvldata,screen,size)
hero_img=pygame.image.load("hero.png")
herorect=pygame.Rect(lvl.spawn_x, lvl.spawn_y, 100, 100)
hero_position=[lvl.spawn_x, lvl.spawn_y]
velocity = [0,0]
face='R'
bkrd = pygame.image.load("bkrd2.png")
bkrdrect = bkrd.get_rect()
Gc=0
while True:
    Gc+=0.1
    if lvl.checkcollision(herorect):
        velocity=[0,0]

    for event in pygame.event.get():
        keys=pygame.key.get_pressed()
        if keys[pygame.QUIT]:
            sys.exit()

        if event.type == pygame.KEYDOWN:

            if keys[pygame.K_LEFT]:
                if face=='R':
                    hero_img = pygame.transform.flip(hero_img, True, False)
                    face='L'
                if herorect.left>0:
                    hero_position[0]-=10
                else:
                    hero_position[0]=0

            if keys[pygame.K_RIGHT]:
                if face=='L':
                    hero_img = pygame.transform.flip(hero_img, True, False)
                    face='R'
                if herorect.right<size[0]:
                    hero_position[0]+=10

            if keys[pygame.K_SPACE]:
                if velocity[1] == 0:
                    gravity=0
                    grav_time=1
                    add_grav_time = True
                    while add_grav_time:
                        gravity += grav_time
                        while gravity <30:
                            velocity[1] -=5
                        while gravity >=30:
                            velocity[1] -=5










                    #gravity = 0 
                    #timer.start()
                    #while gravity <50:
                    #   velocity[1] -=5
                    #   gravity += timer.s()
                    #while gravity >=50:    
                    #   velocity[1] +=5



                    #while velocity[1] >=-10:
                    #   velocity[1] -=5
                    #while velocity[1] <=-10:



        elif event.type==pygame.KEYUP:
            add_time = False
            gravity = 0
            if event.key==pygame.K_SPACE and velocity[1]<0 :velocity[1] +=10
                        #if event.key==pygame.K_space




                                #Gc=0
                        #elif not keys[pygame.K_SPACE]:
                                #velocity[1]+=Gc

    herorect.left=hero_position[0]
    herorect.top=hero_position[1]
    if hero_position[1]<0:
        hero_position[1]-=hero_position[1]
    hero_position[0]+=velocity[0]
    hero_position[1]+=velocity[1]
    if herorect.left<0 or herorect.right>=size[0]: velocity[0]=0
    #screen.fill((0,0,0))
    screen.blit(bkrd,bkrdrect)

    lvl.draw()
    screen.blit(hero_img, herorect)
    pygame.display.flip()

print lvl

程序停止的直接原因是無限循環: while條件在循環內不會改變,因此一旦輸入,循環就永遠不會退出。

但是,您的問題更具概念性。 您不能在游戲循環中使用循環來執行此操作:無論如何,游戲循環都需要繼續旋轉。 在游戲循環的每一遍中,您都可以稍微調整速度,直到達到地面(或您具有的任何其他條件)為止。

如果您要對真實的物理場進行建模,則跳躍應該使您立即將速度向上增加; 然后在主循環的每個通道上,將該速度減小一個常數(在現實世界中為9.81 m / s / s),並在地面上將其設為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM