Untitled
unknown
plain_text
a year ago
7.3 kB
1
Indexable
Never
#####################library files######################## import pygame import time import random ################this inistialises the pygame settings########################## pygame.init() #set up screen dimensions display_width = 1080 display_height = 600 #setup the defaul time start_time = 0 size = display_width,display_height = 1080,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 ##images Menu = pygame.image.load("CS Images/Main Menu/new Menu.png").convert_alpha() #Menu image Menu = pygame.transform.scale(Menu, size) # scale images to fit the games size Map = pygame.image.load("CS Images\Map\Map.png").convert_alpha() Map = pygame.transform.scale(Map, size) play_button = pygame.image.load("CS Images/Main Menu/Buttons/play button.png").convert_alpha() play_button_big = pygame.transform.scale_by(play_button, 2.4) scoreboard_button = pygame.image.load("CS Images/Main Menu/Buttons/scoreboard button.png").convert_alpha() scoreboard_button_big = pygame.transform.scale_by(scoreboard_button, 2.4) quit_button = pygame.image.load("CS Images/Main Menu/Buttons/quit button.png").convert_alpha() quit_button_big = pygame.transform.scale_by(quit_button, 2.4) test_font = pygame.font.Font("Font\Pixeltype.ttf",50) # imports the font i want to use #making entities t1_surf = pygame.image.load("CS Images/Entities/t1/t1.png").convert_alpha() t1_surf = pygame.transform.scale_by(t1_surf, 0.4) t1_rect = t1_surf.get_rect(center= (1000,500)) #this creates our buttons parameters img,x,y, function to run. def button(img,x,y,action): mouse_pos = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() # Create a rectangle from the image and set it to the correct position: button_rect = img.get_rect(center = (x,y)) # Check if a point (mouse_poss) is inside a rectangle (button_rect): if button_rect.collidepoint(mouse_pos): if click[0] == 1 and action != None: action() gameDisplay.blit(img, button_rect) def display_score(): current_time = pygame.time.get_ticks() - start_time score_surf = test_font.render(f'{current_time}',False, (64,64,64)) score_rect = score_surf.get_rect(center = (400,50)) gameDisplay.blit(score_surf,score_rect) def display_time(): time_started = int((pygame.time.get_ticks() - start_time) / 1000) score_surf = test_font.render(f'Time: {time_started}',False, (64,64,64)) score_rect = score_surf.get_rect(center = (500,50)) gameDisplay.blit(score_surf,score_rect) #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 gameDisplay.blit(Menu, (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),(170)) gameDisplay.blit(TextSurf, TextRect) #call button function to create the buttons #these buttons trigger different game states button(play_button_big,(display_width//2),270,game_loop) button(scoreboard_button_big,(display_width//2),360,score_board) button(quit_button_big,(display_width//2),450,quitgame) pygame.display.update() clock.tick(30) ### set frame rate to 30 #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() start_time = pygame.time.get_ticks() gameDisplay.fill(GRAY) gameDisplay.blit(Map, (0,0)) gameDisplay.blit(t1_surf, t1_rect) display_time() # mouse_pos = pygame.mouse.get_pos() # if mouse_pos == mouse_pos: # time.sleep(5) # print(mouse_pos) #v = 2 #t1_rect.x -= 2 #pygame.Vector2.move_towards_ip((950, 514),v) # if t1_rect.right <= 0: t1_rect.left = 800 gameDisplay.blit(t1_surf,t1_rect) #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(play_button_big,150,450,game_loop) #button(scoreboard_button_big,350,450,score_board) #button(quit_button_big,550,450,quitgame) pygame.display.update() clock.tick(30) #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(play_button_big,150,450,game_loop) button(scoreboard_button_big,350,450,score_board) button(quit_button_big,550,450,quitgame) pygame.display.update() clock.tick(15) def quitgame(): pygame.quit() quit() main_menu()