Closed pinkteddybearfluff closed 4 months ago
I suspect this is an issue with the fact that normal rects truncate to integers. At higher fps, dt is so small that 300*dt is probably less than 1, making it essentially adding 0 when you add it to an integer. Try using an frect instead or using a vector for your position, and update the rect from that
It's not an issue with pygame, as oddbookworm said it's the wrong rect usage. Either a FRect or a Vector2 would work. replacing get_rect with get_frect completely fixes the issue
As oddbookworm and Damus666 said , it worked perfectly fine by using frect but it doesn't work with the combination of only get_rect and pygame.math.Vector2() but as get_frect is new I will be using that anyways, so the issue is solved. Thank You
it doesn't work with the combination of only get_rect and pygame.math.Vector2()
No, it works perfectly fine if you use them correctly. Here's your code modified to use a vector for positioning. Note that you should also move the pygame.display.flip()
call to after you do your drawing
import pygame
pygame.init()
running = True
clock = pygame.time.Clock()
player_surf = pygame.Surface((32,32))
player_surf.fill("red")
player_pos = pygame.Vector2(640, 360)
player_rect = player_surf.get_rect(center = player_pos)
screen = pygame.display.set_mode((1280,720))
FPS = 500
while running:
dt = clock.tick(FPS)/1000
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player_pos.y -= 300*dt
if keys[pygame.K_DOWN]:
player_pos.y += 300*dt
if keys[pygame.K_LEFT]:
player_pos.x -= 300*dt
if keys[pygame.K_RIGHT]:
player_pos.x += 300*dt
player_rect.center = player_pos
#pygame.display.flip()
screen.fill(pygame.Color(48,48,48))
screen.blit(player_surf, player_rect)
pygame.display.flip()
pygame.quit()
Environment:
You can get some of this info from the text that pops up in the console when you run a pygame program.
Current behavior:
These issues below only occur at higher FPS( for FPS higher than 334) and used with delta time:
Expected behavior: Smooth movement in all directions
Screenshots
Steps to reproduce:
1.the issue could be reproduced with the test code with any key pressed and at FPS higher than maybe 500 or 1000(334 for me)
Test code