2016-10-13 5 views
0

私はRectでジャンプをシミュレートしようとしています。ジャンプシミュレーションが地面に戻ったときに落ちなくなる

ジャンプをシミュレートするために、KEYDOWNとKEYUPイベントとK_SPACEボタンを設定しました。

私の難しさは、直腸が地面に到達したとき(コードXを使用して)戻ってくる落下を止めることです。

GAME_BEGINステートメントでは機能しないとわかります。

import pygame 

pygame.init() 
screen = pygame.display.set_mode((400,300)) 
pygame.display.set_caption("shield hacking") 
JogoAtivo = True 
GAME_BEGIN = False 
# Speed in pixels per frame 
speedX = 0 
speedY = 0 
cordX = 10 
cordY = 100 
jumping=False; 
run=False; 


def draw(): 
    screen.fill((0, 0, 0)) 
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52)) 
    pygame.display.flip(); 



while JogoAtivo: 
    for evento in pygame.event.get(): 
     print(evento) 
    #verifica se o evento que veio eh para fechar a janela 
     if evento.type == pygame.QUIT: 
       JogoAtivo = False 
       pygame.quit(); 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_a: 
        print('GAME BEGIN') 
        GAME_BEGIN = True 
        draw();   
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_LEFT: 
        speedX=-0.006 
        run= True; 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_RIGHT: 
        speedX=0.006 
        run= True; 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_SPACE: 
        speedY=-0.090 
        jumping= True; 
     if evento.type == pygame.KEYUP: 
      if evento.key == pygame.K_SPACE: 
        speedY=+0.090 
        jumping= True; 


    if GAME_BEGIN: 
     if not jumping: 
      gravity = cordX; 
     """if gravity == cordY: 
      speedY=0;""" 
     cordX+=speedX 
     cordY+=speedY 
     draw() 

は、ここに私のコードで、

import pygame 

pygame.init() 
screen = pygame.display.set_mode((400,300)) 
pygame.display.set_caption("shield hacking") 
JogoAtivo = True 
GAME_BEGIN = False 
# Speed in pixels per frame 
speedX = 0 
speedY = 0 
cordX = 10 
cordY = 100 
groundX=0; 
groundY=150; 
jumping=False; 
run=False; 


def draw(): 
    screen.fill((0, 0, 0)) 
    ground = pygame.draw.rect(screen, (0, 255, 0), (groundX, groundY,400, 10)) 
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52)) 
    pygame.display.flip(); 



while JogoAtivo: 
    for evento in pygame.event.get(): 
     print(evento) 
    #verifica se o evento que veio eh para fechar a janela 
     if evento.type == pygame.QUIT: 
       JogoAtivo = False 
       pygame.quit(); 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_a: 
        print('GAME BEGIN') 
        GAME_BEGIN = True 
        draw();   
      if evento.key == pygame.K_LEFT: 
        speedX=-0.006 
        run= True; 
      if evento.key == pygame.K_RIGHT: 
        speedX=0.006 
        run= True; 
      if evento.key == pygame.K_SPACE: 
        speedY=-0.090 
        jumping= True; 
     if evento.type == pygame.KEYUP: 
      if evento.key == pygame.K_SPACE: 
        speedY=+0.090 
        jumping= True; 


    if GAME_BEGIN: 
     cordX+=speedX 
     cordY+=speedY 
     draw(); 
     if cordY +50>= groundY: 
      speedY=0 
      jumping=False; 
+0

ところで:あなたは 'もしevento.type == pygame.KEYDOWN繰り返す必要はありませんが、' - それはコードが読みにくくなります。 'KEYUP、K_SPACE'の – furas

+0

に' jumping = True'を設定すると、ジャンプし続けるようになります。そして、いつジャンプを止めるか(つまり落ちるか)を決めるルールはありません。 'codeY <= groundY:jumping = False' – furas

答えて

0

TKSは、私はあなたが言ったことを行っているBELOW UPDATED

import pygame 

pygame.init() 
screen = pygame.display.set_mode((400,300)) 
pygame.display.set_caption("shield hacking") 
JogoAtivo = True 
GAME_BEGIN = False 
# Speed in pixels per frame 
speedX = 0 
speedY = 0 
cordX = 10 
cordY = 100 
groundX=0; 
groundY=150; 
jumping=False; 
run=False; 


def draw(): 
    screen.fill((0, 0, 0)) 
    ground = pygame.draw.rect(screen, (0, 255, 0), (groundX, groundY,400, 10)) 
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52)) 
    pygame.display.flip(); 



while JogoAtivo: 
    for evento in pygame.event.get(): 
     print(evento) 
    #verifica se o evento que veio eh para fechar a janela 
     if evento.type == pygame.QUIT: 
       JogoAtivo = False 
       pygame.quit(); 
     if evento.type == pygame.KEYDOWN: 
      if evento.key == pygame.K_a: 
        print('GAME BEGIN') 
        GAME_BEGIN = True 
        draw();   
      if evento.key == pygame.K_LEFT: 
        speedX=-0.006 
        run= True; 
      if evento.key == pygame.K_RIGHT: 
        speedX=0.006 
        run= True; 
      if evento.key == pygame.K_SPACE: 
        speedY=-0.090 
        jumping= True; 
     if evento.type == pygame.KEYUP: 
      if evento.key == pygame.K_SPACE: 
        speedY=+0.090 
        jumping= True; 


    if GAME_BEGIN: 
     cordX+=speedX 
     cordY+=speedY 
     draw(); 
     if cordY +50>= groundY: 
      speedY=0 
      jumping=False; 

本当に助けていただきありがとうございます。

+0

BTW:はるかに複雑な[platform jumper](http://programarcadegames.com/python_examples/show_file.php?file=platform_jumper.py) – furas