GC NEA - Phase 1 Final Code
unknown
python
2 years ago
11 kB
12
Indexable
# Built-in Imports:
# Third-Party Imports:
import pygame
# PyGame asset creation:
# Initialise the PyGame library
pygame.init()
# Defining the screen size
game_window = pygame.display.set_mode((1200, 650))
# Getting the size of the screen
x,y = game_window.get_size()
# Setting the name of the PyGame window.
pygame.display.set_caption("Projectile Modelling Simulator")
# Setting the icon of the projectile simulator.
image = pygame.image.load("projectile.png")
pygame.display.set_icon(image)
# Setting the colour of the start-up menu.
background_color = (255, 220, 196)
game_window.fill(background_color)
# Variable Creation:
# Image generation for buttons
start_button_image = pygame.image.load("start.png").convert_alpha()
menu_button_image = pygame.image.load("menu.png").convert_alpha()
exit_button_image = pygame.image.load("exit.png").convert_alpha()
white_bar_image = pygame.image.load("whitebar.png").convert_alpha()
arrow_image = pygame.image.load("arrow.png").convert_alpha()
# Flags:
opening_interface = True
# Index
# Variables to cycle through the array
display_mode_index = 0
sound_effects_index = 0
achievements_index = 0
vector_index = 0
motion_graphs_index = 0
# Subroutines and Objects:
def text(text, text_color, size, x_position, y_position, font_type = "bahnschrift"):
"""
:param text: The text to be displayed on the screen
:param text_color: The value (typically RGB) of the colour of the text to be displayed.
:param size: The size of the text to be displayed on screen.
:param x_position: The difference (to the right) of the text from the centre of the screen.
:param y_position: The difference (below) of the text from the centre of the screen.
:param font_type: The type of font to be displayed, e.g. arial
"""
# Defining the default font to be modified.
font = pygame.font.SysFont(font_type, size)
# Modifying the default font.
text_render = font.render(text, font, text_color)
# Getting the rectangular co-ordinates of the text.
textrect = text_render.get_rect()
# Establishing the centre of the screen, and modifying it with the inputted values for x and y.
textrect.center = ((x//2 + x_position, y//2 + y_position))
# Placing text onto the screen.
game_window.blit(text_render, textrect)
class button():
def __init__(self, contents, x_position, y_position, x_scale, y_scale):
"""
This object is responsible for creating an interactive button on the screen.
:param contents: The instance that will be turned into the button.
:param x_position: The change in x co-ordinates from the centre of the screen.
:param y_position: The change in y co-ordinates from the centre of the screen.
:param x_scale: The size ratio of the x dimension of contents.
:param y_scale: The size ratio of the y dimension of contents.
"""
# Button dimensions:
height = (contents.get_height())
width = (contents.get_width())
# Scaling the contents:
self.contents = pygame.transform.scale(contents, (int((height*x_scale*4)), int((width*y_scale))))
# Getting the rectangular co-ordinates of the contents.
self.rect = self.contents.get_rect()
self.rect.center = (x//2 + x_position, y//2 + y_position)
# Clicked flag
self.clicked = False
# Delete flag
self.delete = False
def create(self):
"""
This function will create and draw the button on the screen.
"""
# Getting the mouse position
mouse_position = pygame.mouse.get_pos()
# Checking whether the button has been clicked:
if self.rect.collidepoint(mouse_position):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
# Updating state of clicked flag
self.clicked = True
# Creating the button on the screen, moving the button from the centre of the screen.
if self.delete == False:
self.coordinates = (self.rect.x, self.rect.y)
game_window.blit(self.contents, (self.rect.x, self.rect.y))
# Creating the menu button instances
start = button(start_button_image, 0, -150, 3.5, 3)
menu = button(menu_button_image, 0 , 0 , 3.5, 3)
exit = button(exit_button_image, 0, 150, 3.5, 3)
arrow = button(arrow_image, -490, -250, 2 ,7)
# Condition to keep the game running
exit_game = False
# Iteration to keep the game window open.
while exit_game == False:
# Check to see if the opening interface is opened or not.
if opening_interface == True:
game_window.fill(background_color)
menu.clicked = False
start.clicked = False
# Delete flags
menu.delete = False
start.delete = False
exit.delete = False
# Creating the button instances
start.create()
menu.create()
exit.create()
# Condition to check whether the buttons have been pressed.
if menu.clicked == True or start.clicked == True:
opening_interface = False
# Deleting the buttons
start.delete = True
menu.delete = True
exit.delete = True
# Menu screen
if menu.clicked == True:
# Setting the background colour.
game_window.fill(background_color)
# Creating the arrow button
arrow.create()
if arrow.clicked == True:
opening_interface = True
arrow.clicked = False
# Updating the flags
# Creating the text for the menu screen.
text("Settings", (0,0,0), 60, 0, -210)
text("Display Mode", (0,0,0), 25, -120, -120)
text("Sound Effects", (0,0,0), 25, -120, -70)
text("Achievements", (0,0,0), 25, -120, -20)
text("Vector Components", (0,0,0), 25, -120, 30)
text("Motion Graphs", (0,0,0), 25, -120, 80)
# Instantiating the drop-down buttons:
white_bar_display_mode = button(white_bar_image, 140, -120,0.25, 0.15)
white_bar_sound_effects = button(white_bar_image, 140, -70,0.25, 0.15)
white_bar_achievements = button(white_bar_image, 140, -20,0.25, 0.15)
white_bar_vector_components = button(white_bar_image, 140, 30,0.25, 0.15)
white_bar_motion_graphs = button(white_bar_image, 140, 80, 0.25, 0.15)
# Creating the buttons
white_bar_display_mode.create()
white_bar_sound_effects.create()
white_bar_achievements.create()
white_bar_vector_components.create()
white_bar_motion_graphs.create()
# Creating the arrays
display_mode_array = ["Default", "Windowed", "Fullscreen"]
sound_effects_array = ["On", "Off"]
achievement_array = ["Off", "On"]
vector_components_array = ["Off", "On"]
motion_graphs_array = ["Off", "Displacement", "Velocity", "Acceleration"]
# Creating text on the buttons.
text(display_mode_array[display_mode_index], (0,0,0), 20, 140, -120)
text(sound_effects_array[sound_effects_index], (0,0,0), 20, 140, -70)
text(achievement_array[achievements_index], (0,0,0), 20, 140, -20)
text(vector_components_array[vector_index], (0,0,0), 20, 140, 30)
text(motion_graphs_array[motion_graphs_index], (0,0,0), 20, 140, 80)
# When the display mode bar is clicked
if white_bar_display_mode.clicked == True:
# Increment the index
display_mode_index += 1
# If the index is greater than the length of the array, set the index back to 0
if display_mode_index > 2:
display_mode_index = 0
# Re-establishing the button
white_bar_display_mode.create()
# When the sound effects bar is clicked
elif white_bar_sound_effects.clicked == True:
# Increment the index
sound_effects_index += 1
# If the index is greater than the length of the array, set the index back to 0
if sound_effects_index > 1:
sound_effects_index = 0
# Re-establishing the button
white_bar_sound_effects.create()
# When the achievements bar is clicked
elif white_bar_achievements.clicked == True:
# Increment the index
achievements_index += 1
# If the index is greater than the length of the array, set the index back to 0
if achievements_index > 1:
achievements_index = 0
# Re-establishing the button
white_bar_achievements.create()
# When the vector components bar is clicked
elif white_bar_vector_components.clicked == True:
# Increment the index
vector_index += 1
# If the index is greater than the length of the array, set the index back to 0
if vector_index > 1:
vector_index = 0
# Re-establishing the button
white_bar_vector_components.create()
# When the motion graphs components bar is clicked
elif white_bar_motion_graphs.clicked == True:
# Increment the index
motion_graphs_index += 1
# If the index is greater than the length of the array, set the index back to 0
if motion_graphs_index > 3:
motion_graphs_index = 0
# Re-establishing the button
white_bar_motion_graphs.create()
# Start menu:
if start.clicked == True:
# Placeholder to test whether the start has been opened (blue)
game_window.fill((0,0,255))
# Creating the arrow button
arrow.create()
# If the arrow button is clicked
if arrow.clicked == True:
# Update the flags
opening_interface = True
arrow.clicked = False
# If the exit button is clicked, close the simulator.
if exit.clicked == True:
exit_game = True
for event in pygame.event.get():
# If the escape key is clicked, the opening interface opens.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
opening_interface = True
if event.type == pygame.QUIT:
# If a quit is detected, exit and close PyGame.
exit_game = True
# Updating the changes as soon as they are made.
pygame.display.update()
# Exit PyGame
pygame.display.quit()
pygame.quit()
quit()Editor is loading...
Leave a Comment