1
\$\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\$
    – user35344
    May 23, 2021 at 19:39
  • \$\begingroup\$ All right, done. \$\endgroup\$
    – Zgn
    May 23, 2021 at 19:55

2 Answers 2

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, 2021 at 19:51
  • \$\begingroup\$ Write the vector math out, like I said the code in the question is pseudo-code. \$\endgroup\$
    – user35344
    May 23, 2021 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, 2021 at 20:39
0
\$\begingroup\$

this is the way that worked for me.

First you need to find de center of the image, i did it like this:

self.mid_height = (self.image.get_width()/2)
self.mid_width = (self.image.get_height()/2)

this way i store the middle value of the height and width of the sprite or image, then, i separete the two values that returns te get_pos() function in two variables:

self.cursor_x, self.cursor_y = pygame.mouse.get_pos()

and substract each value of middle height and middle width to the position values of the cursor:

self.cursor_center_x = (self.cursor_x - self.mid_height)
self.cursor_center_y = (self.cursor_y - self.mid_width)

then i stored again the new values in the final variable:

self.cursor_pos = self.cursor_center_x, self.cursor_center_y

and last i used that final tuple in the blit function:

self.screen.blit(self.image, self.cursor_pos)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .