Pygame中闪烁的文字

时间:2017-02-26 17:50:35

标签: python pygame python-3.6

所以我一直在PyGame制作游戏,但我遇到了一个问题。我正在制作一个分数计数器,在游戏循环中,如果发生某些事情,分数会上升并且它会调用字体功能。但是,我认为问题是它只会在分数上升时被调用,所以这只是一帧。那么如何让它坚持每一帧?

    #Importation
import pygame
import time
from random import randint


#Initialization
pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255, 255, 255)
orange = (255, 122, 55)
slow_yellow = (255, 203, 82)
medium_orange = (255, 119, 0)
fast_red = (255, 102, 82)
bk_orange = (255, 228, 196)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Dodger Man')
clock = pygame.time.Clock()

score = 0

#MUSIC
pygame.mixer.music.load('coastin_converted.wav')
pygame.mixer.music.set_volume(.3)
pygame.mixer.music.play(-1)

#Man Sprite
manImg = pygame.image.load('tutorialcharacter.png')
man_width = 64
man_height = 64

#Wall Sprites
wall_LImg = pygame.image.load('wall.png')

def wall_L(wall_LX, wall_LY):
    gameDisplay.blit(wall_LImg, (wall_LX, wall_LY))


wall_RImg = pygame.image.load('wall.png')

def wall_R(wall_RX, wall_RY):
    gameDisplay.blit(wall_RImg, (wall_RX, wall_RY))


#Functions
def man(x,y):
    gameDisplay.blit(manImg,(x,y))

#OBSTACLES
def obs(obsx, obsy, obsw, obsh, obscolor):
    pygame.draw.rect(gameDisplay, obscolor, [obsx, obsy, obsw, obsh])

def obs_2(obsx_2, obsy_2, obsw_2, obsh_2, obscolor_2):
    pygame.draw.rect(gameDisplay, obscolor_2, [obsx_2, obsy_2, obsw_2, obsh_2])

def obs_3(obsx_3, obsy_3, obsw_3, obsh_3, obscolor_3):
    pygame.draw.rect(gameDisplay, obscolor_3, [obsx_3, obsy_3, obsw_3, obsh_3])


##Messages

def font_func(font_size, font_msg, font_color):
    font =  pygame.font.SysFont("freesansbold.tff", font_size )
    text = font.render(font_msg, True, font_color)

##Death
def death():
    pygame.mixer.music.load('death.wav')
    pygame.mixer.music.set_volume(.3)
    pygame.mixer.music.play(1)
    font_func(115, "YOU\'RE DEAD", orange)
    window.blit(font_msg, (display_width / 2), 70)
    x = (display_width * 0.40)
    y = (display_height * 0.8)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            end_game()
        else:
            pass
    time.sleep(.5)
    pygame.mixer.music.load('coastin_converted.wav')
    pygame.mixer.music.set_volume(.3)
    pygame.mixer.music.play(-1)
    game_loop(False)


#Game Loop
def game_loop(gameExit):
    global score

    x = (display_width * 0.40)
    y = (display_height * 0.8)

    wall_LX = 0
    wall_LY = 0

    wall_RX = display_width - 64
    wall_RY = 0

    #Obstacles
    obs_startx = randint(64, display_width - 64)
    obs_starty = -800
    obs_speed = 10
    obs_width = 75
    obs_height = 75

    obs_startx_2 = randint(64, display_width - 64)
    obs_starty_2 = -300
    obs_speed_2 = 12
    obs_width_2 = 75
    obs_height_2 = 75

    obs_startx_3 = randint(64, display_width - 64)
    obs_starty_3 = -1000
    obs_speed_3 = 15
    obs_width_3 = 75
    obs_height_3 = 75

    #Movement variables
    x_change = 0
    y_change = 0

    #Event Loop
    while gameExit != True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
                end_game()

            #Movement
            if event.type == pygame.KEYDOWN:                    
                if event.key == pygame.K_LEFT:
                    x_change = -10
                if event.key == pygame.K_RIGHT:
                    x_change = 10
                if event.key == pygame.K_UP:
                    y_change = -10
                if event.key == pygame.K_DOWN:
                    y_change = 10

            #Non-Movement
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        #Logic
        x += x_change
        y += y_change

        gameDisplay.fill(bk_orange)
        wall_L(wall_LX, wall_LY)
        wall_R(wall_RX, wall_RY)

        ##Blocks
        obs(obs_startx, obs_starty, obs_width, obs_height, slow_yellow)
        obs_starty += obs_speed

        obs_2(obs_startx_2, obs_starty_2, obs_width_2, obs_height_2, medium_orange)
        obs_starty_2 += obs_speed_2

        obs_3(obs_startx_3, obs_starty_3, obs_width_3, obs_height_3, fast_red)
        obs_starty_3 += obs_speed_3

        man(x,y)

        if x >= display_width - man_width or x <= 0:
            death()

        if y >= display_height - man_height or y <= 0:
            y_change = 0

        if obs_starty > display_height:
            obs_starty = 0 - obs_height
            obs_startx = randint(64, display_width - 64)
            score += 1
            font_func(70, ("SCORE:", str(score)), orange)
            gameDisplay.blit(font_msg, (display_width/2), 70)

        if obs_starty > 400:
            if obs_starty_2 > display_height:
                score += 2
                font_func(70, ("SCORE:", str(score)), orange)
                gameDisplay.blit(font_msg, (display_width / 2), 70)
            obs_starty_2 = 0 - obs_height_2
            obs_startx_2 = randint(64, display_width - 64)

        if obs_starty_2 > 500:
            if obs_starty_3 > display_height:
                score += 3
                font_func(70, ("SCORE:", str(score)), orange)
                gameDisplay.blit(font_msg, (display_width / 2), 70)
            obs_starty_3 = 0 - obs_height_3
            obs_startx_3 = randint(64, display_width - 64)

        if y <= obs_starty + obs_height:
            if x > obs_startx and x < obs_startx + obs_width or x + man_width > obs_startx and x + man_width < obs_startx + obs_width:
                if y + man_height < obs_starty:
                    pass
                else:
                    score = 0
                    death()

        if y <= obs_starty_2 + obs_height_2:
            if x > obs_startx_2 and x < obs_startx_2 + obs_width_2 or x + man_width > obs_startx_2 and x + man_width < obs_startx_2 + obs_width_2:
                if y + man_height < obs_starty_2:
                    pass
                else:
                    score = 0
                    death()
        if y <= obs_starty_3 + obs_height_3:
            if x > obs_startx_3 and x < obs_startx_3 + obs_width_3 or x + man_width > obs_startx_3 and x + man_width < obs_startx_3 + obs_width_3:
                if y + man_height < obs_starty_3:
                    pass
                else:
                    score = 0
                    death()

        #Updates
        pygame.display.flip()
        clock.tick(60)

#Terminate program
def end_game():
    pygame.quit()
    quit()
game_loop(False)

1 个答案:

答案 0 :(得分:3)

不要在if块中创建和blit得分font_msg。每帧渲染一次。

Connection to SQLite has been established.
Food table has been established.
Utilities table has been established.
[SQLITE_ERROR] SQL error or missing database (table food has no column named price)

Process finished with exit code 0