Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Going from the menu into the game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 K1NNY   Members   -  Reputation: 163

Like
0Likes
Like

Posted 18 September 2012 - 09:27 AM

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, randomimport randintimport pplrtyimport sysfrom pygame.locals import *pygame.init()FPS = 30fpsClock = pygame.time.Clock()#variablesx = randint.xyear = 2011p = pplrty.pmousepos = pygame.mouse.get_pos()#get colorsWHITE = (255, 255, 255)RED = (255, 0, 0)#get display stuffDISPLAY = pygame.display.set_mode((640,480))pygame.display.set_caption('Program')#create fontsfontObj1 = pygame.font.Font('freesansbold.ttf', 16)#make wordstextObj1 = fontObj1.render('Hit Enter', True, RED)RectObj1 = textObj1.get_rect()RectObj1.center = (315, 240)#get imagestitle = 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 stuffDISPLAY.blit(sbg, (0, 0))DISPLAY.blit(stitle, (188, 10))DISPLAY.blit(textObj1, RectObj1)#Main Loopwhile 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.

Sponsor:

#2 Goran Milovanovic   Members   -  Reputation: 878

Like
0Likes
Like

Posted 18 September 2012 - 10:38 AM

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.
Learn the basics with my Python 3 video tutorial series. Looking for a good game engine, and relevant tutorials? Here you go.
Small and simple Python 3.x media library: pslab




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS