Python Forum
[PyGame] sound effect delay and program laggy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] sound effect delay and program laggy
#16
How is my code 'all over the place'? Its grouped by the screen its associated with! There's the start screen, the main game screen (where most of the code is), then the loss, victory, and playAgain screens. The whole structure of my program is this:

start()
game()
loss()
playAgain()
win()
playAgain()

Of course, bc my functions all call each other, I have to declare them in reverse order, which let's honest doesn't really add much to complexity:

frandxy()
closegame()
playAgain()
loseGame()
winGame()
game()
start()

And even my main game loop follows an obvious pattern:

check to see if the 'close' button is being pressed
check to see which arrow key is being pressed and update the 'move' variable accordingly
change x,y coordinates of the snake based on what 'move' is set to
check to see if the snake's head has collided with food, if so, run frandxy
check for loss and victory conditions
draw the screen, which is sub-divided into drawing the background, drawing the snake, drawing the food

Concerning drawing the screen, I learned by trial and error that the order you have the program draw things determines layering, so everything is ordered from back to front. First the background, and then everything in front of the background. That's why it uses the order it does. As for that, it was fortunate that I drew the food AFTER the snake, because otherwise when it spawned the food in on top of the snake, it would've just looked like it had disappeared until the snake was no longer on top of it. Another lesson learned.

The last line of code, which shortens the list I use to draw the snake's tail, was put there because my program kept slowing down as the game progressed, and rather quickly. I was able to resolve that by actually putting a limit on how long the 'path' list could get. As you see, I limited it to the value of 'length'. This is actually a change I made before I made it so the snake couldn't cross its own path. And yes, even if I didn't eat food the game would still continue to slow down. In fact, what I did in-game made no difference. That's how I figured out it must be the 'path' list, since it continuously grows longer regardless of whether or not the snake grows.

As for my variables, yeah, I declared them a bit randomly. I just added them in as I got to them. Though that hasn't really caused me any problems. Besides, if I really need to I could just add comments to all of them explaining what they do. And of course, group them up in a more logical order (such as the variables for the snake's graphics, which includes x, y, path, length, and snakeSize). Though looking at, I just now noticed that I declare the posx and posy variables (which are the x,y coordinates for the food) in the food's main loop! Oops.

My code isn't that complicated, and I have no problems finding what I need when I do alter it. It just looks complicated to you because you're not used to it. As I pointed out earlier, your OOP code looks like spaghetti to me. Now, I'm sure there is a logic to it, but its not apparent to me. Just because the logic isn't immediately apparent to you doesn't mean its not there.

And yeah, obviously I need to clean up and optimize my code a bit, I'm just saying that I didn't do anything 'wrong' just because I did it differently from how you do it. Besides, I've seen people on youtube arguing AGAINST object-oriented programming.

As for my code, I just had an idea on what I could do fix the delay. As I said, I could move all the screens into a single loop using if-then conditions. If I do that, I could have the program limit the fps to 7 at the start of the 'drawing' section, and then have it set it back when its done. Wonder what that would do? Only one way to find out!

And since I'm going to be overhauling my code, I WILL be saving this as a separate file. I'll probably just call it PySnakeV2.


Messages In This Thread
RE: sound effect delay and program laggy - by xBlackHeartx - Sep-28-2019, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Laggy Game Movement game_slayer_99 12 4,789 Oct-05-2022, 11:34 AM
Last Post: metulburr
Music [PyGame] Chopper wash effect efficiency questions. michael1789 9 4,634 Jan-19-2021, 07:12 PM
Last Post: michael1789
  Appropriately delay this PyGame code kleynah22 2 4,516 Nov-09-2017, 02:00 PM
Last Post: Windspar
  [PyGame] My program is very laggy GamePlanet 6 7,255 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