当我按下跳跃键时,玩家得分增加了很多

时间:2020-11-08 03:23:54

标签: python pygame

因此,我试图做到这一点,以便每次跳跃时,我的玩家分数都会增加一,但是到目前为止,每次跳跃或按住SPACE键时,分数都会增加很多。我尝试以不同的方式编写此代码,但这对我不起作用。 https://gyazo.com/7854a425ac4f66783980492dae774f84

我的代码在玩家每次跳跃时加1得分[不起作用]

if keys[pygame.K_SPACE]:
        score += 1
        text = font.render(" = "+str(score),True,(255,255,255))
        textRect.center = ((150,40))

我的完整代码

import pygame
import random
pygame.init()

window = pygame.display.set_mode((700,500))
pygame.display.set_caption(("Noobs First Game"))

# Player class
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 6
        self.fall = 0
        self.isJump = False
        self.JumpCount = 10
        self.rect = pygame.Rect(x,y,width,height)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

# Platform class
class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


# displaying Color
white = (255,255,255)

# Drawing player
playerman = Player(255,255,40,40,white)

# Drawing Platform
platform1 = Platform(200,300,700,30,white)

# Putting Platform in a list
platforms = [platform1]


# redrawing window
def redrawwindow():
    window.fill((0,0,0))
    
    # bliting a counter the game
    window.blit(text,textRect)
    # showing player on the screen
    playerman.draw()

    # Drawing Platform
    for Platform in platforms:
        Platform.draw()

# The conter and how its going look like
font = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render(" = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((150,40))


fps = 30
clock = pygame.time.Clock()

x = 10
y = 10

x_change = 0
y_change = 0

old_x = x
old_y = y

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()



            


        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                x_change = -7
            if event.key == pygame.K_a:
                x_change = 7

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                x_change = 0

            x += x_change
            if x > 500 - playerman.width or x < 0:
                x = old_x

            
        

    # If keys get pressed
    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y

    if keys[pygame.K_SPACE]:
        score += 1
        text = font.render(" = "+str(score),True,(255,255,255))
        textRect.center = ((150,40))
        


    # Player movment
    if keys[pygame.K_a] and playerman.x > playerman.speed:
        px -= playerman.speed

    if keys[pygame.K_d] and playerman.x < 700 - playerman.width - playerman.speed:
        px += playerman.speed

    if keys[pygame.K_w] and playerman.y > playerman.speed:
        py -= playerman.speed

    if keys[pygame.K_s] and playerman.y < 500 - playerman.height - playerman.speed:
        py += playerman.speed


    platform_rect_list =[p.rect for p in platforms]
    player_rect = playerman.get_rect()
    playerman.rect.topleft = (px,py)


    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px
        
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

            # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
                           
                # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

            # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

        
            
    redrawwindow()
    pygame.display.update()
quit_game

2 个答案:

答案 0 :(得分:1)

由于按住空格键,分数会增加。为了使乐谱正常工作,您需要等待释放空格键。

尝试此更新:

spcdown = False
while run:
    ......

    # If keys get pressed
    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y
    
    if not keys[pygame.K_SPACE]: spcdown = False  # space released
    
    if keys[pygame.K_SPACE]:
        if not spcdown: score += 1  # if space pressed first time
        spcdown = True  # space key is pressed
        text = font.render(" = "+str(score),True,(255,255,255))
        textRect.center = ((150,40))

答案 1 :(得分:0)

使用键盘事件(KEYDOWN ​​KEYUP)进行跳转或生成项目符号之类的操作。

pygame.key.get_pressed()返回带有每个键状态的列表。如果按住某个键,则该键的状态为True,否则为False。使用pygame.key.get_pressed()评估按钮的当前状态并连续移动。

当键的状态更改时,键盘事件(请参阅pygame.event模块)仅发生一次。每次按下一个键,KEYDOWN事件就会发生一次。每次释放键都会发生KEYUP。使用键盘事件可以执行单个操作或逐步移动。

您只想在按 SPACE 时一次增加分数。因此,您应该使用键盘事件KEYDOWN

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()


        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                x_change = -7
            if event.key == pygame.K_a:
                x_change = 7

            if event.key == pygame.K_SPACE:
                score += 1
                text = font.render(" = "+str(score),True,(255,255,255))
                textRect.center = ((150,40))               
 
        
        # [...]
相关问题