【问题标题】:Pygame: Blitting a text surface onto another surface continuouslyPygame:将文本表面连续粘贴到另一个表面上
【发布时间】:2017-10-24 08:09:34
【问题描述】:

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

看不懂的看图:

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

这是我的代码:

    import pygame
    from pygame.locals import *
     
    #Init pygame
    pygame.init()
    window_size = (960,480)
    window = pygame.display.set_mode(window_size,RESIZABLE)
    pygame.display.set_caption("Space Shooter")
     
    #COLOR
    WHITE = (255,255,255)
    BLACK =  (  0, 0, 0)
     
    #Variables
    money = 0
     
    #Font
    calibri_font = pygame.font.SysFont("Calibri",50)
     
    #Load image,surface
    light = pygame.image.load("light.png")
    bg = pygame.image.load("space.jpg").convert()
    plane = pygame.image.load("plane.png").convert_alpha()
     
    #Transfo sprite
    light = pygame.transform.scale(light,(500,550))
    plane = pygame.transform.scale(plane,(1000,550))
     
    #Text
    money_text_surface = calibri_font.render(str(money) +" $",True, WHITE)
     
    #Rect
    money_text_rect = money_text_surface.get_rect()
    position_light= rotate_light = light.get_rect()
    position_plane = plane.get_rect()
     
    #Position
    money_text_rect.centerx =750
    money_text_rect.centery =425
     
    #other
    plane_speed = (0,0)
     
    #functions
    def move(speed, acceleration):
        vx, vy = speed
        ax, ay = acceleration
        speed = (vx + ax, vy + ay)
        return speed
     
     
    #Event loop
    continue = True
    while continue:
        #FPS limit
        pygame.time.Clock().tick(90)
        for event in pygame.event.get():
            if event.type == QUIT:
                    continue = False
            elif event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    plane_s = move(plane_speed, (10,0))
                elif event.key == K_LEFT:
                    plane_speed = move(plane_speed, (-10,0))
                if event.key == K_UP:
                    plane_speed = move(plane_speed, (0,-10))
                elif event.key == K_DOWN:
                    plane_speed = move(plane_speed, (0,10))
            elif event.type == KEYUP:
                if event.key == K_RIGHT:
                    plane_speed = move(plane_speed, (-10,0))
                elif event.key == K_UP:
                    plane_speed = move(plane_speed, (0,10))
                elif event.key == K_DOWN:
                    plane_speed = move(plane_speed, (0,-10))
                elif event.key == K_LEFT:
                    plane_speed = move(plane_speed, (10,0))
     
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    money +=10
                    money_text_surface = calibri_font.render(str(money) +" $",True,WHITE)
     
     
            i=0
            for loop in range(1):
                rotate_light = pygame.transform.rotate(light, 1+i)
                i+= 1
     
     
     
        #Logic
        vx, vy = plane_vitesse
        position_plane.left += vx
        position_plane.top += vy
     
     
        #Display
        bg.blit(money_text_surface, money_text_rect)
        window.blit(fond, (0,0))
        window.blit(rotate_light, position_light)
        window.blit(avion, position_plane)
        pygame.display.flip()

谢谢!

【问题讨论】:

  • 请将您的图片添加到实际帖子中 - 由于内部网络的限制,我们中的一些人无法点击图片链接。
  • 你好,保罗,我很抱歉 :(我需要在网站 x 上获得 10 个声望)
  • 有人吗?请::::
  • 所以你只是想更新表面上的数字?
  • 是的-----------

标签: python pygame


【解决方案1】:

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

在本例中,我在 render_money 函数中执行此操作,并返回带有当前资金的副本 (money_surf)。然后我将它分配给main 函数中的money_surf 变量,并在其中对它进行blit。如果用户点击图片,我检查鼠标pos是否与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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2010-12-10
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    相关资源
    最近更新 更多