0
\$\begingroup\$

I made a sprite (hitmarker) to appear middle of your mouse when you click on a sprite (egg), but the hitmarker sprite appears at the right bottom of your mouse like this:

My Result

however, I wanna make it appear like this:

What i wanna make

I tried screen.blit(hitmarker, (pygame.mouse.get_pos())) but it appears at the right bottom of your mouse.

Source Code:

import pygame, sys, time
from pygame.locals import *
from millify import millify,prettify

pygame.mixer.init()
pygame.init()
music = open("music.txt", "r+")
pygame.mixer.music.load("soundtrack.wav")
x = int(music.read())
WHITE = 255,255,255
font = pygame.font.SysFont(None, 44)
cpsecond = open("clickpersecond.txt", "r+")
cps = int(cpsecond.read())
baltotal = open("totalbal.txt", "r+")
totalbal = int(baltotal.read())
totalbalM = prettify(totalbal, '.')
clock = pygame.time.Clock()
w = 800
h = 600
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption('Tap Simulator')
Loop = True
background = pygame.image.load("Background.jpg")
egg = pygame.image.load("egg.png")
resized_egg = pygame.transform.scale(egg, (282, 352))
text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
pygame.mixer.music.play(-1,0.0)
volume = pygame.image.load("volume.png")
mute = pygame.image.load("mute.png")
resized_volume = pygame.transform.scale(volume, (100, 100))
resized_mute = pygame.transform.scale(mute, (100,100))
icon = pygame.image.load("ico.ico")
hitmarker = pygame.image.load("hitmarker.png")
resized_hitmarker = pygame.transform.scale(hitmarker, (50, 50))

pygame.display.set_icon(icon)

while 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))
                vol_rect = volume.get_rect(topleft = (700,500))
                if vol_rect.collidepoint(event.pos):
                    x += 1
                    if x > 2:
                        x = 1
                if egg_rect.collidepoint(event.pos):
                    totalbal += cps
                    totalbalM = prettify(totalbal, '.')
                    screen.blit(background, (0,0))
                    if x == 1:
                        screen.blit(resized_volume, (700,500))
                    if x == 2:
                        screen.blit(resized_mute,(700,500))
                    screen.blit(text, (235,557))
                    screen.blit(resized_egg, (260,150))
                    screen.blit(resized_hitmarker, (pygame.mouse.get_pos()))
                    pygame.display.flip()
                    text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                    print("Your total clicks are", totalbalM, end="\r")
                    time.sleep(0.07)



    #print(pygame.mouse.get_pos()) #to get mouse pos
    screen.blit(background, (0,0))
    screen.blit(resized_volume, (700,500))
    if x == 0:
        screen.blit(background, (0,0))# [...] draw something different
    elif x == 1:
        pygame.mixer.music.unpause()
        screen.blit(resized_volume, (700,500))
    elif x == 2:
        pygame.mixer.music.pause()
        screen.blit(background, (0,0))
        screen.blit(resized_mute,(700,500))
    screen.blit(text, (235,557))    
    screen.blit(resized_egg, (260,150))
    pygame.display.flip()
    pygame.display.update()
    clock.tick(30)

with open("totalbal.txt", "w") as baltotal:
    baltotal.write(str(totalbal))
baltotal.close

with open("music.txt", "w") as music:
    music.write(str(x))
music.close
pygame.quit()
sys.exit()
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Please include relevant source code in the question body instead of linking to an external site. \$\endgroup\$
    – Tyyppi_77
    May 23 at 19:39
  • \$\begingroup\$ All right, done. \$\endgroup\$
    – Zgn
    May 23 at 19:55
3
\$\begingroup\$

pygame.Surface.blit takes the destination position as the top-left corner of the surface you want to blit. To move the center of the surface to the position, you simply need to offset the position by half of the surface's width. This way the center of the image aligns with the original position.

So in pseudo-code you want to do screen.blit(cursor, pygame.mouse.get_pos() - cursor.get_size() / 2).

\$\endgroup\$
3
  • \$\begingroup\$ I get this error, what do i do? TypeError: unsupported operand type(s) for /: 'tuple' and 'int' \$\endgroup\$
    – Zgn
    May 23 at 19:51
  • \$\begingroup\$ Write the vector math out, like I said the code in the question is pseudo-code. \$\endgroup\$
    – Tyyppi_77
    May 23 at 20:04
  • \$\begingroup\$ i don't know what do you mean by "Write the vector math out" but i did it like this screen.blit(hitmarker, pygame.mouse.get_pos() - hitmarker.get_size() / 2) and it still says the same thing \$\endgroup\$
    – Zgn
    May 23 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.