Skip to content

Instantly share code, notes, and snippets.

@prozacgod
Created November 10, 2015 15:48
Show Gist options
  • Save prozacgod/49591d475bfa13c4b58e to your computer and use it in GitHub Desktop.
Save prozacgod/49591d475bfa13c4b58e to your computer and use it in GitHub Desktop.
import os, sys
import pygame
from pygame.locals import *
# this is for library initialization
pygame.init()
pygame.font.init()
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
def setupScreen():
pygame.display.set_caption('Some random game example')
screen = pygame.display.set_mode((640, 480))
return screen
#pygame.mouse.set_visible(0)
# so there's also an idea of a "resource manager", but we're gonna skip that
# this class's goal is to be a base class inherited by other game state classes
class GameState():
def __init__(self):
# this is a "soft" initializer.. it's um... a bit redundant here, but meh..
self.init()
self.reset()
def reset(self):
pass
def update(self, events):
pass
def render(self, surface):
pass
class MenuItem():
# this is a static class variable, it exists in the python world as... MenuItem.font ;) shorter
# this is decent OOP encapsulation, a GAME would require better resource management but this is fine.
font = pygame.font.Font(None, 36)
def __init__(self, text, pos, colorNormal, colorSelected):
self.pos = pos
self.surfaces = {
True: MenuItem.font.render(text, 1, colorSelected),
False: MenuItem.font.render(text, 1, colorNormal)
}
def render(self, surface, selected):
surface.blit(self.surfaces[selected], self.pos)
def setPos(self, pos): # Not required, but fancy
self.pos = pos
def getSize(self):
return self.surfaces[True].get_size()
class MainMenuState(GameState):
def init(self):
menu = ["Play", "Options", "Quit"]
screenCenter = 640/2
y = 100
self.menuItems = []
# dark, then white for selected
for menuText in menu:
menuItem = MenuItem(menuText, (0,0), (128,128,128),(255,255,255))
self.menuItems += [menuItem]
size = menuItem.getSize()
centerX = screenCenter - (size[0] / 2)
menuY = y
#okay.... now that a menu Item is rendered we can calculate it's pixel cetner and align it with the center of the screen
y = y + size[1] + 20 # height of menuItem + 20px
# lol... derpasarus
menuItem.setPos((centerX, y))
def reset(self):
self.selected = 0
def update(self, events):
# after this state updates, it tells the main loop which state to go to next this would imply looping forever ATM.
# we can look for keyboard events and then act on our model to update it to reflect the current state
# first up, lets look for escape and then return None for our next state...
for event in events:
if event.type == QUIT:
return None
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
return None
if event.key == K_UP:
self.selected = self.selected - 1
if event.key == K_DOWN:
self.selected = self.selected + 1
if event.key == K_RETURN:
if self.selected == 0:
return GameState
if self.selected == 2:
return None
if self.selected < 0:
self.selected = 0
if self.selected > 2:
self.selected = 2
# this is good shit, now we can draw this information on the screen...
return MainMenuState
def render(self, surface):
# we need to erase the screen too
surface.fill((0,0,0)) # black for now
# since we use a # to signify the selected menu item, we need to make that jive with our objects that' aren't numbers...
# the easiest atm is this...
selected = self.menuItems[self.selected]
for item in self.menuItems:
item.render(surface, item == selected)
class GamePlayState(GameState):
def init(self):
pass
def reset(self):
pass
def update(self, events):
pass
def render(self, surface):
pass
def gameStateLoop(surface):
states = {
MainMenuState: MainMenuState(),
GamePlayState: GamePlayState()
}
currentState = MainMenuState
# loop forever
while True:
events = pygame.event.get()
currentState = states[currentState].update(events)
if currentState == None:
return
states[currentState].render(surface)
# after a screen is rendered, we need to display all of its glory...
pygame.display.flip()
def main():
screenSurface = setupScreen()
gameStateLoop(screenSurface)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment