Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyBreakout
#4
global very, good, job
But, in all seriousness, it is great that you are making some working complete programs but there are a lot of things you need to change if you are going to write more complex code.

My guess is you learned pygame from the Invent book (correct me if I'm wrong).
Your main issue is use of globals.  You should consider the global keyword completely off limits until you become more experienced.  The one use case for it where it is acceptable is for declaring a global CONSTANT inside a function.  I do this in pygame for instance when I want to load a graphic that I want to be available globally but can't be loaded until pygame and the display is set up.  Rather than having all this setup code in the global namespace I do it in a function but declare the resource loads global.

But, and this is the important part, they are constants.  After they are created they are never changed.

Also more trivial, but none of those color definitions (or at least the vast majority) are necessary.  Nearly every color you could need (all the ones defined with names in html) are predefined for you so instead of writing (255, 0, 0) you just write pg.Color("red") and achieve much more readable code.

Here is a template for a very simple pygame game:  
https://gist.github.com/Mekire/f67d83087...0fb2939796  
Even the simplest pygame programs should really adhere to that.

With the complexity you will soon be approaching though you are going to need to look into some form of scene manager.
Here is my scene manager template:  
https://github.com/Mekire/pygame-mutisce...with-movie
And for a slightly softer intro to the concept, check here:  
https://www.reddit.com/r/pygame/comments...e_example/

Also for a number of basic pygame examples written in a sane style check here:
https://github.com/Mekire/pygame-samples
Reply


Messages In This Thread
PyBreakout - by TerryRitchie - Apr-24-2017, 04:41 PM
RE: PyBreakout - by RandoomDude - Apr-26-2017, 04:39 PM
RE: PyBreakout - by TerryRitchie - Apr-26-2017, 08:14 PM
RE: PyBreakout - by Mekire - Apr-26-2017, 08:31 PM
RE: PyBreakout - by TerryRitchie - Apr-27-2017, 03:31 AM
RE: PyBreakout - by RandoomDude - Apr-27-2017, 04:32 AM

Forum Jump:

User Panel Messages

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