Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame中闪烁的文本_Python_Pygame_Python 3.6 - Fatal编程技术网

Python Pygame中闪烁的文本

Python Pygame中闪烁的文本,python,pygame,python-3.6,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.

所以我一直在用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)
#导入
导入pygame
导入时间
从随机导入randint
#初始化
pygame.mixer.pre_init(44100,16,24096)
pygame.init()
显示宽度=800
显示高度=600
黑色=(0,0,0)
白色=(255,255,255)
橙色=(255、122、55)
慢黄=(255、203、82)
中橙色=(255、119、0)
快速红色=(255、102、82)
bk_橙色=(255228196)
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('Dodger Man'))
clock=pygame.time.clock()
分数=0
#音乐
pygame.mixer.music.load('coastin_converted.wav'))
pygame.mixer.music.set_音量(.3)
pygame.mixer.music.play(-1)
#人妖
manImg=pygame.image.load('tutorialcharacter.png')
人宽=64
人高=64
#墙精灵
wall\u LImg=pygame.image.load('wall.png')
def wall_L(wall_LX,wall_LY):
blit(wall_LImg,(wall_LX,wall_LY))
wall\u RImg=pygame.image.load('wall.png'))
def wall_R(wall_RX,wall_RY):
游戏显示.blit(墙边缘,(墙接收,墙接收))
#功能
defman(x,y):
游戏显示.blit(manImg,(x,y))
#障碍物
def obs(obsx、obsy、obsw、obsh、obscolor):
pygame.draw.rect(游戏显示,obscolor,[obsx,obsy,obsw,obsh])
def obs_2(obsx_2、obsy_2、obsw_2、obsh_2、obscolor_2):
pygame.draw.rect(游戏显示,obscolour\u2,[obsx\u2,obsy\u2,obsw\u2,obsh\u2])
def obs_3(obsx_3、obsy_3、obsw_3、obsh_3、obscolor_3):
pygame.draw.rect(游戏显示,obscolour\u3[obsx\u3,obsy\u3,obsw\u3,obsh\u3])
##信息
def font_func(字体大小、字体消息、字体颜色):
font=pygame.font.SysFont(“freesansbold.tff”,字体大小)
text=font.render(font\u msg、True、font\u color)
##死亡
def death():
pygame.mixer.music.load('death.wav'))
pygame.mixer.music.set_音量(.3)
pygame.mixer.music.play(1)
font_func(115,“你死了”,橙色)
blit(字体信息,(显示宽度/2),70)
x=(显示宽度*0.40)
y=(显示高度*0.8)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
游戏结束()
其他:
通过
时间。睡眠(.5)
pygame.mixer.music.load('coastin_converted.wav'))
pygame.mixer.music.set_音量(.3)
pygame.mixer.music.play(-1)
博弈循环(错误)
#游戏循环
def game_循环(游戏退出):
全球得分
x=(显示宽度*0.40)
y=(显示高度*0.8)
壁厚x=0
壁厚=0
墙\u RX=显示器\u宽度-64
壁厚=0
#障碍物
obs\u startx=randint(64,显示宽度-64)
obs_starty=-800
obs_速度=10
obs_宽度=75
obs_高度=75
obs\u startx\u 2=randint(64,显示宽度-64)
obs_starty_2=-300
obs_速度_2=12
obs_宽度_2=75
obs_高度_2=75
obs\u startx\u 3=randint(64,显示宽度-64)
obs_starty_3=-1000
obs_速度_3=15
obs_宽度_3=75
obs_高度_3=75
#运动变量
x_变化=0
y_变化=0
#事件循环
当游戏退出时!=正确:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
gameExit=True
游戏结束()
#运动
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-10
如果event.key==pygame.K_RIGHT:
x_变化=10
如果event.key==pygame.K_UP:
y_变化=-10
如果event.key==pygame.K_向下:
y_变化=10
#不移动
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
如果event.key==pygame.K_向上或event.key==pygame.K_向下:
y_变化=0
#逻辑
x+=x_变化
y+=y_变化
游戏显示。填充(黑色橙色)
墙(墙,墙)
墙壁(墙壁RX,墙壁RX)
##块
obs(obs_startx、obs_starty、obs_宽度、obs_高度、慢速_黄色)
obs\U starty+=obs\U速度
obs_2(obs_startx_2、obs_starty_2、obs_宽度_2、obs_高度_2、中橙色)
obs_起点_2+=obs_速度_2
obs_3(obs_startx_3,obs_starty_3,obs_宽度_3,obs_高度_3,快速红色)
obs_起点_3+=obs_速度_3
男子(x,y)
如果x>=显示宽度-人员宽度或x=显示高度-人员高度或y显示高度:
obs_起点=0-obs_高度
obs\u startx=randint(64,显示宽度-64)
分数+=1
字体(70,(“分数:”,str(分数)),橙色)
gameDisplay.blit(字体信息,(显示宽度/2),70)
如果obs_starty>400:
如果obs\U starty\U 2>显示高度:
分数+=2
字体(70,(“分数:”,str(分数)),橙色)
gameDisplay.blit(字体信息,(显示宽度/2),70)
obs_起点_2=0-obs_高度_2
obs\u startx\u 2=randint(64,显示宽度-64)
如果obs_starty_2>500:
如果obs\u starty\u 3>显示高度:
分数+=3
字体(70,(“分数:”,str(分数)),橙色)
gameDisplay.blit(字体信息,(显示宽度/2),70)
obs_起点_3=0-obs_高度_3
obs\u startx\u 3=randint(64,显示宽度-64)
如果y obs_startx和xobs_startx和x+man_宽度# Some global font objects that you can reuse in the while loop.
FONT = pygame.font.SysFont('freesansbold.tff', 32)
FONT_BIG = pygame.font.SysFont('freesansbold.tff', 70)

    while not gameExit:
        # In the drawing phase draw this every frame. 
        font_msg = FONT.render('SCORE: {}'.format(score), True, orange)
        gameDisplay.blit(font_msg, (display_width//2, 70))