pygame examples

Started by
25 comments, last by biggjoee5790 15 years, 3 months ago
eshh really.. Well ok lets say I was going to make a simple menu in a game.. like a GUI that had choices like new game, difficulty, quit, high scores, etc that popped up when the game started and when you pause it. Would I have to draw the polygon to hold the choices and then put the choices inside of it? Is there a pygame class that has methods that act like a button or a radio buttion in Tkinter?
Advertisement
Quote:Original post by biggjoee5790
eshh really.. Well ok lets say I was going to make a simple menu in a game.. like a GUI that had choices like new game, difficulty, quit, high scores, etc that popped up when the game started and when you pause it. Would I have to draw the polygon to hold the choices and then put the choices inside of it?

PyGame is a 2D API, so it doesn't draw polygons per se. It draws rectangles. Or images. You can load a bunch of images for each of your menu options, then draw them in the appropriate positions:
(the following is just a code fragment, not a complete program!)
menuNewGame = image.load(newGameMenuImage)menuDifficulty = image.load(difficultyMenuImage)menuHighScore = image.load(highScoreImage)menuQuit = image.load(quitImage)menu = [menuNewGame, menuDifficulty, menuHighScore, menuQuit]# create menu image positions as a list rectsmenu = zip(menu, rect)selected = 0for evt in event.get():  if evt.type == REDRAW:    for i, (m, r) in enumerate(menu):      if i == selected:        screen.fill(menuHighlightColor, r.inflate(5, 5))      screen.blit(m, r)  elif evt.type == KEYDOWN:    if evt.key == K_UP:      selected -= 1      selected %= len(menu)    elif evt.key == K_DOWN:      selected += 1      selected %= len(menu)    elif evt.key == K_RETURN:      # use selected to determine what event to generate or function to call


Quote:Is there a pygame class that has methods that act like a button or a radio buttion in Tkinter?

No. You'll need to write your own. It's not hard, just tedious.
ahh ok I gotcha now. So I would have to create an area that would act as a button, and have it respond by getting a mouse click event that occurs only within the area the "button" covers? Thats what Im understanding. If thats the case is there a way to have the "button" be raised or lowered? like when you click it have the button go in and come out when you release.. like in Tkinter?
Quote:Original post by biggjoee5790
If thats the case is there a way to have the "button" be raised or lowered? like when you click it have the button go in and come out when you release.. like in Tkinter?

Yes. It's called hit testing, and it's a bunch of inconvenience to implement manually, especially if you want it to be as robust as regular GUI toolkits. You need to receive the mousedown event, check to see if the mousepoint is within the rectangle of any of your buttons and then set its state to depressed - but not fire the click event until the button in subsequently released. That allows you to move your mousepoint out of the button while holding the mouse button down and avoid dispatching the event connected to the button.

(As usual, the following is just a code fragment, not a complete program!)

# we use this to check, quickly, if the mouse was depressed over a button by # storing a reference to the buttonmouseInButton = 0if evt.type == MOUSEBUTTONDOWN:  if evt.button == 1: # left mouse button    # evt.pos is a tuple representing the x,y coordinate of the mouse cursor    # you need to test it against your various buttons    for b in buttons:      r = b.get_rect()      if r.collidepoint(evt.pos):        b.state = BUTTONSTATE_PRESSED        mouseInButton = belif evt.type == MOUSEMOTION:  if mouseInButton:    r = mouseInButton.get_rect()    if r.collidepoint(evt.pos):      mouseInButton.state = BUTTONSTATE_PRESSED    elif:      mouseInButton.state = BUTTONSTATE_RELEASEDelif evt.type == MOUSEBUTTONUP:  if mouseInButton:    r = mouseInButton.get_rect()    # now we test to see if the mouse button is still within the button's area    if r.collidepoint(evt.pos):      b = mouseInButton      mouseInButton = 0      b.dispatch()
thanks so much Oluseyi .. youve really been a huge help. So GUIs in pygame are pretty tedious from what I can tell. Ill have to practice a lot. Im looking to eventually make a small action rpg with an isometric style level. RPGs are full of menus and GUIs.. skill trees, inventories, character profiles, etc.. so I wanted to make sure there was a clear way to implement these things in pygame, thanks alot man. I appreciate it
Quote:Original post by biggjoee5790
thanks so much Oluseyi .. youve really been a huge help.

Glad to help. It's actually also serving as research for my planned series of detailed resources on Python game programming with PyGame and Pyglet. I hope to publish that sometime in 2009, maybe around summer. [smile]

Quote:So GUIs in pygame are pretty tedious from what I can tell.

They're equally tedious in OpenGL, DirectX and XNA. Generally, because games have their own custom UIs, there's little point to providing a GUI toolkit as part of a graphics API. Provide the facilities for people to build their GUI toolkits, instead. One of the more recent developments is support for embedding Flash UIs in games - Scaleform Gfx is the main commercial option, and gameswf is an open source alternative. If you're up to the hacking, you could potentially wrap gameswf for Python to work with PyGame surfaces.
Quote:Original post by Oluseyi
Quote:Original post by biggjoee5790
thanks so much Oluseyi .. youve really been a huge help.

Glad to help. It's actually also serving as research for my planned series of detailed resources on Python game programming with PyGame and Pyglet. I hope to publish that sometime in 2009, maybe around summer. [smile]

Quote:So GUIs in pygame are pretty tedious from what I can tell.

They're equally tedious in OpenGL, DirectX and XNA. Generally, because games have their own custom UIs, there's little point to providing a GUI toolkit as part of a graphics API. Provide the facilities for people to build their GUI toolkits, instead. One of the more recent developments is support for embedding Flash UIs in games - Scaleform Gfx is the main commercial option, and gameswf is an open source alternative. If you're up to the hacking, you could potentially wrap gameswf for Python to work with PyGame surfaces.


sounds interesting I would definetely like to read that when you finish it. Ya I see what you're saying about the GUI's... makes sense. Ill just do it manually Its good practice anyways. Thanks again man

This topic is closed to new replies.

Advertisement