Pygame:连续将文本表面模糊到另一个表面上

时间:2017-10-24 08:09:35

标签: python pygame

我创建了一个显示文本的程序:“0 $”,当我们点击它时会增加资金并显示新图像。

如果你不明白看图片:

image

您看到第一个图像“0 $”正确显示,但是当我单击鼠标时,第一个文本仍然存在,并且新文本被绘制在其上。

这是我的代码:

import pygame
from pygame.locals import *
 
#Initialisation de pygame
pygame.init()
taille_fenetre = (960,480)
fenetre = pygame.display.set_mode(taille_fenetre,RESIZABLE)
pygame.display.set_caption("Space Shooter")
 
#COULEUR
BLANC = (255,255,255)
NOIR =  (  0, 0, 0)
 
#Variables
argent = 0
 
#Font
calibri_font = pygame.font.SysFont("Calibri",50)
 
#Chargement image,surface
lumiere = pygame.image.load("lumiere_tournante.png")
fond = pygame.image.load("espace.jpg").convert()
avion = pygame.image.load("avion_anim.png").convert_alpha()
 
#Transfo sprite
lumiere = pygame.transform.scale(lumiere,(500,550))
avion = pygame.transform.scale(avion,(1000,550))
 
#Texte
argent_text_surface = calibri_font.render(str(argent) +" $",True,BLANC)
 
#Rect
argent_text_rect = argent_text_surface.get_rect()
position_lumiere= rotate_lumiere = lumiere.get_rect()
position_avion = avion.get_rect()
 
#Position
argent_text_rect.centerx =750 #en bas à droite
argent_text_rect.centery =425
 
#autre
avion_vitesse = (0,0)
 
#fonction
def move(vitesse, acceleration):
    vx, vy = vitesse
    ax, ay = acceleration
    vitesse = (vx + ax, vy + ay)
    return vitesse
 
 
#Boucle événementielle
continuer = True
while continuer:
    #Limitations des fps
    pygame.time.Clock().tick(90)
    for event in pygame.event.get():   #On parcours la liste de tous les événements reçus
        if event.type == QUIT:
                continuer = False
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT:
                avion_vitesse = move(avion_vitesse, (10,0))
            elif event.key == K_LEFT:
                avion_vitesse = move(avion_vitesse, (-10,0))
            if event.key == K_UP:
                avion_vitesse = move(avion_vitesse, (0,-10))
            elif event.key == K_DOWN:
                avion_vitesse = move(avion_vitesse, (0,10))
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                avion_vitesse = move(avion_vitesse, (-10,0))
            elif event.key == K_UP:
                avion_vitesse = move(avion_vitesse, (0,10))
            elif event.key == K_DOWN:
                avion_vitesse = move(avion_vitesse, (0,-10))
            elif event.key == K_LEFT:
                avion_vitesse = move(avion_vitesse, (10,0))
 
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                argent +=10
                argent_text_surface = calibri_font.render(str(argent) +" $",True,BLANC)
 
 
        i=0
        for loop in range(1):
            rotate_lumiere = pygame.transform.rotate(lumiere, 1+i)
            i+= 1
 
 
 
    #Logique de jeu
    vx, vy = avion_vitesse
    position_avion.left += vx
    position_avion.top += vy
 
 
    #L'affichage du jeu
    fond.blit(argent_text_surface, argent_text_rect)
    fenetre.blit(fond, (0,0))
    fenetre.blit(rotate_lumiere, position_lumiere)
    fenetre.blit(avion, position_avion)
    #Rafraichissement
    pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话,你想把钱砸到钱背景图像/表面上。您必须创建原始背景的副本,然后将新渲染的文本blit到其上,否则您将在同一原始表面上blit越来越多的文本表面。

在这个示例中,我在render_money函数中执行此操作,并返回包含当前货币的副本(money_surf)。然后我将它分配给money_surf函数中的main变量,我在其中进行blit。如果用户点击图像,我会检查鼠标位置是否与money_rect发生碰撞,增加资金并重新渲染货币表面。

import pygame as pg


pg.init()
CALIBRI_FONT = pg.font.SysFont('Calibri', 50)
# The money background surface.
# Replace these two lines with your loaded image.
MONEY_SURF = pg.Surface((150, 70))
MONEY_SURF.fill((0, 60, 120))


def render_money(money):
    """Blit the money onto a copy of the money background image."""
    money_text_surf = CALIBRI_FONT.render(str(money)+' $', True, (230, 250, 250))
    # Make a copy of your background image, so that the original is not modified.
    money_surf = MONEY_SURF.copy()
    money_surf.blit(money_text_surf, (10, 10))
    return money_surf


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()

    money = 0
    money_surf = render_money(money)
    # This rect serves as the blit position and for the collision detection.
    money_rect = money_surf.get_rect(topleft=(100, 200))

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                # Check if the rect collides with the mouse pos (event.pos).
                if money_rect.collidepoint(event.pos):
                    money += 10
                    # Now re-render the money surface.
                    money_surf = render_money(money)

        screen.fill((30, 30, 30))
        screen.blit(money_surf, money_rect)

        pg.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    main()
    pg.quit()