chrome dino 游戏在 pygame 中每次点击跳跃两次 [英] chrome dino game jumping twice per click in pygame

查看:58
本文介绍了chrome dino 游戏在 pygame 中每次点击跳跃两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我目前正在尝试在 pygame 中制作一个跳跃游戏,就像 chrome 恐龙游戏一样.我编写了一些简单的代码来绘制一个正方形并使其跳跃.我将在下面列出我的代码.我的问题是跳跃部分.每当我按下跳跃按钮时,方块就会跳跃多次(通常是 2 次).堆栈溢出的好人请帮助有需要的人.这是我的代码

导入pygamepygame.init()屏幕宽度 = 500屏幕高度 = 400isJump = 假y = 350x = 50蓝色=(0,0,255)运行 = 真screen = pygame.display.set_mode((screen_width, screen_height))screen.fill((0,0,0))pygame.display.set_caption(syoma n9ot 智能")pygame.draw.rect(屏幕,蓝色,(x,y,50,50))运行时:pygame.display.flip()对于 pygame.event.get() 中的事件:键 = pygame.key.get_pressed()如果键[pygame.K_w]:对于范围内 (1,250):y -= .5screen.fill((0,0,0))pygame.draw.rect(屏幕,蓝色,(x,y,50,50))pygame.display.flip()对于范围内 (1,250):y += .5screen.fill((0,0,0))pygame.draw.rect(屏幕,蓝色,(x,y,50,50))pygame.display.flip()如果 event.type == pygame.QUIT:运行 = 错误pygame.quit()

解决方案

使用 KEYDOWN 事件代替 pygame.key.get_pressed().

导入pygamepygame.init()屏幕宽度 = 500屏幕高度 = 400isJump = 假跳跃计数 = 0y = 350x = 50蓝色=(0,0,255)运行 = 真screen = pygame.display.set_mode((screen_width, screen_height))时钟 = pygame.time.Clock()pygame.display.set_caption(syoma n9ot 智能")pygame.draw.rect(屏幕,蓝色,(x,y,50,50))运行时:时钟滴答(100)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_w:isJump = 真跳跃计数 = 20如果是跳转:如果 jumpCount >0:y -= 5elif 跳转计数 <= 0:y += 5跳转计数 -= 1如果跳转计数 == -20:isJump = 假screen.fill((0,0,0))pygame.draw.rect(屏幕,蓝色,(x,y,50,50))pygame.display.flip()

hello i am currently trying to make a jumping game in pygame a lot like the chrome dino game. i have made some simple code to draw a square and make it jump. i will my code dow n bellow. my problem is with the jumping part. whenever i press w wichis the the jump button the square jumps multiple times(usauly 2 times). good people of stack overflow please help a man in need. here is my code

import pygame

pygame.init()

screen_width = 500
screen_height = 400
isJump = False
y = 350
x = 50
BLUE=(0,0,255)
run = True

screen = pygame.display.set_mode((screen_width, screen_height))
screen.fill((0,0,0))
pygame.display.set_caption("syoma n9ot intelent")
pygame.draw.rect(screen,BLUE,(x,y,50,50))

while run:
  pygame.display.flip()
  for event in pygame.event.get():
      keys = pygame.key.get_pressed()
      if keys[pygame.K_w]:
        for a in range (1,250): 
          y -= .5
          screen.fill((0,0,0))
          pygame.draw.rect(screen,BLUE,(x,y,50,50))
          pygame.display.flip()

          
        for a in range (1,250):
          y += .5
          screen.fill((0,0,0))
          pygame.draw.rect(screen,BLUE,(x,y,50,50))
          pygame.display.flip()


          
        
        
 
      if event.type == pygame.QUIT:
       run = False
pygame.quit() 

解决方案

Use the KEYDOWN event instead of pygame.key.get_pressed().

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(100)

runs 100 times per second.

Do not control the game with an extra loop in the application or event loop. Use the application loop. Use the variables isJump and jumpCount to control the jump. Set the variable isJump = True and jumpCount = 20 when w is started pressed. Decrement jumpCount in the application loop and change the y position of the player. Set isJump = False if jumpCount == -20:


Complete example:

import pygame

pygame.init()

screen_width = 500
screen_height = 400
isJump = False
jumpCount = 0
y = 350
x = 50
BLUE=(0,0,255)
run = True

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
pygame.display.set_caption("syoma n9ot intelent")
pygame.draw.rect(screen,BLUE,(x,y,50,50))

while run:
    clock.tick(100)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                isJump = True
                jumpCount = 20

    if isJump:
        if jumpCount > 0:
            y -= 5
        elif jumpCount <= 0:
            y += 5
        jumpCount -= 1
        if jumpCount == -20:
            isJump = False
          
    screen.fill((0,0,0))
    pygame.draw.rect(screen,BLUE,(x,y,50,50))
    pygame.display.flip()    

这篇关于chrome dino 游戏在 pygame 中每次点击跳跃两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