# Should I drop pygame?

## Recommended Posts

Last August I posted a thread asking whether I should focus on Python or Game Maker Language. I chose Python since the advice seemed somewhat mixed. I've been making a game in Pygame but I recently read the post "is my Idea Possible with Pygame". Since I've been struggling just to make a button class with Pygame I felt like maybe I should scrap what little progress I've made in python and just do something else.

One of the reasons I've become wary about Pygame because in the thread it was mentioned that Pygame is best for small short projects. I don't let myself have (too many) grandiose plans but I'd like something I can easily just continue working with indefinitely.

My first thought is that I should code in unity. I don't know C# but I'll have to learn it sooner or later for my Computer Science major and it seems to be a better choice for hobbyist game development than python. I understand C# in unity is different from regular C# but I'm not sure how much.

Overall I guess I'm just overwhelmed by the different options. To sum up, my question is what should I do at this point, stick with Pygame, or learn C# and use unity, or maybe work with some completely different sort of game-making software. If possible I'd like to do something that  will benefit my Computer Science studies. I'll also have to learn Java and a bunch of other languages. Once this semester is over, I won't use python again for my major.

Edited by RidiculousName

##### Share on other sites

If you're making a game with Pygame, then a game with the same complexity as:

pong, snake, asteroids, breakout, tetris, missile command, galaga,  pac-man, ikari warriors, gradius, super mario bros, or zelda

then pygame should be [more than] sufficient.

If you're struggling with making a button class, then know it's not going to be much easier in other languages. Python is one of the easier languages to work with.

So I guess the questions really are:

• what is it exactly you're struggling with,
• what's the button planned to be used for (menu, part of in-game GUI, etc),
• and do you understand what's actually needed to make a button class?

##### Share on other sites

If you're making a game with Pygame, then a game with the same complexity as:

pong, snake, asteroids, breakout, tetris, missile command, galaga,  pac-man, ikari warriors, gradius, super mario bros, or zelda

then pygame should be [more than] sufficient.

I don't doubt it's more than sufficient. I would bet you could do just about any 2D game project in pygame. However it seems like it would be substantially more difficult than programming with game-software specifically designed to help you create games.

If you're struggling with making a button class, then know it's not going to be much easier in other languages. Python is one of the easier languages to work with.

According to the post I mentioned, Pygame is aging significantly. I guess since (I think) it's independently developed, it seems a lot harder to get a grasp of than any of the python libraries that I'm familiar with.

what is it exactly you're struggling with,

Typical programming issues. I make something I think will work. It doesn't, so I fix it. Then I find out something else doesn't work. I hesitate to answer here because I'm afraid if I answer somebody will just give me the answer to the specific problem I'm facing. if I don't struggle for a while, I don't learn. I just don't want to work harder than I have to. I'll edit my post above because I didn't mention this, but I won't be learning python again for my major. At least not for awhile, so I'm free to move to another language. Right now Python is all I know.

what's the button planned to be used for (menu, part of in-game GUI, etc),

I'm making a button "class" specifically because I want the buttons it creates to be used for a bunch of different stuff. All of the examples you mentioned would fit.

and do you understand what's actually needed to make a button class?

Erm, I'm not sure what you mean here. If I understood that, I wouldn't be having problems would I?

Overall it seems like you might think I've only been working on this for a little while, got frustrated and maybe started to give up. That's not what I intended to convey. This post is about planning for the future. Ultimately my game is a hobby I have no plans to abandon. I'm just wondering if it might not be better to switch to a different medium

Edited by RidiculousName

##### Share on other sites

However it seems like it would be substantially more difficult than programming with game-software specifically designed to help you create games.

Switching to Unity or a other engine would make things a hundred times easier, yet it's the worst thing you could do.

Yes Unity will allow you to make buttons easy, load sprites easy and in fact Unity will do all the basics for you. Then what? After you have some basic thing that looks like a game and it gets to the point where you need to make the game, you hit the same wall.

Making object, characters and effects for a game still uses the same basics as making a button. It doesn't matter that the language you use is different, the principals remain the same.

So for you to switch to Unity you will need to make at least a menu with pygame, if you don't you will face the same problem later.

First make the button then create a class for the button.

Making games is all about passing walls, true you can run to Unity and avoid the wall for a while, however sooner or later you will have to get past it.

##### Share on other sites

