为我的点击游戏添加粒子效果 [英] Adding a particle effect to my clicker game

查看:24
本文介绍了为我的点击游戏添加粒子效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 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()如果 mouse_x >element_x >mouse_x - x_to_remove 和 \mouse_y >element_y >mouse_y - y_to_remove:返回真def check_events(硬币,设置):对于 pygame.event.get() 中的事件:# 如果鼠标正在触摸它,则更改按钮颜色如果 button_collide_mouse(coin.image_x, coin.image_y, 125, 125):coin.image = pygame.image.load('pyfiles/images/click_button.png')如果 event.type == pygame.MOUSEBUTTONUP:settings.money += settings.income别的:coin.image = pygame.image.load('pyfiles/images/click_button_grey.png')

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

解决方案

参见

导入pygame随机导入pygame.init()窗口 = pygame.display.set_mode((400, 400))font = pygame.font.SysFont(None, 40)时钟 = pygame.time.Clock()硬币 = 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 = []运行 = 真运行时:时钟滴答(60)current_time = pygame.time.get_ticks()对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果 event.type == pygame.MOUSEBUTTONDOWN:如果 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_rect)对于我在范围内(len(text_pos_and_time)):pos, text_end_time = text_pos_and_time[i]如果 text_end_time >当前时间:window.blit(text, text.get_rect(center = pos))别的:del text_pos_and_time[i:]休息pygame.display.flip()pygame.quit()出口()

I'm currently making a Python clicking game using Pygame. Right now, there is a coin in the center of the screen that you can click. What I want to add now, it a little green "+$10" icon that appears somewhere next to the coin whenever someone clicks it. This is what I want the game to look like whenever someone clicks the coin:

Here is the code of my coin functions:

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')

Using my current code, how can I add that kind of effect?

解决方案

See How to make image stay on screen in pygame?.

Use pygame.time.get_ticks() to return the number of milliseconds since pygame.init() was called. When the coin is clicked, calculate the point in time after that the text image has to be removed. Add random coordinates and the time to the head of a list:

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))

Draw the text(s) in the main application loop. Remove the text when the time has expired from the tail of the list:

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


Minimal example:

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()

这篇关于为我的点击游戏添加粒子效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