Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Laggy Game Movement
#1
I've noticed that the movement in my Pong game is experiencing a strange amount of lag when I start the game up in Python, despite coding it to run at 60 fps. I converted the game to an .exe file and found that it's still experiencing this same lag. However, I added code to print the fps on the screen and it's telling me that it is running at 60 fps. What's most strange however is if I open the .exe file a second time so that the game is running twice in two different windows, the gameplay is notably smoother. As soon as I close the second window, the gameplay again returns to its normal laggy self. I think this is more of an issue with the computer rather than Python but I don't know enough to diagnose the issue or how I can fix this. I've tested the game on three other computers and none of them have this issue. If someone could help that would be great.
Reply
#2
Most of the time it because you didn't use delta or/and floats . Both these will effect movement. Without code it be hard to tell.
99 percent of computer problems exists between chair and keyboard.
Reply
#3
(Jul-19-2022, 12:10 AM)Windspar Wrote: Most of the time it because you didn't use delta or/and floats . Both these will effect movement. Without code it be hard to tell.

I can understand how faulty code could lead to poor performance, but I tested the game on multiple computers and it ran perfectly fine on them. It also runs fine on mine, but only if I open the application up at least twice, which doesn't make sense to me.
Reply
#4
Bottlenecks in programs can cause odd issues. Some operating systems may have issues while others do not. Exe will carry improper coding issues along with it.

Without code it is impossible to diagnose. People here will need to search through the code to find the bottleneck. If you have multiple files with or without resources use Github to upload so it is easier for us to download everything.
Recommended Tutorials:
Reply
#5
Another reason for poor performance . Your not letting computer idle. If you let it max out cpu. It will cause lag.
clock = pygame.time.Clock()
# in main loop
delta = clock.tick(60) # best if match monitor refresh rate
99 percent of computer problems exists between chair and keyboard.
Reply
#6
(Jul-19-2022, 12:05 PM)metulburr Wrote: Bottlenecks in programs can cause odd issues. Some operating systems may have issues while others do not. Exe will carry improper coding issues along with it.

Without code it is impossible to diagnose. People here will need to search through the code to find the bottleneck. If you have multiple files with or without resources use Github to upload so it is easier for us to download everything.

https://github.com/GameSlayer99/PepePong
Here's the github link. You actually helped me find some issues last year with the same game but I abandoned working in Pygame after that because I got so frustrated with the issue I'm currently facing.
Reply
#7
First rule of game programming. Never use the sleep command.

Delta is use for smooth movement.
metulburr likes this post
99 percent of computer problems exists between chair and keyboard.
Reply
#8
(Jul-19-2022, 11:14 PM)Windspar Wrote: First rule of game programming. Never use the sleep command.

Delta is use for smooth movement.

How would you go about replacing all of my code involving the sleep command? I've never heard of using Delta since I'm so new to Pygame.
Reply
#9
By using pygame.time.get_ticks.
This is how I normal handle it. This handle three different ways. one shot timer, repeat timer, and trip counter timer.
class Ticker:
    def __init__(self, ticks, interval):
        self.tick = ticks + interval
        self.interval = interval
        self.count = 0

    # One Shot Ticker
    def elapsed(self, ticks):
        return ticks >= self.tick

    def reset(self, ticks, interval=None):
        if interval:
            self.interval = interval
            
        self.tick = ticks + self.interval

    # Repeat Ticker
    def tick(self, ticks):
        if ticks >= self.tick:
            self.tick = ticks + self.interval
            return True

        return False

    # Trip Counter. Keep track when interval are low. 
    def tick_count(self, ticks):
        self.count = 0
        while ticks >= self.tick:
            self.tick += self.interval
            self.count += 1

        return bool(self.count)
In main loop. You can do something like this.
ticks = pygame.get_ticks()
ticker = Ticker(ticks, 2000) # 2000 milliseconds equals 2 seconds.

# In Main Loop
ticks = pygame.get_ticks()
if ticker.elasped(ticks):
    ticker.count += 1
    if ticker.count < 2:
        # set next ticker if needed
        ticker.reset(ticks, 3000)

if ticker.count == 0:
    # draw your stuff
elif ticker.count == 1:
    # draw your stuff
99 percent of computer problems exists between chair and keyboard.
Reply
#10
Delta is the time between pygame.display.flip. Since fps can and will vary. This is why you use pygame.time.Clock.
The clock.tick will always return in millisecond. You can simple * 0.001 to return in seconds. Then you take delta * object speed. This will give you the correct amount of movement. No matter what the fps is. Don't forget to store floats. Since Rects are only integers.
clock = pygame.time.Clock()
delta = 0
# in main loop
delta = clock.tick(fps) # This tell how long between flips.
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Particle movement mystery XavierPlatinum 10 2,872 Jul-09-2022, 04:28 AM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,325 Nov-02-2021, 06:56 AM
Last Post: Josselin
  [PyGame] object's movement to left leave shadow on the screen Zhaleh 3 3,082 Aug-02-2020, 09:59 PM
Last Post: nilamo
  [PyGame] sound effect delay and program laggy xBlackHeartx 26 12,591 Oct-09-2019, 11:36 AM
Last Post: metulburr
  Using classes for room movement (text game) Lawr3y 3 6,484 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Problem with coding for movement keys Aresofthesea 3 3,429 Jul-05-2019, 07:05 PM
Last Post: nilamo
  Pygame Movement X/Y coord TheHumbleIdiot 2 3,475 Mar-19-2019, 02:21 PM
Last Post: TheHumbleIdiot
  Movement after KEYUP, only after pause func esteel 2 3,249 Aug-22-2018, 03:03 PM
Last Post: Windspar
  [PyGame] How to stop the sprite's movement when it touches the edges of the screen? mrmn 5 11,447 May-13-2018, 06:33 PM
Last Post: mrmn
  [PyGame] My program is very laggy GamePlanet 6 6,987 Aug-15-2017, 03:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020