Stick with PyGame. There is a tremendous amount of value in learning to solve problems in your given environment. There is also a tremendous amount of value in learning how to iteratively prototype and develop in languages like Python that offer a REPL (Read-Evaluate-Print Loop). Thirdly, there is a tremendous amount of value in learning different approaches/techniques embodied by different languages. A language like Python is deeply, trivially introspective, whereas a language like C# requires more elaborate reflection libraries to approximate the same; or Python's structural flexibility and preference for composition over inheritance versus C#'s object orientation orthodoxy.

Over the course of your studies and, should you continue to pursue this professionally, career, you will need to work with and learn many different languages. Completing reasonably complex projects in different languages is a great way to build competence. Finish your current project in PyGame, then reevaluate and see if you want to move to Unity or something else for your next project.

Good luck!

Switching to Unity or a other engine would make things a hundred times easier, yet it's the worst thing you could do.

Yes Unity will allow you to make buttons easy, load sprites easy and in fact Unity will do all the basics for you. Then what? After you have some basic thing that looks like a game and it gets to the point where you need to make the game, you hit the same wall.

Making object, characters and effects for a game still uses the same basics as making a button. It doesn't matter that the language you use is different, the principals remain the same.

So for you to switch to Unity you will need to make at least a menu with pygame, if you don't you will face the same problem later.

First make the button then create a class for the button.

Making games is all about passing walls, true you can run to Unity and avoid the wall for a while, however sooner or later you will have to get past it.

Thank you for these informative posts. I understand what you guys are saying. I probably could easily make a menu in Pygame by following any of numerous instructions on the internet. They didn't really cover what I wanted to do. That said, I'm already extremely close to making buttons work. There's just one more thing to sort out and then I think I'll be golden.

That said, I'm only interested in working on a single project right now. I have various game ideas that I want to work on for awhile instead of just making tiny game after tiny game. That's why I'm worried about the whole "Pygame aging" and "small quick games" things. To clarify, I don't plan on making the next triple-A game. I just want a project that I can work on a little at a time for a long time. This creates a problem with the whole "finish what you start" idea, because I don't really have a finish line.

Also, I the problem I'm having seems to be more of a Pygame syntax thing than something to do with understanding buttons in general. I just need to figure out how to get Pygame events returned from a function or class into the game loop, or find someway around that. If it isn't possible.

##### Share on other sites

Also, I the problem I'm having seems to be more of a Pygame syntax thing than something to do with understanding buttons in general. I just need to figure out how to get Pygame events returned from a function or class into the game loop, or find someway around that. If it isn't possible.

You use return for that. However I don't think you need to for something like this.
Pygame's syntax is derived from python, so you just need to understand how pythons classes and functions work.

[spoiler]
# ******************************
myFunction():
print("Hello world")

# to run a function we call it
myFunction()

# ******************************

def myFunctionWithInput(MyInput):
print(MyInput)

# we call it again, now with a unput
myFunctionWithInput("Hello again world")

#******************************
# a function with return
def myMath(InputA, InputB):

# we print the function and because it returns MyAnswer we print MyAnswer
print(myMath( 1, 3))

# ******************************
# a class
class MyCharacter():

def __init__(self): # this allows us to call self

self.HP = 100

def damageCharacter(self,AmountOfDamage): #add self here so we can use it
self.HP = self.HP - AmountOfDamage

#To use a class we create a instannce
Player = MyCharacter()
print(Player.HP) # We will get a 100 because it's what self.HP is set to

Player.damageCharacter( 20)
print(Player.HP) # We get 80 because the player lost 20


[/spoiler]
So we can see that Pygame's pygame.draw.rect() is just a function that wants inputs.

If you have problems understanding this, I recommend the python tutorial on CodeAcademy. Edited by Scouting Ninja

##### Share on other sites

I posted that thread you're referring to, and I decided to stay with PyGame.

I just want a project that I can work on a little at a time for a long time. This creates a problem with the whole "finish what you start" idea, because I don't really have a finish line.

The game I'm working on is a hobby project, so for me, it's done when it's done :wink:.

I decided to stick with it, because as Oluseyi, it's a chance to learn a new language, and show you can solve a problem regardless of environment. I've never worked with Python, but I do have some experience with C#/Java.

If you interested, another library that was mentioned was Arcade, although other than the official website, tutorials are hard to find, but according to the website, it's improves on certain PyGame functions. I'm not saying don't try Unity, but I'm using PyGame as a beginning/starter project, when I get comfortable, then I can probably move on to Unity.

I also don't think you should use "Game Making" software, unless you want to get an idea of how they work, so you can create you own game making software. To me, this seems like a cheat, and as a programmer, you're a problem solver, regardless on language.

Edited by Nexusfactor

##### Share on other sites

