how to make pygame use float number code example
Example: 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()