2016-12-05 9 views
0

私のプログラムでは、車を飛び越える広場があります。しかし、プレーヤーは単にスペースを押さえることができ、決して地面に触れることはできません。つまり、これはできません。私はどのようにして、キューブが一定時間後に地面に戻ってくるように強制するのだろうと思っています。ここでプレーヤーをキーアップするように強制するには?

は私のコードです:

import pygame 
import time 

pygame.init() 
screen = pygame.display.set_mode((800, 600)) 
pygame.display.set_caption('Traffic Jumper') 

orange = (255, 100, 0) 
black = (0,0,0) 
grey = (128,128,128) 
yellow = (255,255,0) 
green = (0,128,0) 
red=(255,0,0) 
blue = (64,224,208) 
brown = (139,69,19) 
magenta = (255,0,255) 
olive = (128,128,0) 
clock = pygame.time.Clock() 

def options(): 
    op = True 
    while op: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.QUIT() 
      screen.fill(magenta) 
      largeText = pygame.font.Font('freesansbold.ttf',115) 
      Textsurf, TextRect = text_objects("Get Hit Bro!", largeText) 
      TextRect.center = ((400),(300)) 
      screen.blit(Textsurf, TextRect) 
      smallText = pygame.font.Font('freesansbold.ttf',30) 
      Textsurf, TextRect1 = text_objects("Press Y to restart and N to\ leave", smallText) 
      TextRect1.center = (400,(400)) 
      screen.blit(Textsurf, TextRect1) 
      pygame.display.update() 
      for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_y: 
        game_loop() 
       elif event.key == pygame.K_n: 
        pygame.quit() 

def pause(): 
    paused = True 
    while paused: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       paused=False 
       pygame.QUIT() 
       quit() 
     screen.fill(olive) 
     largeText = pygame.font.Font('freesansbold.ttf',115) 
     Textsurf, TextRect = text_objects("Paused", largeText) 
     TextRect.center = ((400),(300)) 
     screen.blit(Textsurf, TextRect) 
     smallText = pygame.font.Font('freesansbold.ttf',30) 
     Textsurf, TextRect1 = text_objects("Press r To Resume", smallText) 
     TextRect1.center = (400,(400)) 
     screen.blit(Textsurf, TextRect1) 
     pygame.display.update() 
     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_r: 
        paused = False 



def text_objects(text, font): 
    textsurface = font.render(text,True,red) 
    return textsurface, textsurface.get_rect() 

def message_display(text): 
    largeText = pygame.font.Font('freesansbold.ttf',115) 
    Textsurf, TextRect = text_objects(text, largeText) 
    TextRect.center = ((400),(300)) 
    screen.blit(Textsurf, TextRect) 
    pygame.display.update() 
    time.sleep(2) 
    #game_loop() 

def game_loop(): 
    paused = False 
    score = 0 
    x = 20 
    y = 520 
    carx = 1000 
    cary = 490 
    carspeed = 7 
    done = False 
    while not done: 
      screen.fill((255, 255, 255)) 
      sky=pygame.draw.rect(screen,blue,pygame.Rect(0,0,800,400)) 
      grass=pygame.draw.rect(screen,green,pygame.Rect(0,400,800,100)) 
      floor1=pygame.draw.rect(screen,black,pygame.Rect(0,500,800,10)) 
      floor2=pygame.draw.rect(screen,black,pygame.Rect(0,590,800,10)) 
      road=pygame.draw.rect(screen,grey,pygame.Rect(0,510,800,80)) 
      paint1=pygame.draw.rect(screen,yellow,pygame.Rect(10,540,100,\ 20)) 
      paint2=pygame.draw.rect(screen,yellow,pygame.Rect(200,540,100,\ 20)) 
      paint3=pygame.draw.rect(screen,yellow,pygame.Rect(400,540,100,\ 20)) 
      paint4=pygame.draw.rect(screen,yellow,pygame.Rect(600,540,100,\ 20)) 
      paint5=pygame.draw.rect(screen,yellow,pygame.Rect(780,540,100,\ 20)) 
      player=pygame.draw.rect(screen, orange, pygame.Rect(x, y, 60,\ 60)) 
      car=pygame.draw.rect(screen,black,pygame.Rect(carx,cary,80,\ 95)) 
      if x > 800 - 60: 
       x -= 3 
      elif x < 0: 
       x += 3 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        #done = True 
        quit() 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: 
       y -= 200 
      if event.type == pygame.KEYUP and event.key == pygame.K_SPACE: 
       y += 200 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_p: 
       pause() 


      pressed = pygame.key.get_pressed() 
      #if pressed[pygame.K_UP]: y -= 3 
      #if pressed[pygame.K_DOWN]: y += 3 
      if pressed[pygame.K_LEFT]: x -= 3 
      if pressed[pygame.K_RIGHT]: x += 3 
      carx -= carspeed 
      if carx < -100: 
       score += 1 
       carspeed += 0.2 
       carx = 1000 

      if x > carx - 55 and x < carx + 55 and y > cary: 
       #message_display('Get Hit Bro!') 
       options() 
       font = pygame.font.SysFont(None,25) 
       text = font.render("Dodged: " + str(score) ,True,black) 
       screen.blit(text,(0,0)) 

     pygame.display.flip() 
     clock.tick(60) 
game_loop() 
+0

あなたはどんなライブラリを使っていますか? 'イベント 'とは何ですか? –

+0

私はすべてのコードを –

答えて

0

あなたはプレイヤーがスペースを押すたびに、設定されたカウンタを持つことができます。このカウンタは、各反復を下に数えることができる、それがゼロになると、y位置をバックに設定されます:

delta_y = 0 
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: 
    counter = 10 
if event.type == pygame.KEYUP and event.key == pygame.K_SPACE: 
    counter = 0 
if counter > 0: 
    delta_y = 200 
    counter -= 1 
else: 
    delta_y = 0 

あなたはwhileループの外counter = 0を設定する必要があります。私はdelta_yを垂直位置に追加しました。これはもう少しクリーンな方法です。この行を次のように更新する必要があります。

player=pygame.draw.rect(screen, orange, pygame.Rect(x, y + delta_y, 60, 60)) 

カウンタは10からカウントダウンします。これを試してみるだけです。

2

game_loopするためにこれらの変数を追加します。

hasJumped = False 
    jumpCooldown = 0.0 

をあなたのループでは、中に '行っていない間'

if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: 
      if hasJumped == False: 
       y -= 200 
       hasJumped = True 
    if hasJumped == True:   
     jumpCooldown += clock.get_time() 
    if jumpCooldown > [Jump Cooldown Limit]: # Set this number to the amount of time the car can jump at once. You'll have to experiment. 
     hasJumped = False 
     jumpCooldown = 0.0 

PSを追加:あなたはpygame.KEYDOWNを使用する理由私は理解していません/ KEYUPはキーのセットを検出し、もう一つのセットはpygame.key.get_pressed()を検出します。それは面倒なようです

関連する問題