I make something I think will work. It doesn't, so I fix it. Then I find out something else doesn't work.

Don't worry about that; that pretty much sums up programming for everyone ever. Edited by Nypyren

##### Share on other sites

Before you go any further I would like you to look at the files in this repo. I have used Pygame in this Windows utility.

Use the code if you wish to but give me som credit.. :)

https://bitbucket.org/diliupg/file_rename_new

##### Share on other sites

I've been making a game in Pygame but I recently read the post "is my Idea Possible with Pygame". Since I've been struggling just to make a button class with Pygame

This is normal, and in fact happens to everybody until the day they stop programming.

I want to make a game working (it's broken now), and I have only vague ideas how to do that. I have code, but it doesn't work. I have never solved this problem before. Yes my problem is an order of magnitude bigger than yours, but the idea is the same.
You want to achieve something, you are trying, but it's not doing what you want it to do. This is to a large part, programming. You solve stuff you've never done before, and at first it seems possible, then it appears really more complicated than it looked, if you're unlucky, you find even more nasty corners where you don't want to go. How do you cope with that? Mostly, stick with it. This problem is not going to win.
I try to solve it one way. The problem deflects my attempt, it doesn't work. At this moment, I stop, and analyze why my attempt failed. Why can't I make it work, what am I missing? The next question is then "How to avoid this from happening again?". The answer to that is where you learn. That's progress.
Then regroup, and attack again, with this new knowledge. At some point, the problem will run out of new nasty corners that I haven't seen. Then I'll win.

The "how to avoid" question has many possible answers.
Running away to another language or programming system is not the answer. The same thing will happen there again, at some point. For a CS study, I'd say it's not even an option. Your job will be fighting problems, you're eventually supposed to know how to do that. Fire fighters also don't say "euhm, big fire eh? Let's go elsewhere, find a smaller one".
Don't see your program as the sure-fire solution to whatever complicated problem you're trying to solve. It doesn't work that way. You're fighting a problem you have never seen before, let alone that you understand it. Coding a solution is therefore likely to fail. It's like going on a holiday, and when you board the plane, they say, oh, you seem smart, can you fly the plane for us, that would be lovely! They throw you into the cockpit, and lock the door. "Good luck!".
Is flying a plane a solvable problem? Yep, you can see these things flying all around the world. Can you just do it on your first attempt? Not bloody likely.
So how do you get to the destination then? Simple, you experiment. You try a few knobs, read some labels, etc. In some time you manage to taxi around the airport. Eventually, you can fly this thing, given enough attempts.
Now a small problem in real-life is that plane crashes tend to be lethal. Therefore, it's not so likely you will be able to reach your holiday destination, I am sorry to say. In computer systems, not so much. most problems can be tried over and over again, in all possible scenarios. In programming, doing experiments works. While they generally fail to control the problem, they do give you information about the problem in how they fail, and you use that new knowledge in the next experiments. Eventually, you covered all basic problems, and it flies! Oh man, such a sweet moment if the thing you're coding actually works!

One of the reasons I've become wary about Pygame because in the thread it was mentioned that Pygame is best for small short projects. I don't let myself have (too many) grandiose plans but I'd like something I can easily just continue working with indefinitely.

"Easy" and "indefinitely" don't work well together. Bigger problems become exponentially harder as they grow in size. A bicycle is simple to get to your holiday destination (it's not so fast, but definitely simple). A car is much less simple (gears, clutch, gasoline, rules of how to use the road, signs, etc etc), and an airplane, euhm yeah, next to impossible, I'd say.
What you consider "small and short" is probably much less small and short than you think.

To sum up, my question is what should I do at this point, stick with Pygame, or learn C# and use unity, or maybe work with some completely different sort of game-making software.

A good strategy in surviving anything complicated is to reduce scale. Don't try the big thing until you know at least some of the small parts. You don't go out on the high way with a car driving lesson before you know about gears and clutches. Same applies in programming. Start small to build basic knowledge before you tackle the big thing. It's not working? If it is not a simple mistake you know how to correct, it might be still too big. Make it smaller. Failures is learning experience, success is confirmation that you know enough of the problem to control it for the case you tried.

Obviously, scaling down doesn't always work. In that case, invent another experiment, or understand why it doesn't work, and adapt. Be flexible, find that attacking angle that avoids all problems you are aware of, and bring that problem down to its knees.

C# and Unity will shuffle more problems onto your plate, which is in the wrong direction with respect to scaling down. While they appear to make it simpler (and from what I heard they do), but it's mostly in the ground-work. Things like "let's order some bricks and wood now, since you're building a house, surely these will be needed.
They do not decide for you what house to build.

