how to make pygame use float number

'''Pygame cannot adjust the image/rectangle position with floats,
because it rounds the float to an integer. But with this method it works'''

import pygame
pygame.__init__()

WIDTH = 300
HEIGHT = 300
screen = pygame.display.set_mode((WIDTH, HEIGHT))

#Create Sprite Class
class Sprite(pg.sprite.Sprite):
    def __init__(self , pos_x , pos_y, img_name, size):
        pg.sprite.Sprite.__init__(self)
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.image = pg.transform.scale(pg.image.load(img_name), size).convert()
        self.rect = self.image.get_rect(center=(self.pos_x, self.pos_y))


    def update(self):
        pass

      
#Create Object
sprite_obj = Sprite(x, y,"IMAGE.png",(10,10))

#Create Sprite Group
all_sprites = pg.sprite.Group()

#Add Object To The Group
all_sprites.add(sprite_obj)


#Now Create A Function That Redraws the "sprite_obj" in the new location
z = 0
def update_sprite():
  #First remove the old one
  all_sprites.remove(sprite_obj)
  #Make new one with other/updating coordinates
  z += 1
  sprite_obj = Sprite(z, y,"IMAGE.png",(10,10))
  #Add the new one to the group
  all_sprites.add(sprite_obj)
  

  
#Game Loop
while True:
    clock.tick(120)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    #Update
    all_sprites.draw(screen)
    all_sprites.update()
    
    update_sprite()
    
    pygame.display.flip()

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source