跳回模拟在返回地面时停止下降

时间:2016-10-13 03:32:28

标签: python pygame

我正在尝试模拟Rect中的跳转。

我设置KEYDOWN和KEYUP事件以及K_SPACE按钮来模拟跳转。

我很难在直到地面时停止返回坠落(使用cordX)。

我意识到这不起作用,并在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;

1 个答案:

答案 0 :(得分:0)

Tks,我已经完成了你所说的,这是我的代码

    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;
  
    
      

非常感谢帮助。