Start small, stick with Python and py-game, they give you enough trouble, you don't need more trouble, you need less.

BTW, Python and C# have a lot of common ideas. Sure, Python looks different than C# (and it will feel different as well), but knowledge in one language can generally at least partly be transferred to another language, in particular your knowledge how to fight problems that aren't very co-operative!

##### Share on other sites

Much good advice above. I'll reiterate the following:

• Python is a good language, worth knowing. Any syntax problems that you're currently having in Python will be matched in number (and more besides) in any other language you use. The best skill you can acquire is the ability to overcome these problems.
• Pygame is indeed limited but that doesn't matter. If you're making a "small short project" then finish it with your current language. You can consider something different for the next project.
• Any good programmer is going to have to learn a handful of languages in order to be productive. There's no such thing as learning the 'wrong' one or being stuck in a dead-end because the skill of learning new ones is one you'll have to cultivate. Luckily, moving between the more popular languages like Java, C#, and C++ is very easy, and Python is not much different to those. You don't need to worry now about languages you might need in 3, 5, 10 years time. It's not like natural languages. You can pick a new one up every couple of months if you like.
• GUI programming is awkward. Finding it tricky to create a button that works in the way you expect is not a sign that you are a bad programmer or that the language or library is broken. There's a lot of complexity in there - rendering, input handling, data structures - that go towards making a functional GUI. Do feel free to ask specific questions about implementation, and if you'd rather not be spoon-fed the answers, you can just say that in your question and ask for hints instead. :)

##### Share on other sites

Wow, I'm a little amazed at all the responses. Okay, if everyone here argues I should stick with Pygame/Python for now, I can be pretty sure that's the right thing to do. I'll redesign my project to something smaller and with a definite finish point. Thanks for the advice everyone. I really appreciate it.

##### Share on other sites

I'll redesign my project to something smaller and with a definite finish point.

That will certainly help, but I think based on your descriptions you would be even better served by finding a good mentor in the language or an online course that includes real people who can help you understand the course material.

Having issues with things like creating a class or returning correct values are at a more fundamental level than writing a game.  Before you can write a game you need to know how to write code in general.  Learn to crawl before learning to walk and all that.

##### Share on other sites

I'll redesign my project to something smaller and with a definite finish point.

That will certainly help, but I think based on your descriptions you would be even better served by finding a good mentor in the language or an online course that includes real people who can help you understand the course material.

Having issues with things like creating a class or returning correct values are at a more fundamental level than writing a game.  Before you can write a game you need to know how to write code in general.  Learn to crawl before learning to walk and all that.

I understand how to create classes and return correct values. I am sure having a mentor would help me, but I'm having trouble with Pygame, not Python in general. My issue is that I want to return a pygame event into the game loop and I don't understand how to do that or how to get around it if it's not possible.

Here is the function I'm trying to use to handle how clicking buttons affects the game:

def butts(isClick, screenList):
"""handles buttons"""
if isClick[0] == True:
rat = pg.mouse.get_pos()

for eItem in screenList:
for ei in eItem:
# if button-X + button Width > mouse-X > button-X
# and button-Y + button Width > mouse-Y > button-Y
if ei[0] + ei[2] > rat[0] > ei[0] \
and ei[1] + ei[3] > rat[1] > ei[1]:
if ei[-1] == "Exit":
return pg.QUIT


My Button class:

import pygame as pg

class Button:
"""
Creates a button
"""
_buttList = []
def __init__(self, x, y, w, h, imgAddr, name):
"""
x and y are ints and
indicate where the top right corner of the button will be

w is an int that
represents how wide the button will be

h is an int that
represents the height of the button

it is used to create a pygame image object

name is a string indicating button function
"""
# inits self._image to pygame image object

#pygame x y coordinates
self._x         = x
self._y         = y

#width and height of button
self._w         = w
self._h         = h

self._buttList = []
self._buttList.append((self._x, self._y, self._w, self._h, self._image, name))
#self._index = 0

def draw(self, surf):
"""
draws a button the window
:param surf:
surf is the pygame surface window the button is drawn to
"""
# draws button to screen
surf.blit(self._image, ((self._x, self._y), (self._w, self._h)))

def __getitem__(self, index):
"""

:param index:
int that finds proper index

"""
#print(index)
#print("button list:",self._buttList)
result = self._buttList[index]
return result



