generated from dvorakmortonxd/pygame-template
70 lines
No EOL
5.2 KiB
Python
70 lines
No EOL
5.2 KiB
Python
#!/usr/bin/env python3
|
|
import pygame, levels # Imports the pygame library and our own levels.py we made for the code to be more readable, obviously
|
|
|
|
window_fps = 60 # the frames per second, this decides how fast the main loop runs per second
|
|
|
|
def main(fps=window_fps): # the main function
|
|
pygame.init() # initalizes pygame
|
|
screen_width = 768 # the screen width
|
|
screen_height = 576 # the screen height
|
|
tick = 0
|
|
# for sprites 48x36
|
|
title = "Revival Inc." # the title of the window obviously
|
|
lastclickpos = [0, 0] # last position that was clicked, this changes every time you click somewhere on the game window
|
|
screen = pygame.display.set_mode((screen_width, screen_height)) # this initalizes the display with the screen_width and screen_height variables
|
|
pygame.display.set_caption(title) # this actually sets the title of the window from the title variable
|
|
smalltext = pygame.font.Font('Monocraft.ttf', 25)
|
|
basicfont = pygame.font.Font('Monocraft.ttf', 50) # this sets a basic font for now, we can change this to anything later onvariable
|
|
bigtext = pygame.font.Font('Monocraft.ttf', 50)
|
|
clock = pygame.time.Clock() # this is used to control the fps of the game if you scroll down the code a bit
|
|
click = False # variable I use to check if u clicked with the mouse when calling level function from levels.
|
|
sheet = pygame.image.load("sprites.png") # the sprite sheet which we then use to load specific sprites from
|
|
level = 1 # the level variable, this also gets changed during the "gameplay"
|
|
running = True # this variable starts the game loop
|
|
while running: # game loop
|
|
screen.fill((0, 0, 0)) # fills the screen with black, so the sprites loaded previously dont overlap and this clears the last frame every "tick"
|
|
for event in pygame.event.get(): # this checks for stuff like key presses and mouse clicks
|
|
if event.type == pygame.QUIT: # what happens when you click the X button on the window
|
|
running = False # poof
|
|
if event.type == pygame.KEYDOWN: # what happens when you press down a key
|
|
if event.key == pygame.K_ESCAPE: # what happens when you press escape
|
|
running = False # poof
|
|
if event.key == pygame.K_k:
|
|
level = 4.5
|
|
if event.type == pygame.MOUSEBUTTONDOWN: # what happens when you press down a mouse button
|
|
click = True # sets the click variable to True so level function in levels.py can see
|
|
print(pygame.mouse.get_pos()) # prints out where the mouse is during the click for debugging purposes
|
|
lastclickpos = pygame.mouse.get_pos() # sets the variable to the position you last clicked
|
|
if event.type == pygame.MOUSEBUTTONUP: # what happens when you release a mouse button
|
|
click = False # sets the click variable to False so level function in levels.py can see again
|
|
if level == 0:
|
|
running = False
|
|
|
|
elif level == 1: # what happens when the level variable is equal to 1
|
|
level = levels.level1(screen, sheet, screen_width, screen_height, lastclickpos, basicfont, pygame.mouse, click, bigtext, level, tick) # runs the level1 function from levels.py
|
|
# THE REASON WE CALL IT LIKE THIS INSTEAD OF NORMALLY IS SO WE CAN CHANGE THE LEVEL VARIABLE
|
|
|
|
elif level == 2: # what happens when the level variable is equal to 2 ofc
|
|
tick += 1
|
|
level = levels.level2(screen, sheet, screen_width, screen_height, lastclickpos, basicfont, pygame.mouse, click, bigtext, level, tick, smalltext) # runs the level2 function from levels.py
|
|
elif level == 2.5:
|
|
tick = 0
|
|
level = 3
|
|
elif level == 3:
|
|
level = levels.level3(screen, sheet, screen_width, screen_height, lastclickpos, basicfont, pygame.mouse, click, bigtext, level, tick, smalltext) # runs the level3 function from levels.py
|
|
elif level == 4:
|
|
tick += 1
|
|
level = levels.level4(screen, sheet, screen_width, screen_height, lastclickpos, basicfont, pygame.mouse, click, bigtext, level, tick, smalltext) # runs the level4 function from levels.py
|
|
elif level == 4.5:
|
|
tick = 0
|
|
level = 5
|
|
elif level == 5:
|
|
tick += 1
|
|
level = levels.level5(screen, sheet, screen_width, screen_height, lastclickpos, basicfont, pygame.mouse, click, bigtext, level, tick, smalltext) # runs the level5 function from levels.py
|
|
fps_text = smalltext.render(str(int(clock.get_fps())), True, (0, 255, 0)) # defines a thing that renders your fps in the top left corner
|
|
screen.blit(fps_text, (5, 5)) # this actually renders "ftp_text" on the screen at the position (5, 5) which is the top left corner
|
|
pygame.display.flip() # this updates the display so it displays all the new stuff we added
|
|
clock.tick(fps) # this makes sure the game loop runs at the fps we set it to before
|
|
pygame.quit() #this quits our game when the game loop somehow ends, for example by pressing escape or the X button on the window
|
|
if __name__ == "__main__": # this just makes sure that this only runs when you launch it and not when you import it as a module like we imported levels.py at the top
|
|
main() # this actually runs our stuff |