Sign in to follow this  
K1NNY

Going from the menu into the game

Recommended Posts

I need help going from my starting menu to my game. At the moment i would like to prompt the user to hit enter and then go directly into the playable part of the program. However, i cant figure out how to clear what im doing and start up the game part. Heres the code so far (i will add the interactive part at the title menu later when i learn how from you guys):

[source lang="python"]import pygame, random
import randint
import pplrty
import sys
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

#variables
x = randint.x
year = 2011
p = pplrty.p
mousepos = pygame.mouse.get_pos()

#get colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)

#get display stuff
DISPLAY = pygame.display.set_mode((640,480))
pygame.display.set_caption('Program')

#create fonts
fontObj1 = pygame.font.Font('freesansbold.ttf', 16)

#make words
textObj1 = fontObj1.render('Hit Enter', True, RED)
RectObj1 = textObj1.get_rect()
RectObj1.center = (315, 240)

#get images
title = pygame.image.load('title.png')
stitle = pygame.transform.scale(title, (264, 64))

bg = pygame.image.load('bg.png')
sbg = pygame.transform.scale(bg, (640, 480))


#fill the display with good stuff
DISPLAY.blit(sbg, (0, 0))
DISPLAY.blit(stitle, (188, 10))
DISPLAY.blit(textObj1, RectObj1)

#Main Loop
while True:

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
year += 1

if event.type == QUIT:
pygame.quit()
sys.exit()

pygame.display.update()[/source]

keep in mind its still very underdeveloped.

Share this post


Link to post
Share on other sites
You should do some reserach on state machines.

At the most basic level, you could start with something like this:

[code]
def menu():
... whatever you would typically do for the menu
if keyHit(ENTER):
return game

def game():
... your game loop
if (SHOULD_GO_BACK_TO_MENU):
return menu

state = menu

while True:
state_next = state()
if state_next:
state = state_next
[/code]

If you're relatively new to Python, you might want to try my Python 3 video tutorial series: [url="http://www.youtube.com/playlist?list=PLDFB7FFF90EE6F0C1"]link[/url].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this