Untitled
unknown
plain_text
2 years ago
5.6 kB
8
Indexable
#####################library files########################
import pygame
import time
import random
################this inistialises the pygame settings##########################
pygame.init()
#set up screen dimensions
display_width = 800
display_height = 600
size = display_width,display_height = 800,600
#colour constants for button backgrounds and screen backgrounds
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (200,0,0)
GREEN = (0,200,0)
YELLOW = (255,255,0)
BRIGHT_RED = (255,0,0)
BRIGHT_GREEN = (0,255,0)
LIGHT_YELLOW = (255,255,51)
AQUA = (0, 128, 128)
GRAY = (190, 190, 190)
#creates the screen area - will need one for each screen when using background images to be done next
# gamedisplay is the screen area where we draw things to from now on
gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock() ### this enables us to set frame rate
#this creates our buttons parameters text, x position, y position, width of button, height of button, colour, hover colour, function to run.
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#check position and draw the rectangle - takes the screen we set up gameDisplay. We will need to amend if using multiple screens
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("arial",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
#this is for text boxes on the screens i.e. titles etc
def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
# main menu screen
def main_menu():
intro = True #### will display until we set to false
while intro:
for event in pygame.event.get():
#checks that we havent hit the x in the window
if event.type == pygame.QUIT:
pygame.quit()
quit()
#set our screen background colour
gameDisplay.fill(GRAY)
# display an image in the background
image = pygame.image.load("Menu.jpg") #loads the image
image = pygame.transform.scale(image, size) # resizes the image to fit the display
gameDisplay.blit(image, (0,0))
#loads the image
#sets the text size and font for title -could be declared globally
largeText = pygame.font.SysFont("tahoma",50)
TextSurf, TextRect = text_objects("MAIN MENU", largeText)
#sets the position of title text - i just changed the display_height divided by for vertical position
TextRect.center = ((display_width/2),(display_height/5))
gameDisplay.blit(TextSurf, TextRect)
#call button function to create the buttons
#these buttons trigger different game states
button("PLAY!",350,250,125,50,GRAY,BRIGHT_GREEN,game_loop)
button("Scoreboard",350,350,125,50,GRAY,BRIGHT_GREEN,score_board)
button("Quit",350,450,125,50,GRAY,BRIGHT_GREEN,quitgame)
pygame.display.update()
clock.tick(15) ### set frame rate to 15
#main game loop - change your main game play loop to a procedure so that it runs from the button.
def game_loop():
intro = False
play = True
scoreboard = False
while play:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(GRAY)
largeText = pygame.font.SysFont("TAHOMA",50)
TextSurf, TextRect = text_objects("GAME PLAYING Here", largeText)
TextRect.center = ((display_width/2),(display_height/3))
gameDisplay.blit(TextSurf, TextRect)
button("Main Menu",150,450,100,50,GRAY,BRIGHT_GREEN,main_menu)
button("Score Board",350,450,125,50,GRAY,BRIGHT_GREEN,score_board)
button("Quit",550,450,100,50,GRAY,BRIGHT_GREEN,quitgame)
pygame.display.update()
clock.tick(15)
#scoreboard -same set up as game_loop whilst we text menu and structure. will be added later
def score_board():
#we may use these variables for a back button.
intro = False
play = False
scoreboard = True
while scoreboard:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(AQUA)
largeText = pygame.font.SysFont("TAHOMA",50)
TextSurf, TextRect = text_objects("Scoreboard Here", largeText)
TextRect.center = ((display_width/2),(display_height/5))
gameDisplay.blit(TextSurf, TextRect)
button("Main Menu",150,450,100,50,GRAY,BRIGHT_GREEN,main_menu)
button("Play",350,450,125,50,GRAY,BRIGHT_GREEN,game_loop)
button("Quit",550,450,100,50,GRAY,BRIGHT_GREEN,quitgame)
pygame.display.update()
clock.tick(15)
def quitgame():
pygame.quit()
quit()
main_menu()
Editor is loading...