"""
Contains classes to handle the various screens of the game
"""
__author__ = "RidiculousName"
__date__ = "4/22/17"
#imports Button class
from button import Button as b
import pygame as pg
"""
Class that handles the main menu screen
"""
def __init__(self, surf):
"""
surf is a pygame.surface object that
the buttons, images, etc. are blitted to

isClick is a pygame mouse object that
determines whether the mouse has been clicked
"""
# where main menu images are located
self._surf = surf
self._surf.fill((0,0,0))    #fill surface with white

# x value, y value, width, height, image path, surface, button type
self._mmList = [
b(150, 300, 200, 80, self._i + "mmResume.png",
"Resume"),
b(150, 400, 200, 80, self._i + "mmStart.png",
"Start"),
b(150, 500, 200, 80, self._i + "mmLoad.png",
b(150, 600, 200, 80, self._i + "mmCreate.png",
"Create"),
b(150, 700, 200, 80, self._i + "mmExit.png",
"Exit"),
b(600, 100, 400, 160, self._i + "mmTitle.png",
"Title")
]

# blits buttons to screen
for ei in self._mmList:
b.draw(ei,surf)

def getScreenList(self):
return self._mmList



and my main function, where I try to put it all together:

def main():
""" set up the game and run the main game loop """
pg.init()  # prepare pygame module for use
surfW = 1600  # window width
surfH = 900  # window height

# create window
surf = pg.display.set_mode((surfW, surfH))

#set window caption
pg.display.set_caption("Aztlan")

while True:
#opens first screen

#print(type(butts(pg.mouse.get_pressed(), mm.getScreenList())))

butts(pg.mouse.get_pressed(), mm.getScreenList())

ev = pg.event.poll()    # look for any event

if ev.type == pg.QUIT:  # window close button clicked?
break

pg.display.flip()

pg.quit()

if __name__ == "__main__":
main()


It doesn't return an error. I just can't get the exit button to work regardless.

##### Share on other sites

Your bug is in not catching and processing a returned value, but I'd like to suggest a bit cleanup first.

def butts(isClick, screenList):
"""handles buttons"""
if isClick[0] == True:
rat_x, rat_y = pg.mouse.get_pos()

for eItem in screenList:
if eItem._x + eItem._w > rat_x > eItem._x \
and eItem._y + eItem._h > rat_y > eItem._y:
if eItem._name == "Exit":
return "quit"

return "continue"


The 'screenList' is a list buttons, and you can simply access fields from the button. That makes your code a whole lot more readable.

I also replaced your magic "pg.QUIT" value by a piece of text.

In the main function

while True:
....
butts(pg.mouse.get_pressed(), mm.getScreenList())   

You are not getting the returned value from the 'butts' call. The value that you return from the function is just lost thus.

With my 'quit' string return value, the call would be something like

while True:
....
next_action = butts(pg.mouse.get_pressed(), mm.getScreenList())
if next_action == 'quit':
break

Note that values like pygame.QUIT look like magic, but they're not. They are simple numeric values given a name, as you can simply try:

Python 3.5.3 (default, Apr 24 2017, 13:32:13)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
>>> import pygame
>>> pygame.QUIT
12

See, it's just the number 12 in disguise.

It is usually best to make your own ways to return results from the functions. It gives you more freedom in expressing what you exactly want.

##### Share on other sites

Thanks Alberth! I was so sure there would be a way to just return pygame.QUIT.  I don't think I did a great job explaining myself in this thread so thank you for explaining this to me.

##### Share on other sites

I was so sure there would be a way to just return pygame.QUIT
Oh you can definitely return pyame.QUIT, but it's not doing anything by itself; computers are terribly stupid, you have to literally spell everything out for them :)

You're welcome. For the nex time, ask about some debugging techniques, so you can point out the problem more precisely.

##### Share on other sites

A third option is to switch from pygame to PySDL2. Unfortunately the API is quite different, but it's mostly function name changes, the fundamental principles are the same. Pygame/SDL 1.2 are quite old, which does have some advantages I suppose, but working with SDL 1.2 would give me the feeling that I'm working with something that's been replaced, and missing out on useful features, even if the old version is still being supported and maintained.

##### Share on other sites

Well I can tell you not to fret. I have seen professional projects with this same problems. When fixing X I break Y and Z. This is generally caused by one of two things. Lack of RCA (root cause analysis), the programmer is not looking deep enough to find the true issue he is finding the first indiction of an issue and patching it up. The second is aggressive coding, the programmer is attempting to do too much with a class or a method. This creates a lot of dependencies and when it comes time to make changes disaster is the result.

Edited by Kylotan

## Create an account

Register a new account

• 9
• 10
• 12
• 10
• 10