0

I wanna make a hitmarker efffect like in the shooter games with pygame.

When you click on the "resized_egg" hitmarker is gonna appear for about 0.2 seconds. Like this:

I wanna make a hitmarker effect like this

I tried it with screen.blit() but i didn't manage it to make it.

# [...]
egg = pygame.image.load("egg.png")
resized_egg = pygame.transform.scale(egg, (282, 352))
pygame.image.load("hitmarker.png")
# [...]

while Loop: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            Loop = False


        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1: # 1 == left click
                egg_rect = resized_egg.get_rect(topleft = (260,150))
                if egg_rect.collidepoint(event.pos):
                    # [...]
#[...]
Zgn
  • 132
  • 7
  • 1
    It would be easier to tell if you provided an MWE. However, here's the idea: you blit the background, then the sprites (eg the egg), and then visual effects (ie the marker), then you flip. If you don't blit in the correct order, the marker won't be visible. – Right leg May 20 '21 at 17:33
  • as you said in your question. When you click it then create new object and add to list of displayed object. And if this object will have variable which you set `time_to_remove = current_time + 0.2 seconds` then in every loop you can compare it with current time and remove it from list when `current_time >= time_to_remove` – furas May 20 '21 at 18:10
  • MWE(Minimal Working Example) is the same as MRE([mre]) which You should read about – Matiiss May 20 '21 at 18:46
  • Related [How can I show explosion image when collision happens?](https://stackoverflow.com/questions/64305426/how-can-i-show-explosion-image-when-collision-happens/64305746#64305746) and [Adding a particle effect to my clicker game](https://stackoverflow.com/questions/64793618/adding-a-particle-effect-to-my-clicker-game/64794954#64794954). – Rabbid76 May 20 '21 at 19:01

1 Answers1

0

You should use list (or pygame.sprite.Group) to keep hitboxes with current_time + 200 ms

And in every loop you should check every hitbox on list if it is time to remove it from list.

And later you should draw all hitboxes from list.


Working example:

At start I create empty list

all_items = []

In loop I get

current_time = pygame.time.get_ticks()

When I click mouse button then I add items to this list with remove_at

        x, y = event.pos

        item = {
            'rect': pygame.Rect(x-10, y-10, 20, 20),
            'remove_at': current_time + 500,  # 500ms = 0.5s
        }

        all_items.append(item)

Later I use for-loop to keep only items which have remove_at > current_time

    keep_items = []
    
    for item in all_items:
        if item['remove_at'] > current_time:
            keep_items.append(item)
        else:
            print('removed:', item)
            
    all_items = keep_items

And later I draw all items from this list:

    for item in all_items:    
        pygame.draw.rect(screen, RED, item['rect'])

Full code:

import pygame

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25 # for more than 220 it has no time to update screen

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED   = (255, 0, 0)

# --- functions --- (lower_case_names)

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

all_items = []

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    current_time = pygame.time.get_ticks()
    
    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False
    
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            item = {
                'rect': pygame.Rect(x-10, y-10, 20, 20),
                'remove_at': current_time + 500,  # 500ms = 0.5s
            }
            all_items.append(item)
            print("add at:", x, y)
            
    # --- changes/moves/updates ---

    # filter objects
    
    keep_items = []
    
    for item in all_items:
        if item['remove_at'] > current_time:
            keep_items.append(item)
        else:
            print('removed:', item)
            
    all_items = keep_items
    
    # --- draws ---

    screen.fill(BLACK)

    for item in all_items:    
        pygame.draw.rect(screen, RED, item['rect'])
    
    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))
    
# --- end ---

pygame.quit()
 
furas
  • 134,197
  • 12
  • 106
  • 148
  • x, y = event.pos AttributeError: 'Event' object has no attribute 'pos' – Zgn May 20 '21 at 19:41
  • what PyGame version do you use? Check `print(pygame.__version__)`. I tested it on `pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)` Did you use it for `MOUSEBUTTONDOWN` or `MOUSEBUTTONUP`? Other events may not have `pos` which is mouse position. – furas May 20 '21 at 19:57
  • do you get this error with my code or with your code? – furas May 20 '21 at 20:01
  • my pygame version is 1.9.6 also i wanna make it to appear on where you click, i tried pygame.mouse.get.position() but the hitmarker appears at the bottom right of the mouse. This is my source code: https://www.paste.tc/raw/test-54083 – Zgn May 20 '21 at 20:03
  • i get the error after adding your code on my code – Zgn May 20 '21 at 20:10
  • how did you add it ? `event.pos` has to be in `if event.type == pygame.MOUSEBUTTONDOWN:`. Besides in your code I see `if egg_rect.collidepoint(event.pos):` inside `if event.type == pygame.MOUSEBUTTONDOWN:` so it should work. It has to be used in corrrect event. Key events don't have `pos`. Maybe simply use `print(event.type)` before `x, y = event.pos` to see for what event you use it. – furas May 20 '21 at 20:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232669/discussion-between-zgn-and-furas). – Zgn May 20 '21 at 20:46