PyGame:如何每10点增加一个值?

时间:2018-11-08 16:19:07

标签: python pygame

我有一个用pygame制作的相对简单的游戏。这个想法是躲避掉落在你身上的方块(在我的代码中它们被称为“事物”)。我想提高每10个闪避后那些滑子的速度。我已经有一个躲避计数器,可以计算躲避多少。我知道在每个块都被躲避之后,我可以仅用thing_speed += 1来提高速度,但是这使得在躲避大约30个之后基本上不可能。躲避的值是一个计数器,每个块离开屏幕后都会添加到该计数器。

这是我的代码:

import pygame
import time
import random
import base64
import io
import webbrowser

pygame.init()

display_width = 800
display_height = 600

black = (0, 0, 0)
white = (255, 255, 255)
grey = (200, 200, 200)

pepe_width = 70
pepe_height = 70

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('The Best Game')
clock = pygame.time.Clock()

photo = 'R0lGODudrGfXe1i..........nzveVVABFSQBYIgIBAA7'
photo2 = 'R0lGADNVMzNVZ............jNVmTOAZmYAAOBAAA7'

output = io.BytesIO(base64.b64decode(photo))
output2 = io.BytesIO(base64.b64decode(photo2))

pepeImg = pygame.image.load(output)
bgImg = pygame.image.load(output2)


def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, black)
    gameDisplay.blit(text, (10, 10))


def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])


def pepe(x, y):
    gameDisplay.blit(pepeImg, (x, y))


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def message_display(text):
    LargeText = pygame.font.Font('freesansbold.ttf', 70)
    TextSurf, TextRect = text_objects(text, LargeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()


def crash():
    message_display('lel pal u ded')


def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 3.5
    thing_width = 100
    thing_height = 100

    dodged = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0

        x += x_change
        y += y_change
        gameDisplay.blit(bgImg, [0,0])

        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        pepe(x, y)
        things_dodged(dodged)

        if x > display_width - pepe_width or x < 0:
            crash()
        if y > display_height - pepe_width or y < 0:
            crash()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            thing_speed += 1

        if (x < thing_startx + thing_width and x + pepe_width > thing_startx and
            y < thing_starty + thing_height and y + pepe_height > thing_starty):
            crash()

        pygame.display.update()
        clock.tick(120)


game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:3)

我不知道这是您要寻找的东西,但是您可以对速度进行最大或弯曲。假设您做到了,那么阻止速度只能达到一定程度。

     if thing_speed < max_speed: # you would set this somewhere else
         thing_speed += 1

或者您可以使用一条曲线使其弯曲,这样当您越躲越多它们时,它们的阻滞就会稍微快一点。

curve_amount = 0.2 # I just made this number up, you would have to tweek it.
if dodged: # Im assuming you dodge detection code looks like this.
    thing_speed *= 1+curve_amount

或者您可以同时使用两者!

或者如果您想根据数字进行操作,则可以做一个简单的计数器

# counter declared someplace else
if counter == 10:
    thing_speed += 1
    counter = 0