game/main.py
SpagettiFisch ed6c91fa8f test for gameobjects (level, rooms and scenes) created
Signed-off-by: SpagettiFisch <63868515+SpagettiFisch@users.noreply.github.com>
2024-02-22 10:59:02 +01:00

181 lines
6.6 KiB
Python

import pygame
import sys
import json
import time
from classes import *
from viecher import *
fps = 60
def setUp(config):
pygame.init()
if config["fullscreen"]:
screen = pygame.display.set_mode(config["res"], pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode(config["res"])
clock = pygame.time.Clock()
return screen, clock, True, True, "start.png", []
def readConfig():
with open('config.json', 'r') as c:
json_data = c.read()
return json.loads(json_data)
def quitGame():
#save progress somehow, if needed
pygame.quit()
quit()
def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = [[], [], []] #0 - maincharacter stuff; 1 - mobs; 2 - npcs
objects[0].append(MainCharacter('Herbert', 100, 'reddy.png', 125, 5, 1, 1, 50))
while running:
screen.fill('#000000')
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
quitGame()
# RENDER YOUR GAME HERE
"""with open(background, 'r') as i:
bg = pygame.image.load(i)
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# fill the screen with an image to clear the screen
screen.blit(bg, (0, 0))
"""
for obj in objects:
obj.update(pygame.key.get_pressed())
obj.draw(screen)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(fps) # limits FPS to 60
def options(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = []
# List that is displayed while selecting the window resolution level
resolution = [("1920x1080", "1920x1080"),
("1920x1200", "1920x1200"),
("1280x720", "1280x720"),
("2560x1440", "2560x1440"),
("3840x2160", "3840x2160")]
# This function displays the currently selected options
def printSettings():
print("\n\n")
# getting the data using "get_input_data" method of the Menu class
settingsData = settings.get_input_data()
for key in settingsData.keys():
print(f"{key}\t:\t{settingsData[key]}")
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# RENDER YOUR GAME HERE
with open(background, 'r') as i:
bg = pygame.image.load(i)
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# fill the screen with an image to clear the screen
screen.blit(bg, (0, 0))
for obj in objects:
obj.process(screen)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
def menu(screen, clock, running, background, isblack, WIDTH, HEIGHT):
objects = []
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'medieval', 48, "Play", play))
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'medieval', 48, "Options", uwu))
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'medieval', 48, "Exit game", quitGame))
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quitGame()
# RENDER YOUR GAME HERE
with open(f'art/images/{background}', 'r') as i:
bg = pygame.image.load(i)
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# fill the screen with an image to clear the screen
screen.blit(bg, (0, 0))
for obj in objects:
obj.process(screen, clock, running, background, isblack, WIDTH, HEIGHT)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
def test(screen, clock, running, background, isblack, WIDTH, HEIGHT):
level = []
level.append(Level('blau', 'normal', None, [], WIDTH, HEIGHT, 'blue', [
Room('blau1', 'normal', 'art/images/blau1.png', [], WIDTH, HEIGHT, [True, True, True, False], 0),
Room('blau2', 'normal', 'art/images/blau2.png', [], WIDTH, HEIGHT, [True, True, True, False], 1),
Room('blau3', 'normal', 'art/images/blau3.png', [], WIDTH, HEIGHT, [True, True, True, False], 2),
]))
level.append(Level('rot', 'normal', None, [], WIDTH, HEIGHT, 'red', [
Room('red1', 'normal', 'art/images/rot1.png', [], WIDTH, HEIGHT, [True, True, True, False], 0),
Room('red2', 'normal', 'art/images/rot2.png', [], WIDTH, HEIGHT, [True, True, True, False], 1),
Room('red3', 'normal', 'art/images/rot3.png', [], WIDTH, HEIGHT, [True, True, True, False], 2),
]))
scene = Scene('test', 'normal', None, None, WIDTH, HEIGHT, level)
# RENDER YOUR GAME HERE
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quitGame()
scene.update()
scene.draw(screen)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
def main():
config = readConfig()
screen, clock, running, isblack, background, objects = setUp(config["screen"])
WIDTH, HEIGHT = screen.get_size()
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'medieval', 48, "Play", play))
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'medieval', 48, "Options", uwu))
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'medieval', 48, "Exit game", quitGame))
test(screen, clock, running, background, isblack, WIDTH, HEIGHT)
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
"""while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
if not isblack:
with open(background, 'r') as i:
bg = pygame.image.load(i)
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# fill the screen with a color to wipe away anything from last frame
screen.blit(bg, (0, 0))
# RENDER YOUR GAME HERE
else:
for obj in objects:
obj.process(screen, clock, running, background, isblack, WIDTH, HEIGHT)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60"""
pygame.quit()
if __name__ == '__main__':
main()