Related
I made a menu start game and quit game I am really confused on how to put my main loop in a function and call that function when I click the button please help! I tried to put my main loop in a def funtion but my screen will appear black and nothing shows up here is an image of my start menu Video
my full code script
this is my game intro
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('BLOODY.ttf',115)
TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
TextRect.center = ((800/2), (800/2))
window.blit(TextSurf, TextRect)
# make the square brighter if collideded with the buttons
mouse = pygame.mouse.get_pos()
if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_green,(150,450,120,50))
else:
pygame.draw.rect(window, green,(150,450,120,50))
if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_red,(550,450,110,50))
else:
pygame.draw.rect(window, red,(550,450,110,50))
# ---------------------------------------------------------------------
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Start Game", smallText)
textRect.center = ( (150+(120/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Quit Game", smallText)
textRect.center = ( (150+(910/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
pygame.display.update()
clock.tick(15)
Create a boolean called main_hover, and set it to false. Set it to true when the mouse is over the play button. If it isn't, set it to false.
In your event for loop, you will want to add this:
if event.type == pygame.MOUSEBUTTONUP:
if main_hover == True:
main_loop()
Do the same, but for the quit button.
Your code should look like this:
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
intro = True
main_hover = False
quit_hover = False
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONUP:
if main_hover == True:
main_loop()
if quit_hover == True:
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('BLOODY.ttf',115)
TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
TextRect.center = ((800/2), (800/2))
window.blit(TextSurf, TextRect)
# make the square brighter if collideded with the buttons
mouse = pygame.mouse.get_pos()
if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_green,(150,450,120,50))
main_hover = True
else:
pygame.draw.rect(window, green,(150,450,120,50))
main_hover = False
if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_red,(550,450,110,50))
quit_hover = True
else:
pygame.draw.rect(window, red,(550,450,110,50))
quit_hover = False
# ---------------------------------------------------------------------
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Start Game", smallText)
textRect.center = ( (150+(120/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
Hope this helps
I'm trying to get an image to stay on screen when my left click is pressed. I've been mostly following the Sentdex videos on youtube for info on how to draw the buttons etc, so if its a bit messy, its because i'm pretty new to pygame! The aim of this game is to be a First Person Shooter, but recently found out that's virtually impossible with python :( So im just carrying on with this project to see where else it could go. Any ideas/suggestions would be much appreciated!
import time
import pygame
from tkinter import *
#import pyautogui
pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()
display_width = 1200
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
hgrey = (77,197,179)
hlight_grey = (255,102,106)
grey = (68,187,169)
light_grey = (247,94,98)
pygame.mixer.music.load('music/privia_the_begining.mp3')
pygame.mixer.music.play(-1)
counter = 0
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Privia 1.0')
clock = pygame.time.Clock()
gameIcon = pygame.image.load('pics/priviaicon.png')
bannerIcon = pygame.image.load('pics/priviabanner.png')
backgroundMenu = pygame.image.load('pics/bgm.png')
backgroundGameType = pygame.image.load('pics/bgmws.png')
pygame.display.set_icon(gameIcon)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def messeage_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
pygame.mixer.stop()
def main_menu():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.blit(backgroundMenu, [0, 0])
gameDisplay.blit(bannerIcon,(300,4))
button("SinglePlayer",430,260,350,100,grey,hgrey,game_type)
button("Quit",507,400,200,50,light_grey,hlight_grey,quitgame)
pygame.display.update()
clock.tick(15)
def game_type():
gameDisplay.blit(backgroundGameType, [0, 0])
print("working")
main_menu()
The backgroundGameType image isn't staying on the screen because the code only displays (blits) it to screen when the mouse is held down within the region defined for the button.
If you want it say on the screen, one way would be to set a flag that is checked every iteration of in the main event processing loop (while intro:) that tracks whether or not to also display it even when the mouse is no longer in that region.
Below is your code modified to do this. Note I have commented out parts of it that have nothing to with the problem — something you should have done before posting your code (or better yet, have removed completely). See How to create a Minimal, Complete, and Verifiable Example.
An new global flag variable has been added named game_mode_selected and initialized to False, and its value is checked each iteration of the main loop. As long as it does not have a "truthy" value, a call is made to display the "SinglePlayer" button and check the state of the mouse. When the mouse is clicked within the region defined for it, and the associated action function, game_type() gets called, the value of this global flag gets changed to "remember" it happened.
Since I assume at that point you no longer wish to have the "SinglePlayer" button displayed, I also added a gameDisplay.fill(black) call to the beginning of the main loop. Without it the button will continue to be visible even though it not longer does anything.
import time
import pygame
from tkinter import *
#import pyautogui
#pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()
display_width = 1200
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
hgrey = (77,197,179)
hlight_grey = (255,102,106)
grey = (68,187,169)
light_grey = (247,94,98)
#pygame.mixer.music.load('music/privia_the_begining.mp3')
#pygame.mixer.music.play(-1)
counter = 0
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Privia 1.0')
clock = pygame.time.Clock()
#gameIcon = pygame.image.load('pics/priviaicon.png')
#bannerIcon = pygame.image.load('pics/priviabanner.png')
backgroundMenu = pygame.image.load('pics/bgm.png')
backgroundGameType = pygame.image.load('pics/bgmws.png')
#pygame.display.set_icon(gameIcon)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def messeage_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if (x+w > mouse[0] > x) and (y + h > mouse[1] > y):
pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
# pygame.mixer.stop()
game_mode_selected = False # New global variable
def main_menu():
global game_mode_selected
game_mode_selected = False
intro = True
while intro:
gameDisplay.fill(black) # Added.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.blit(backgroundMenu, [0, 0])
# gameDisplay.blit(bannerIcon,(300,4))
if not game_mode_selected:
button("SinglePlayer", 430,260, 350,100, grey, hgrey, game_type)
else:
gameDisplay.blit(backgroundGameType, [0, 0])
button("Quit", 507,400, 200,50, light_grey, hlight_grey, quitgame)
pygame.display.update()
clock.tick(15)
def game_type():
global game_mode_selected
game_mode_selected = True # Change global because button was clicked.
gameDisplay.blit(backgroundGameType, [0, 0])
print("working")
main_menu()
#campaign module for blunt wars
import pygame
import time
import sys
pygame.init()
pygame.mixer.music.load("Library\Sound\Light.wav")
size = [500, 500]
width = size[0]
height = size[1]
#colors
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)
bright_red = (255,0,0)
bright_green = (0,255,0)
#end colors
screen = pygame.display.set_mode((size), pygame.RESIZABLE)
pygame.display.set_caption('Blunt Wars - Campaign')
clock = pygame.time.Clock()
Icon = pygame.image.load('Library\Image\Icon.png')
pygame.display.set_icon(Icon)
bg = pygame.image.load('bg.png')
text = pygame.font.SysFont('Arial', 30)
def text_objects(text, font):
textSurface = font.render(text, True, black)
# Create the text rect.
textRect = textSurface.get_rect()
# Return a tuple consisting of the surface and the rect.
return textSurface, textRect
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
screen.blit(textSurf, textRect)
def intro():
intro = True
#screen.fill(white)
#screen.blit(bg, (0,0))
pygame.mixer.music.play(-1)
hel = False
while intro:
screen.blit(bg, (0,0))
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
#quit()
import Blunt_Wars
if event.type == pygame.VIDEORESIZE:
surface = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
#screen.fill(blue)
largeText = pygame.font.SysFont('Castellar',50)
textSurf, textRect = text_objects("Blunt Wars - Campaign", largeText)
textRect.center = ((700),(100))
screen.blit(textSurf, textRect)
button("Start New",650,200,100,50,green,bright_green,game_loop)
button("Load Save",650,300,100,50,green,bright_green,load_save)
button("Help",650,400,100,50,green,bright_green,hel)
button("Settings",650,500,100,50,green,bright_green,settings)
button("Main Menu",650,600,100,50,red,bright_red,leave)
pygame.display.update()
clock.tick(15)
def leave():
pygame.mixer.music.stop()
pygame.quit()
import Blunt_Wars
def settings():
pygame.mixer.music.stop()
print("settings loaded")
time.sleep(4)
pygame.quit()
quit()
largeText = pygame.font.SysFont('Castellar',50)
def hel():
intro = False
help = True
while help:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#import Blunt_Wars
quit()
#pygame.mixer.music.stop()
screen.fill(white)
screen.blit(bg, (0,0))
textSurf, textRect = text_objects("Help Page", largeText)
textRect.center = ((700),(100))
screen.blit(textSurf, textRect)
pygame.display.update()
def load_save():
pygame.mixer.music.stop()
print("save loaded")
time.sleep(2)
pygame.quit()
quit()
def game_loop():
pygame.mixer.music.stop()
print("game loop ran")
time.sleep(10)
pygame.quit()
quit()
This is how I make my button in my game. It works for all buttons other than the one which causes this error:
line 50, in button
action()
TypeError: 'bool' object is not callable
If anyone can help me, that would be great and any improvement on my code would be great as well.
You have a local hel variable (a boolean) in your intro function which gets passed to the button function instead of the global hel function. Just remove this line hel = False, so that Python can find the global hel function and pass it to button.
Take a look at this question: Short Description of the Scoping Rules?
Also, you're blitting the buttons outside the screen. Correct their coordinates.
in the game when you first start it there's the game start menu/intro, it has 2 buttons (Start Game) (Quit)
Now when you start the game and you're actually playing, when you press P (Paused) there're 2 buttons (Continue) (Main Menu) if you click continue it completes the game normally. However when you click main menu it closes the game instead of returning to the intro, same problem is present when you crash with the car you have (Try Again) and (Main Menu) and also if you click Main Menu it closes the game
Any ideas on what might be the problem ?(ignore the comments)
import pygame
import time
import random
pygame.init()
crash_sound = pygame.mixer.Sound("C:\Users\itzrb_000\Documents\Untitled.wav")
pygame.mixer.music.load("C:\Users\itzrb_000\Downloads\Cool_Ride.mp3")
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)
bright_green = (0,255,0)
bright_red = (255,0,0)
car_width = 50
gameDisplay = pygame.display.set_mode((display_width,display_height))#game screen size
pygame.display.set_caption('What A Ride')
clock = pygame.time.Clock()
carImg = pygame.image.load('C:\Users\itzrb_000\Downloads\Car_Green_Front.png')
gameBackground = pygame.image.load('C:\WhatARide_Background.png')
icon = pygame.image.load('C:\Users\itzrb_000\Downloads\Car_Green_Front - Copy.png')
pygame.display.set_icon(icon)
pause = False
car1 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (3).png')
car2 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (2).png')
car3 = pygame.image.load('C:\Users\itzrb_000\Downloads\images.png')
rock = pygame.image.load('C:\Users\itzrb_000\Downloads\Rock.png')
def background():
gameDisplay.blit(gameBackground,(0,0))
'''def cars(ystart):
objects = [car1, car2, car3, rock]
num_of_objects = random.randint(1,4)
for x in range(num_of_objects):
y = random.choice(objects)
objectblit = random.randrange(130, 625) - (y.get_width())
gameDisplay.blit(y,(random.randrange(130, 625)))'''
def score(count):
font = pygame.font.SysFont(None,25)
text = font.render("Score: "+str(count),True, blue)
gameDisplay.blit(text,(0,0))
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def text_objects(text,font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width*0.5),(display_height*0.5))
gameDisplay.blit(TextSurf, TextRect)#blit display object
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(crash_sound)
largeText = pygame.font.Font("freesansbold.ttf", 115)
TextSurf, TextRect = text_objects("You Crashed", largeText)
TextRect.center = ((display_width * 0.5), (display_height * 0.5))
gameDisplay.blit(TextSurf, TextRect) # blit display object
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Try Again!", 300, 400, 200, 50, green, bright_green, game_loop)
button("Main Menu!", 300, 470, 200, 50, red, bright_red, game_intro)
pygame.display.update()
clock.tick(20)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
def unpause():
global pause
pause = False
pygame.mixer.music.unpause()
def paused():
global pause
pause = True
pygame.mixer.music.pause()
largeText = pygame.font.Font("freesansbold.ttf", 115)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width * 0.5), (display_height * 0.5))
gameDisplay.blit(TextSurf, TextRect) # blit display object
while pause == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Continue!", 300, 400, 200, 50, green, bright_green,unpause)
button("Main Menu", 300, 470, 200, 50, red, bright_red,game_intro)
pygame.display.update()
clock.tick(20)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(blue)
largeText = pygame.font.SysFont("comicsansms", 115)
TextSurf, TextRect = text_objects("What A Ride", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
button("Start Game", 300, 400, 200, 50, green, bright_green,game_loop)
button("Quit", 300, 470, 200, 50, red, bright_red,quitgame)
pygame.display.update()
clock.tick(20)
def game_loop():
global pause
pygame.mixer.music.play(-1)
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
thing_startx = random.randrange(130,625)
thing_starty = -600
thing_speed = 5
thing_width = 100
thing_height = 100
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_p:
pause = True
paused()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
print event
x += x_change
background()
things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed
car(x,y)
score(dodged)
if x > 625 - car_width or x < 130:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(130,625)
dodged += 1
thing_speed += 1
thing_width += (dodged*1.2)
if y < thing_starty+thing_height:
if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx+thing_width:
crash()
pygame.display.update()
clock.tick(51) #fps
game_intro()
game_loop()
pygame.quit()
quit()
Because you're not waiting for the mouse button to be released before you trigger your buttons.
When pause() starts, it brings up two buttons.
User moves mouse to Main Menu.
User clicks mouse.
As soon as the mouse button is depressed, game_intro() is called, which puts a Quit button in the same place.
The mouse button is still depressed, so the game quits.
I have a issue with my game at the moment, the game works fine. however when adding my main menu, It doesn't work, it loads it up but when you press play nothing happens (it doesn't go the game it just stays there). I have attached an image of my simple main menu([1]: http://i.stack.imgur.com/YKY9q.jpg) .My code is below thanks:
import pygame, sys, Funk
from tileC import Tile
from object_classes import *
from interaction import interaction
from A_Star import A_Star
import time
pygame.init()
pygame.font.init()
pygame.mixer.init()
pygame.mixer.music.load('audio/zombie_theme.ogg')
pygame.mixer.music.play(-1)
screen = pygame.display.set_mode((704, 448)) # 32, 32
Tile.pre_init(screen)
clock = pygame.time.Clock()
FPS = 30
total_frames = 0
dungeon = pygame.image.load('images/dungeon.jpg')
survivor = Survivor(32 * 2, 32 * 4)
intro = True
# colours
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
orange = (255,127,0)
grey = (50,50,50)
bright_red = (255,0,0)
bright_green = (0,255,0)
bright_orange = (255,215,0)
def text_objects(text, font):
textSurface = font.render(text, True, (white))
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('Creepster-Regular.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((704/2),(448/2))
gameDisplay.blit(TextSurf, TextRect,)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, (ac), (x, y, w, h))
if click[0] == 1 and action != None:
if action == "start":
return
if action == "end":
pygame.quit()
sys.exit()
else:
pygame.draw.rect(screen, (ic), (x, y, w, h))
smallText = pygame.font.SysFont("monospace.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
screen.blit(textSurf, textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(grey)
largeText = pygame.font.SysFont('Creepster-Regular.ttf',45)
TextSurf, TextRect = text_objects("2D Tile Based Zombie Game", largeText)
TextRect.center = ((704/2),(448/2))
screen.blit(TextSurf, TextRect)
button("Play",100,300,100,50,green,bright_green,"start")
button("Controls",300,300,100,50,orange,bright_orange, "how to play")
button("Quit",500,300,100,50,red,bright_red, "end")
pygame.display.update()
clock.tick(15)
#break
game_intro()
while True:
screen.blit(dungeon, (0,0) )
Zombie.spawn(total_frames, FPS)
Zombie.update(screen, survivor)
survivor.movement()
Bullet.super_massive_jumbo_loop(screen)
A_Star(screen, survivor, total_frames, FPS)
interaction(screen, survivor)
survivor.draw(screen)
Funk.text_to_screen(screen, 'Health: {0}'.format(survivor.health), 0,0)
Funk.text_to_screen(screen, 'Score: {0}'.format(survivor.score), 0,15)
Funk.text_to_screen(screen, 'FPS {0}'.format(FPS), 0,30)
pygame.display.flip()
clock.tick(FPS)
total_frames += 1
if survivor.health <= 0:
time.sleep(1)
screen.blit(pygame.image.load('images/dead.jpg'), (0,0))
pygame.display.update()
screen.fill(black)
Funk.text_to_screen(screen, "You scored {0} points!".format(survivor.score),704/2, 448/2, size = 50)
break
time.sleep(4)
Looks like you are following this tutorial....
Your button isn't doing anything when you click Play, it just returns.
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, (ac), (x, y, w, h))
if click[0] == 1 and action != None:
if action == "start":
return ### Fix here
if action == "end":
pygame.quit()
sys.exit()
Though, you really should define a method for your actions.
def start_game():
# TODO: Implement
print("Starting game")
def how_to_play():
# TODO: Implement
print("How to")
def quit_game():
pygame.quit()
sys.exit()
And in the button method (from the tutorial) you only need to call the action.
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
Then you can attach method to your buttons instead of strings.
button("Play",100,300,100,50,green,bright_green,start_game)
button("Controls",300,300,100,50,orange,bright_orange, how_to_play)
button("Quit",500,300,100,50,red,bright_red, quit_game)