[Pygame] main menu setup

[Pygame] main menu setup

Postby metulburr » Tue May 21, 2013 6:13 am

Wel i figure the goal is obvious. A game menu, where the the text gets altered a little when the mouse is over it to know which one is being selected.

There are a couple weird things going on. 1) the mouse upon the center of the menu list makes them all selected. 2) when text is selected being bold and larger size, it strestches to the right, where i was expecting it to be centered and stretch both to the left and to the right ( and up down too.

The code is pretty bad. It was my first attempt at putting toegther a main menu. I was just going to see other peoples man menu's but then i find those people that program using no classes, and not even fucntions. So I just ignored them and attempted my own thing. Which i guess was not a good idea. I know sysfont is not ideal, but just for testing.


My confusion is probably geared towards centering the menu myself, but i tried
Code: Select all
rect.center = self.screen.get_size().center
to reassign the center of the text surface to the center of the screen. But it centered it on the topleft. Not sure why, so i abandoned it and went this direction

Code: Select all
import pygame

def url_image(url):
    try:
        from urllib2 import urlopen
        from cStringIO import StringIO as inout
    except ImportError:
        from urllib.request import urlopen
        from io import BytesIO as inout
    myurl = urlopen(url)
    return inout(myurl.read())
   
def extract_frames(sheet, row_image_amt, col_image_amt, left_right_first=True):
    '''extract frames from sheet based on number of images of row and col
    left_right_first if True: left to right, top to bottom, where if False: top to bottom, left to right
    will only work with spritesheets subsurface the same size
    '''
    frame_inds = []
    if left_right_first:
        for row in range(col_image_amt):
            for col in range(row_image_amt):
                frame_inds.append((col, row))
    else:
        for row in range(row_image_amt):
            for col in range(col_image_amt):
                frame_inds.append((row, col))
   
    rect = sheet.get_rect()
    cell_w = rect.width / row_image_amt
    cell_h = rect.height / col_image_amt
   
    frames = []
    for cell in frame_inds:
        loc = ((cell_w * cell[0], cell_h * cell[1]), (cell_w, cell_h))
        frames.append(sheet.subsurface(loc))
    return frames
   


class MainMenu:
    def __init__(self, img, screenrect):
        self.screen_rect = screenrect
        self.bg_pos = (0,0)
        self.img = img
        self.rect = img.get_rect()
        self.options = {}
        self.spacer = 30
       
        self.opts = [
            ['1 Player'],
            ['2 Player'],
            ['Level Editor'],
            ['Options'],
            ['Quit'],
        ]
       
        self.set_font()
       
    def set_font(self):
        for option in self.opts:
            font_deselect = pygame.font.SysFont('timesnewroman', 25)
            font_selected = pygame.font.SysFont('timesnewroman', 30)
            font_selected.set_bold(True)
            lbl_deselect = font_deselect.render(option[0], 1, (255,255,255))
            lbl_selected = font_selected.render(option[0], 1, (255,0,0))
           
            option.append(lbl_deselect)
            option.append(lbl_selected)

       
    def update(self, screen):
        screen.blit(self.img, self.bg_pos)

        #
        count = 0
        for opt in self.opts:
            rect = opt[1].get_rect()
            screenrect =  screen.get_rect()
            rect.center = screenrect.center
            options_height = (len(self.opts) * self.spacer) // 2 #get half point of hieght of list options
            if not rect.collidepoint(pygame.mouse.get_pos()):
                fonttype = opt[1]
            else:
                fonttype = opt[2]
            screen.blit(fonttype, (rect.centerx - rect.width // 2, (rect.centery - options_height) + count))
            count += self.spacer

       
class Control:
    def __init__(self):
        pygame.init()
        self.screensize = (800,600)
        self.screen = pygame.display.set_mode(self.screensize)
        self.screenrect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.gamestate = True
       
        url = 'http://www.advoteamcourage.com/wp-content/uploads/2012/12/website-background.jpg'
        img_orig = pygame.image.load(url_image(url)).convert()
        img = pygame.transform.scale(img_orig, self.screensize)
        self.mainmenu = MainMenu(img, self.screenrect)
       
        self.mainloop()
       
    def mainloop(self):
        while self.gamestate:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.gamestate = False
                   
            self.update()
           
    def update(self):
        self.screen.fill((0,0,0))
        self.mainmenu.update(self.screen)
        pygame.display.flip()
       
       
app = Control()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1502
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] main menu setup

Postby Mekire » Tue May 21, 2013 8:24 am

Bit messy but here's a quick write up:
Code: Select all
import pygame
import sys

class MainMenu:
    def __init__(self, img, screenrect):
        self.screen_rect = screenrect
        self.img = img
        self.rect = img.get_rect(topleft = (0,0))

        self.from_bottom = 200
        self.spacer = 35
        self.opts = ['1 Player','2 Player','Level Editor','Options','Quit']
        self.pre_render()

    def pre_render(self):
        font_deselect = pygame.font.SysFont('timesnewroman', 25)
        font_selected = pygame.font.SysFont('timesnewroman', 30)
        font_selected.set_bold(True)

        rendered_msg = {"des":[],"sel":[]}
        for option in self.opts:
            d_rend = font_deselect.render(option, 1, (255,255,255))
            d_rect = d_rend.get_rect()
            s_rend = font_selected.render(option, 1, (255,0,0))
            s_rect = s_rend.get_rect()
            rendered_msg["des"].append((d_rend,d_rect))
            rendered_msg["sel"].append((s_rend,s_rect))
        self.rendered = rendered_msg

    def get_event(self,event):
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            for i,opt in enumerate(self.rendered["des"]):
                if opt[1].collidepoint(pygame.mouse.get_pos()):
                    return i

    def update(self, screen):
        screen.blit(self.img,self.rect)
        for i,opt in enumerate(self.rendered["des"]):
            opt[1].center = (self.screen_rect.centerx,self.from_bottom+i*self.spacer)
            if opt[1].collidepoint(pygame.mouse.get_pos()):
                rend_img,rend_rect = self.rendered["sel"][i]
                rend_rect.center = opt[1].center
                screen.blit(rend_img,rend_rect)
            else:
                screen.blit(opt[0],opt[1])

class Control:
    def __init__(self):
        pygame.init()
        self.screensize = (800,600)
        self.screen = pygame.display.set_mode(self.screensize)
        self.screenrect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.fps = 60
        self.done = False

        img = pygame.Surface(self.screensize).convert()
        img.fill((100,100,100))
        self.gamestate = MainMenu(img, self.screenrect)

        self.mainloop()

    def mainloop(self):
        while not self.done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.done = True
                process = self.gamestate.get_event(event)
                if process != None: #Zero is valid, None is not.
                    print("Selected option number {}.".format(process))
                    break
            self.update()

    def update(self):
        self.screen.fill((0,0,0))
        self.gamestate.update(self.screen)
        pygame.display.flip()
        self.clock.tick(self.fps)

if __name__ == "__main__":
    app = Control()
    pygame.quit();sys.exit()

-Mek
User avatar
Mekire
 
Posts: 1013
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] main menu setup

Postby metulburr » Tue May 21, 2013 2:23 pm

how long have you written games? You are so fluent in game programming. Just can whip up a piece of code to do the intended in no time, lol. When for me I struggle through it, each and every part.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1502
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] main menu setup

Postby Mekire » Tue May 21, 2013 2:43 pm

Been using pygame for about two years. Had a little coding experience before that but not a whole lot. It is more about having approached certain problems before. I have made menus in the past and know generally how I want them to work. You on the other hand are trying to figure it out from scratch (with all the non-class using; namespace flooding examples you could ever wish for at your fingertips). Once you have addressed this the next time you do it you will be far more prepared.

And today, I have been banging my head on my desk trying to implement a nice platformer that can navigate sloped surfaces. This is a problem I haven't solved before so solving it for the first time is... challenging.
http://code.google.com/p/worm-holes/downloads/list
basic_try is main.

-Mek
User avatar
Mekire
 
Posts: 1013
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] main menu setup

Postby metulburr » Tue May 21, 2013 4:33 pm

ah. well thats good. Cant wait to get to that position.

In the menu i was trying to get to this:
http://www.pygame.org/project-slidemenu-1429-.html
but their code is not great at all, so i was trying to do it without swiping some

That platformer seems it handles the slopes pretty well, unless you were looking for something more explicit and not there.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1502
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest