0

I have this project to create a game with a main menu. I have created a very basic main menu at the moment just for prototype purposes. I have my main menu load up and then i want to hit "f" to play game, but it's not working as hoped. When i launch it the g It was fine before I added the functions, but i need these functions. Anybody got any ideas? Thanks.

import pygame, math, sys, random
pygame.init()

#Variables
darkYellow = 204,204,0
grey = 102, 102, 102
black = 0, 0, 0
white = 255, 255, 255
red = 255,0,0
marroon = 120, 0, 0
green = 0,255,0
blue = 0,0,255
darkBlue = 0,0,128

resolution = 650, 600
myFont = pygame.font.SysFont("Times New Roman", 30)
myFont2 = pygame.font.SysFont("Times New Roman", 15)
myFont3 = pygame.font.SysFont("Times New Roman", 60)

redLeft = 150, 575
redRight = 280, 575
blackLeft = 370, 575
blackRight = 500, 575
radius = 20

window = pygame.display.set_mode(resolution)
window.fill(grey)
pygame.display.set_caption('Harrys Game')

def mainMenu():
    rectWidth = 150
    rectHeight = 40

    #Draw buttons
    pygame.draw.rect(window, (white),(250, 150, rectWidth, rectHeight),0)
    pygame.draw.rect(window, (white),(250, 225, rectWidth, rectHeight),0)
    pygame.draw.rect(window, (white),(250, 300, rectWidth, rectHeight),0)
    highlight = pygame.draw.rect(window, (darkYellow),(250, 150, rectWidth, rectHeight),0)
    pygame.display.update()

    playGameText = myFont.render("Play Game", 1, red)
    window.blit(playGameText, (260, 150))
    optionsText = myFont.render("Options", 1, red)
    window.blit(optionsText, (275, 225))
    exitText = myFont.render("Exit", 1, red)
    window.blit(exitText, (300, 300))
    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_f:
                break
            game()

def game():
    while False:
        startBar = pygame.draw.lines(window, black, False, [(0,545.46),(541.7,545.46)], 5)
        bar2 = pygame.draw.lines(window, black, False, [(0,490.92),(541.7,490.92)], 5)
        bar3 = pygame.draw.lines(window, black, False, [(0,436.38),(541.7,436.38)], 5)
        bar4 = pygame.draw.lines(window, black, False, [(0,381.84),(541.7,381.84)], 5)
        bar5 = pygame.draw.lines(window, black, False, [(0,327.3),(541.7,327.3)], 5)
        bar6 = pygame.draw.lines(window, black, False, [(0,272.76),(541.7,272.76)], 5)
        bar7 = pygame.draw.lines(window, black, False, [(0,218.22),(541.7,218.22)], 5)
        bar8 = pygame.draw.lines(window, black, False, [(0,163.68),(541.7,163.68)], 5)
        bar9 = pygame.draw.lines(window, black, False, [(0,109.14),(541.7,109.14)], 5)
        bar10 = pygame.draw.lines(window, black, False, [(0,54.6),(541.7,54.6)], 5)

        #Draw side columns
        rightBar = pygame.draw.lines(window, black, False, [(541.7,600),(541.7,0)], 5)
        leftBar = pygame.draw.lines(window, black, False, [(108.3,600),(108.3,0)], 5)
        centreBar = pygame.draw.lines(window, black, False, [(325,0),(325,600)], 5)

        #Right column text
        label1 = myFont2.render("You rolled a:", 1, black)
        window.blit(label1, (545, 20))
        rollLabel = myFont2.render("Hit space", 1, black)
        window.blit(rollLabel, (545, 100))
        rollLabel2 = myFont2.render("to roll again", 1, black)
        window.blit(rollLabel2, (545, 117))


        #Display column numbers
        start1 = myFont.render("START", 1, black)
        window.blit(start1, (8, 545.46))
        side2 = myFont.render("2", 1, black)
        window.blit(side2, (54.15, 490.92))
        side3 = myFont.render("3", 1, black)
        window.blit(side3, (54.15, 436.38))
        side4 = myFont.render("4", 1, black)
        window.blit(side4, (54.15, 381.84))
        side5 = myFont.render("SAFE 5", 1, black)
        window.blit(side5, (8, 327.3))
        side6 = myFont.render("6", 1, black)
        window.blit(side6, (54.15, 272.76))
        side7 = myFont.render("7", 1, black)
        window.blit(side7, (54.15, 218.22))
        side8 = myFont.render("8", 1, black)
        window.blit(side8, (54.15, 163.68))
        side9 = myFont.render("9", 1, black)
        window.blit(side9, (54.15, 109.14))
        side10 = myFont.render("10", 1, black)
        window.blit(side10, (54.15, 54.6))
        finish11 = myFont.render("FINISH", 1, black)
        window.blit(finish11, (8, 0))
        pygame.display.update()

        redCountLeft = pygame.draw.circle(window, marroon, redLeft, radius)
        redCountRight = pygame.draw.circle(window, marroon, redRight, radius)
        blackCountLeft = pygame.draw.circle(window, black, blackLeft, radius)
        blackCountRight = pygame.draw.circle(window, black, blackRight, radius)

mainMenu()


pygame.display.update()
6
  • "it's not working as hoped" is a very poor problem description. Care to elaborate? Commented Jan 8, 2014 at 20:32
  • I meant that it should just launch and be fine. Thats what i hoped for :/ Commented Jan 8, 2014 at 20:37
  • It doesn't "just launch and be fine"? Thanks for the information. Read this: sscce.org Commented Jan 8, 2014 at 20:44
  • I explained it; It should run a menu, the one i designed and then when i hit f it should launch the "game" function which included the game. Commented Jan 8, 2014 at 20:46
  • And what happens instead?! Error messages, nothing, odd output? What have you tried to do to fix it? Added some print lines to check variables? Tested each function separately in an interpreter? Commented Jan 8, 2014 at 20:48

1 Answer 1

1

A few things:

main_menu() has a while True: loop with no break, raise or return, so would continue looping indefinitely if ever called.

game() contains a while False: "loop" that will never start.

This line makes no sense:

mainMenu() == False

and nor does:

game() == True

For some reason, you define functions (e.g. def counters:) then just call them straight away (e.g. counters()).

When you run your code, this happens:

  1. All the definitions before def main(): happen;
  2. main is defined then immediately called;
  3. mainMenu is defined;
  4. game is defined then immediately called, but doesn't do anything (see above);
  5. counters is defined then immediately called; and
  6. You call pygame.display.update().

That's it. mainMenu is never called (a good thing, as it would uselessly loop forever).

A pseudo code suggestion:

set up variables

set up window 

main menu:
    setup on screen text
    while True:
        get user input 
        if correct user input:
            break
    call game

game:
    while True:
        play game

call main menu
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks, ill get on to it. also do you know how i could call the game function from main menu? as Game is called after the main menu?
To call a function, just do function(). You should generally define all of your functions (def function():) then call the "entry point" (e.g main) at the end of the script.
I'm still confused i moved the calls to the end of the program and now the text just flickers and the program freezes.
I'd guess you're still only looping through the first half of mainMenu, just putting the buttons and text on the screen again and again and again and... Take ten minutes, sit down and draw out what should happen, in what order, where you need to loop, where you need to wait for input. Then write the code.
Yes, but i thought if i had that then when i hit "f" or what ever key i use for enter then it should turn the loop to false and then turn the game to true, so the main menu disappears?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.