game/main.py
SpagettiFisch c3c44bbc71 new river sprite file added
new idea lol

Signed-off-by: SpagettiFisch <63868515+SpagettiFisch@users.noreply.github.com>
2024-03-07 09:50:10 +01:00

259 lines
11 KiB
Python

import pygame
import sys
import json
import time
import random
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 genRooms(WIDTH, HEIGHT, type:str, objects:list):
room_objects = []
#room_objects = [Obstacle('dirt', 'boulder', 'art/images/dirt2.png', False, 32, 32, WIDTH=WIDTH - 64, HEIGHT=HEIGHT - 64)]
room_objects.append(Obstacle('river', 'water', 'art/images/river.png', True, random.randint(32, round(WIDTH * 0.75)), 32, WIDTH=96, HEIGHT=round(HEIGHT * 0.66)))
rooms = [
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, random.randint(0, 1))]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 0),
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, random.randint(0, 1))]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 1),
Room(type, 'normal', f'art/images/{type}.png', [objects[0], objects[1], objects[2], [room_objects[random.randint(0, len(room_objects) - 1)] for i in range(0, random.randint(0, 1))]], WIDTH - 64, HEIGHT - 64, [True, True, True, False], 2),
]
return rooms
def play(screen, clock, running, background, isblack, WIDTH, HEIGHT):
main = [MainCharacter('Herbert', 100, 'oldman.png', 500, 500, 20, 5, 1, 1, 50)]
mobs = [Skeleton(i, random.randint(40, 60), 'reddy.png', random.randint(20,1000), random.randint(20,700), 5, 1, 1, 1, 200) for i in range(0,random.randint(2, 8))]
others = []
npcs = [NPC('name', 100, 'reddy.png', 1, 200, 200)]
objects = [main, mobs, npcs, others]
level = []
rooms = genRooms(WIDTH, HEIGHT, 'grass', objects)
level.append(Stage('blau', 'normal', None, [], WIDTH, HEIGHT, 'blue', rooms))
scene = Scene('test', 'normal', None, None, WIDTH, HEIGHT, level)
freeze = False #Gameplay is freezed in certain situations
while running:
screen.fill('#000000')
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_e: #when book is open gameplay is freezed
freeze = not freeze
# 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))
"""
if not freeze:
objects = scene.getObjects()
screen.blit(scene.background, (32, 32))
for thing in objects[3]:
thing.update(objects)
thing.draw(screen)
for thing in objects[0]:
thing.book.hidden = not freeze
if not thing.update(pygame.key.get_pressed(), pygame.mouse.get_pos(), objects):
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen)
for mob in objects[1]:
mob.update(objects)
mob.draw(screen)
for npc in objects[2]:
npc.update(pygame.key.get_pressed(), objects)
npc.draw(screen)
objects[0][0].book.addspell('windslash')
scene.update(False, objects)
else:
objects[0][0].book.hidden = not freeze
objects[0][0].book.draw(screen)
objects[0][0].book.update()
# 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, 'textbox.png', 'medieval', 48, "Play", play, attributes=[screen, clock, running, background, isblack, WIDTH, HEIGHT]))
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 - 72, 160, 64, 'textbox.png', 'medieval', 48, "Options", uwu))
objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'textbox.png', '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.update(screen)
# 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):
main = [MainCharacter('Herbert', 100, 'oldman.png', 500, 500, 20, 5, 1, 1, 50)]
mobs = [Skeleton(i, random.randint(40, 60), 'reddy.png', random.randint(20,1000), random.randint(20,700), 5, 1, 1, 1, 200) for i in range(0,random.randint(2, 8))]
others = []
npcs = [NPC('name', 100, 'reddy.png', 1, 200, 200)]
objects = [main, mobs, npcs, others]
level = []
rooms = genRooms(WIDTH, HEIGHT, 'grass', objects)
level.append(Stage('blau', 'normal', None, [], WIDTH, HEIGHT, 'blue', rooms))
freeze = False #Gameplay is freezed in certain situations
#level.append(Stage('rot', 'normal', None, [], WIDTH, HEIGHT, 'red', [
# Room('red1', 'normal', 'art/images/grass.png', [], WIDTH, HEIGHT, [True, True, True, False], 0),
# Room('red2', 'normal', 'art/images/grass.png', [], WIDTH, HEIGHT, [True, True, True, False], 1),
# Room('red3', 'normal', 'art/images/grass.png', [], WIDTH, HEIGHT, [True, True, True, False], 2),
# ]))
scene = Scene('test', 'normal', None, None, WIDTH, HEIGHT, level)
# RENDER YOUR GAME HERE
while True:
screen.fill('#000000')
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
quitGame()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_e: #when book is open gameplay is freezed
freeze = not freeze
if not freeze:
objects = scene.getObjects()
for thing in objects[0]:
thing.book.hidden = not freeze
if not thing.update(pygame.key.get_pressed(), objects):
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
thing.draw(screen)
for mob in objects[1]:
mob.update(objects)
mob.draw(screen)
for npc in objects[2]:
npc.update(pygame.key.get_pressed(), objects)
npc.draw(screen)
for thing in objects[3]:
thing.update(objects)
thing.draw(screen)
else:
objects[0][0].book.hidden = not freeze
objects[0][0].book.draw(screen)
objects[0][0].book.update()
# 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, 'textbox.png', 'medieval', 48, "Play", play))
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2, 160, 64, 'textbox.png', 'medieval', 48, "Options", uwu))
#objects.append(Button(WIDTH / 2 - 80, HEIGHT / 2 + 72, 160, 64, 'textbox.png', 'medieval', 48, "Exit game", quitGame))
menu(screen, clock, running, background, isblack, WIDTH, HEIGHT)
test(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()