Jump to content
  • Advertisement
Sign in to follow this  
K1NNY

Going from the menu into the game

This topic is 2106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
You should do some reserach on state machines.

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


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


If you're relatively new to Python, you might want to try my Python 3 video tutorial series: link.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!