python - 向我的答题游戏添加粒子效果

标签 python pygame

我目前正在使用 Pygame 制作一个 Python 点击​​游戏。现在,屏幕中央有一个硬币,您可以点击它。我现在想添加的是,每当有人点击它时,它就会出现在硬币旁边的某个地方,这是一个绿色的“+$10”小图标。这就是我希望每当有人点击硬币时游戏的样子:

这是我的硬币函数的代码:

def button_collide_mouse(element_x, element_y, x_to_remove, y_to_remove):
    mouse_x, mouse_y = pygame.mouse.get_pos()
    if mouse_x > element_x > mouse_x - x_to_remove and \
            mouse_y > element_y > mouse_y - y_to_remove:
        return True

def check_events(coin, settings):
    for event in pygame.event.get():
        # Change button color if mouse is touching it
        if button_collide_mouse(coin.image_x, coin.image_y, 125, 125):
            coin.image = pygame.image.load('pyfiles/images/click_button.png')
            if event.type == pygame.MOUSEBUTTONUP:
                settings.money += settings.income

        else:
            coin.image = pygame.image.load('pyfiles/images/click_button_grey.png')

使用我当前的代码,如何添加这种效果?

最佳答案

参见How to make image stay on screen in pygame? .

使用pygame.time.get_ticks()返回自调用 pygame.init() 以来的毫秒数。当点击硬币时,计算必须删除文本图像之后的时间点。将随机坐标和时间添加到列表的头部:

current_time = pygame.time.get_ticks()
for event in pygame.event.get():
    # [...]

    if event.type == pygame.MOUSEBUTTONDOWN:
       if coin_rect.collidepoint(event.pos):
           pos = ... # random position
           end_time = current_time + 1000 # 1000 milliseconds == 1 scond
           text_pos_and_time.insert(0, (pos, end_time))

在主应用程序循环中绘制文本。当时间到期时从列表尾部删除文本:

for i in range(len(text_pos_and_time)):
   pos, text_end_time = text_pos_and_time[i] 
   if text_end_time > current_time:
       window.blit(text, text.get_rect(center = pos))
   else:
       del text_pos_and_time[i:]
       break

最小示例:

import pygame
import random

pygame.init()
window = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont(None, 40)
clock = pygame.time.Clock()

coin = pygame.Surface((160, 160), pygame.SRCALPHA)
pygame.draw.circle(coin, (255, 255, 0), (80, 80), 80, 10)
pygame.draw.circle(coin, (128, 128, 0), (80, 80), 75)
cointext = pygame.font.SysFont(None, 80).render("10", True, (255, 255, 0))
coin.blit(cointext, cointext.get_rect(center = coin.get_rect().center))
coin_rect = coin.get_rect(center = window.get_rect().center)

text = font.render("+10", True, (0, 255, 0))
text_pos_and_time = []

run = True
while run:
    clock.tick(60)
    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if coin_rect.collidepoint(event.pos):
                pos = pygame.math.Vector2(coin_rect.center) + pygame.math.Vector2(105, 0).rotate(random.randrange(360))
                text_pos_and_time.insert(0, ((round(pos.x), round(pos.y)), current_time + 1000))

    window.fill(0)    
    window.blit(coin, coin_rect)
    for i in range(len(text_pos_and_time)):
        pos, text_end_time = text_pos_and_time[i] 
        if text_end_time > current_time:
            window.blit(text, text.get_rect(center = pos))
        else:
            del text_pos_and_time[i:]
            break
    pygame.display.flip()

pygame.quit()
exit()

关于python - 向我的答题游戏添加粒子效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64793618/

相关文章:

python - 打印列表中的项目数

javascript - 如何仅对列表中选定的行求和

python - (Python)在嵌套的 for 循环中遍历列表?

python - Pygame 面具碰撞

python - Pygame:使用多个 channel 设置绝对音量

python - 如何在 pygame 中创建矩形变量而不绘制它们?

python - 从不同的小部件捕获信号

python - Pandas 按所有功能分组?

python - 有没有一种方法可以更快地渲染点 OpenGL

Python-h2o : How to specifiy column types correctly?