Untitled
unknown
plain_text
2 years ago
7.8 kB
10
Indexable
#####################library files########################
import pygame
import time
import random
import Levels as L
################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= (1080,600))
#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(start_time,time):
score = 0
if time >=30:
score = int((pygame.time.get_ticks() - start_time) / 245) + 217
score_surf = test_font.render(f'Score: {score}',False, (64,64,64))
score_rect = score_surf.get_rect(center = (200,300))
gameDisplay.blit(score_surf,score_rect)
return(score)
#this displays the time since game has been running
def display_time(start_time):
time = int(((pygame.time.get_ticks() - start_time) / 1000))
timer_surf = test_font.render(f'Time: {time}',False, (64,64,64))
timer_rect = timer_surf.get_rect(center = (500,50))
gameDisplay.blit(timer_surf,timer_rect)
return(time)
#this allows us to make an entity move towards a target
# def running(surface,rectangle,target,speed=2):
# pos = pygame.Vector2(rectangle.center)
# target = pygame.Vector2(target)
# dt = speed
# events = pygame.event.get()
# for event in events:
# if event.type == pygame.QUIT:
# running = False
# quitgame()
# pos = pos.move_towards(target, dt)
# rectangle.center = pos
# gameDisplay.blit(surface,rectangle)
#this is for text boxes on the screens i.e. titles etc
def text_objects(text, font):
textSurface = test_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
print(L.CHECKPOINT_1[0])
#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
start_time = pygame.time.get_ticks()
while play:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(GRAY)
gameDisplay.blit(Map, (0,0))
gameDisplay.blit(t1_surf, t1_rect)
time = display_time(start_time)
current_score = display_score(start_time,time)
#L.running(t1_surf,t1_rect,(L.checkpoint_1[0]),2)
# mouse_pos = pygame.mouse.get_pos()
# if mouse_pos == mouse_pos:
# time.sleep(5)
# print(mouse_pos)
#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(30)
#this stops the game from running
def quitgame():
pygame.quit()
quit()
if __name__ == "__main__":
main_menu()
Editor is loading...