简体   繁体   English

Python直升机游戏不会更新分数

[英]Python helicopter game does not update score

as part of my Pygame learning course I have to create simple helicopter game, similar to flappy bird. 作为我的Pygame学习课程的一部分,我必须创建简单的直升机游戏,类似于飞扬的鸟类。 It also should have a score count - so everytime you pass gap between blocks with helicopter the score should add 1. Game logic and everything else works fine, except everytime helicopter pases thru the blocks the score is not added, so all the time score shows 0. Below is the code, can anyone please help 它也应该有一个得分计数 - 所以每当你用直升机通过积木之间的差距时,得分应该增加1.游戏逻辑和其他一切正常,除非每次直升机通过积木进行分数都没有添加得分,所以所有时间得分显示0.以下是代码,任何人都可以帮忙

import pygame
import time
from random import randint


black = (0,0,0)
white = (255,255,255)
pygame.init()
surfaceWidth = 1280
surfaceHeight = 720

imageHeight = 30
imageWidth = 60


surface = pygame.display.set_mode((surfaceWidth,surfaceHeight))
pygame.display.set_caption('EVGENY GAME')
#clock = frames per second
clock = pygame.time.Clock()

img = pygame.image.load('Helicopter.jpg')

def score(count):
    font = pygame.font.Font('freesansbold.ttf', 20)
    text = font.render("Score: "+str(count), True, white)
    surface.blit(text, [0,0])



def blocks(x_block, y_block, block_width, block_height, gap):
    pygame.draw.rect(surface, white, [x_block, y_block, block_width, block_height])
    pygame.draw.rect(surface, white, [x_block, y_block+block_height+gap, block_width, surfaceHeight])

def replay_or_quit():
    for event in pygame.event.get([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT]):
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        elif event.type == pygame.KEYDOWN:
            continue

        return event.key
    return None


def makeTextObjs(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

def msgSurface(text):
    smallText = pygame.font.Font('freesansbold.ttf', 20)
    largeText = pygame.font.Font('freesansbold.ttf', 150)

    titleTextSurf, titleTextRect = makeTextObjs(text, largeText)
    titleTextRect.center = surfaceWidth / 2, surfaceHeight / 2
    surface.blit(titleTextSurf, titleTextRect)

    typTextSurf, typTextRect = makeTextObjs('Press any key to continue', smallText)
    typTextRect.center = surfaceWidth / 2, ((surfaceHeight / 2) + 100)
    surface.blit(typTextSurf, typTextRect)

    pygame.display.update()
    time.sleep(1)

    while replay_or_quit() == None:
        clock.tick()

    main()

def gameover():
    msgSurface('GAME OVER')

def helicopter(x, y, image):
    surface.blit(img, (x,y,))

# first constance = game over, while not true we are playing game

def main():
    x = 50
    y = 100
    y_move = 0

    x_block = surfaceWidth
    y_block = 0
    block_width = 100
    block_height = randint(0, surfaceHeight/2)
    gap = imageHeight * 8
    block_move = 3


    current_score = 0

    game_over = False
    while not game_over:#this will keep running until it hits pygame.quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_move = -10
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    y_move = 5
        y += y_move

        surface.fill(black)
        helicopter(x,y,img)

        score(current_score)


        blocks(x_block, y_block, block_width, block_height, gap)
        x_block -= block_move

        if y > surfaceHeight-40 or y < 0:
            gameover()

        if x_block < (-1*block_width):
            x_block = surfaceWidth
            block_height = randint(0, surfaceHeight/2)

        if x + imageWidth > x_block:
            if x < x_block + block_width:
                if y < block_height:
                    if x - imageWidth < block_width + x_block:
                        gameover()
        if x + imageWidth > x_block:
            if y + imageHeight > block_height+gap:
                gameover()
        if x < x_block and x > x_block-block_move:
            current_score += 1



        pygame.display.update()#update, updates specific area on display, flip - updates whole display
        clock.tick(100)#100 frames per second
main()
pygame.quit()    
quit()            enter code here

Your score counter never updates because your score counter condition is never reached. 您的分数计数器永远不会更新,因为从未达到分数计数器条件。 Change 更改

if x < x_block and x > x_block-block_move:

to

if x_block >= x > x_block-block_move:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM