added all the comments, ALL OF THEM

This commit is contained in:
dvorakmortonxd 2026-05-29 13:29:26 +02:00
commit b0bafa7503
3 changed files with 60 additions and 61 deletions

View file

@ -1,11 +1,11 @@
from render import load_sprite
from render import load_sprite # this makes it so our function for rendering sprites from sprites.png is available in here
def level1(screen, sheet, lastclickpos, basicfont, mouse, click, level):
screen.blit(load_sprite(sheet, 0, 0, 26, 8, 210, 60), (200, 200))
if click:
level = 2
print("yo")
return level
def level2(screen, sheet, lastclickpos, basicfont, mouse, click, level):
print("ur done")
return level
def level1(screen, sheet, lastclickpos, basicfont, mouse, click, level): # the function for our first level, ignore the unused variables here, you can use them when you edit it
screen.blit(load_sprite(sheet, 0, 0, 26, 8, 210, 60), (200, 200)) # this renders a button I made in the sprites.png, its purely for visual purposes so it doesnt matter where you click rn
if click: # this checks if you clicked, when you clicked it does the following stuff under
level = 2 # sets the level to 2
print("yo") # prints out "yo" for debugging purposes, otherwise completely unnecessary
return level # this returns the change we made to the level so that it updates in main.py
def level2(screen, sheet, lastclickpos, basicfont, mouse, click, level): # function for level 2
print("ur done") # prints out "ur done"
return level # returns the current level set

89
main.py
View file

@ -1,48 +1,47 @@
#!/usr/bin/env python3
import pygame
import levels
import pygame, levels # Imports the pygame library and our own levels.py we made for the code to be more readable, obviously
# VARIABLES
screen_width= 800
screen_height = 500
window_fps = 60
title = "Example Title"
window_fps = 60 # the frames per second, this decides how fast the main loop runs per second
def main(fps=window_fps):
pygame.init()
sheet = pygame.image.load("sprites.png")
lastclickpos = [0, 0]
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(title)
basicfont = pygame.font.SysFont(None, 30)
clock = pygame.time.Clock()
click = False
sheet = pygame.image.load("sprites.png")
level = 1
running = True
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill these in with your logic
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: # put in here some action yknow
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
click = True
print(pygame.mouse.get_pos())
lastclickpos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONUP:
click = False
if level == 1:
level = levels.level1(screen, sheet, lastclickpos, basicfont, pygame.mouse, click, level)
elif level == 2:
level = levels.level2(screen, sheet, lastclickpos, basicfont, pygame.mouse, click, level)
fps_text = basicfont.render(str(int(clock.get_fps())), True, (0, 255, 0))
screen.blit(fps_text, (5, 5))
pygame.display.flip()
clock.tick(fps)
pygame.quit()
if __name__ == "__main__":
main()
def main(fps=window_fps): # the main function
pygame.init() # initalizes pygame
screen_width = 800 # the screen width
screen_height = 500 # the screen height
title = "Example Title" # 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
basicfont = pygame.font.SysFont(None, 30) # this sets a basic font for now, we can change this to anything later on
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.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 == 1: # what happens when the level variable is equal to 1
level = levels.level1(screen, sheet, lastclickpos, basicfont, pygame.mouse, click, level) # 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
level = levels.level2(screen, sheet, lastclickpos, basicfont, pygame.mouse, click, level) # runs the level2 function from levels.py
fps_text = basicfont.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

View file

@ -1,7 +1,7 @@
import pygame
import pygame # imports pygame obviously
def load_sprite(sheet, x, y, width, height, screen_width, screen_height):
rect = pygame.Rect(x, y, width, height)
sprite = sheet.subsurface(rect)
scaled = pygame.transform.scale(sprite, (screen_width, screen_height))
return scaled
def load_sprite(sheet, x, y, width, height, screen_width, screen_height): # the function for our sprite rendering function
rect = pygame.Rect(x, y, width, height) # this defines our cutout from the sprite sheet its like position; size
sprite = sheet.subsurface(rect) # this goes on to cut it from the sheet actually
scaled = pygame.transform.scale(sprite, (screen_width, screen_height)) # this scales the cutout to what we want/what we chose
return scaled # this returns our cutout