197 lines
6.5 KiB
Python
197 lines
6.5 KiB
Python
import pygame
|
|
import pygame_menu
|
|
import random as rand
|
|
from copy import deepcopy
|
|
import modules.User as User
|
|
from modules.GameObject import *
|
|
|
|
pygame.init()
|
|
MAINSCREEN_SIZE = (1024,768)
|
|
MAINSCREEN = pygame.display.set_mode(MAINSCREEN_SIZE)
|
|
pygame.display.set_caption("Testgame")
|
|
|
|
WEISS = ( 255, 255, 255)
|
|
SCHWARZ = (0,0,0)
|
|
GRUEN = (0, 255, 0)
|
|
|
|
active = True
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
#TODO: Make Grid for running Rect
|
|
"""TODO: Add new controlstructure for User-class on first login
|
|
Actually buggy if player logsin the first time, because there is no playerselected before there was an insterquery!!!
|
|
"""
|
|
|
|
#Behandelt später die Hauptschleife und Klassenobjekte
|
|
class Game():
|
|
__state = {"win":False, "lose":False, "paused":False}
|
|
def __init__(self) -> None:
|
|
pass
|
|
#Handles all collided objects an adds it as playerfollower
|
|
|
|
def start_game():
|
|
menu.disable()
|
|
return 0
|
|
|
|
def apply_name(input):
|
|
print(input)
|
|
if(len(input) <= 0):
|
|
print("Kein Name eingegeben!")
|
|
else:
|
|
if(User.select(input) == False):
|
|
User.createuser(input)
|
|
elif(User.select(input) == True):
|
|
print("Es existiert bereits ein Spieler mit diesem Namen!")
|
|
|
|
print(F"Hallo {User.getusername()}, dein letztes Spiel war am: {User.getlastlogin()} mit einem Highscore von: {User.gethighscore()}")
|
|
|
|
menu.disable()
|
|
#TODO: Add function for save name
|
|
pass
|
|
|
|
count_follower = 0
|
|
|
|
def append_follwer(x,y):
|
|
print("x: "+str(x), "y: "+str(y))
|
|
newfollower = Block("Follower "+str(count_follower), MAINSCREEN, MAINSCREEN_SIZE, is_rect=True, rect_size=(x,y))
|
|
colided_objects.append(newfollower)
|
|
|
|
User = User.User()
|
|
|
|
menu = pygame_menu.Menu("Snake v1.0", 400, 300,)
|
|
menu.add.text_input("Name: ", onreturn=(apply_name))
|
|
menu.add.button('Play', start_game)
|
|
menu.add.button('Quit', pygame_menu.events.EXIT)
|
|
menu.mainloop(MAINSCREEN)
|
|
|
|
def spawn_entities(x:int, list_of_objects:list):
|
|
count = 0
|
|
for y in range(x):
|
|
rect_size = 80
|
|
Entity = Block("Entitiy-"+str(count), MAINSCREEN, MAINSCREEN_SIZE, rand.randint(1,MAINSCREEN_SIZE[0]), rand.randint(1,MAINSCREEN_SIZE[1]), rand.randint(1,15), is_rect=True, rect_size=(rect_size,rect_size))
|
|
Entity.get_name(True)
|
|
list_of_objects.append(Entity)
|
|
count += 1
|
|
|
|
player = Player("Player", MAINSCREEN, MAINSCREEN_SIZE, 10, 10, 10, is_player=False, is_rect=True, color=(0,255,0) ,rect_size=(80,80))
|
|
|
|
goo = GOIMAGE("TESTOBJEKT", MAINSCREEN, MAINSCREEN_SIZE, 0,0,0,image="data/models/testmodel.bmp")
|
|
|
|
testlist = list()
|
|
for y in range(10):
|
|
Entity = GOIMAGE("Testobject", MAINSCREEN, MAINSCREEN_SIZE, posx_init=rand.randint(10, 600), posy_init=rand.randint(10, 600), image="data/models/testmodel.bmp")
|
|
testlist.append(Entity)
|
|
|
|
background = pygame.surface.Surface((640,480))
|
|
|
|
x = 3600
|
|
invert = False
|
|
colided_objects = list()
|
|
spawned_entities = list()
|
|
spawn_entities(150, spawned_entities)
|
|
count_hidden_entities = 0
|
|
count_spawend_enities = len(spawned_entities)
|
|
print("Es wurden " + str(count_spawend_enities)+ " Objekte gespawnt")
|
|
|
|
testx = 10
|
|
testy = 10
|
|
|
|
#Fenster-Hauptschleife+
|
|
while active == True:
|
|
MAINSCREEN.fill((255,255,255))
|
|
for event in pygame.event.get():
|
|
if (event.type == pygame.QUIT):
|
|
print("Programm wird geschlossen!")
|
|
print(spawned_entities, end="\n")
|
|
active = False
|
|
elif (event.type == pygame.KEYDOWN):
|
|
if (event.key == pygame.K_UP):
|
|
#print("Keydown")
|
|
player.change_direction("up")
|
|
elif (event.key == pygame.K_DOWN):
|
|
#print("Keydown")
|
|
player.change_direction("down")
|
|
elif (event.key == pygame.K_RIGHT):
|
|
#print("Keydown")
|
|
player.change_direction("right")
|
|
elif (event.key == pygame.K_LEFT):
|
|
#print("Keydown")
|
|
player.change_direction("left")
|
|
|
|
goo.draw()
|
|
|
|
player.draw()
|
|
player.update_pos_lastframe(player.get_position()[0], player.get_position()[1], False)
|
|
player.move(5)
|
|
#print("Aktuelle Position: " + str(player.get_position()))
|
|
#print("Aktuell kollidierte Objekte: ", str(count_hidden_entities))
|
|
#Spawns the Entities
|
|
for y in spawned_entities:
|
|
y:Block
|
|
y.draw()
|
|
if(y.is_collided() == True):
|
|
#Der erste Follower folgt dem Spieler, alle übrigen folgen den vorherien Verfolgern
|
|
y.hide()
|
|
pass
|
|
if(y.is_collided() == False):
|
|
if(y.collide(player)):
|
|
print(F"Score: {y.get_score()}")
|
|
player.update_player_score(y.get_score())
|
|
print(F"Aktueller playerscore: {player.get_player_score()}")
|
|
count_hidden_entities += 1
|
|
xy = y.get_rect_size()
|
|
append_follwer(xy[0], xy[1])
|
|
count_follower += 1
|
|
|
|
for t in testlist:
|
|
t:GOIMAGE
|
|
t.draw()
|
|
|
|
#Zeichnet alle Verfolgerobjekte, sofern sie vorliegend sind
|
|
#TODO: Follower sollen ab dem zweiten Objekt dem vorangegangenen Objekt folgen und nicht dem player
|
|
|
|
#if(colided_objects):
|
|
# colided_objects[0].set_position_player(player.get_position(), player.get_playermovedirection(), True)
|
|
i=0
|
|
|
|
if(colided_objects):
|
|
pass
|
|
"""
|
|
#TODO:Berechne Position follower Objekte anhand Position aus letztem Frame
|
|
for follower in colided_objects:
|
|
follower:Block
|
|
if(i >= 0):
|
|
colided_objects[0].set_position_player(player.get_position(), player.get_playermovedirection(), True)
|
|
pass
|
|
if(i > 0 and i <= len(colided_objects)):
|
|
#colided_objects[i].set_position_player(follower.set_position(colided_objects[i-1].get_position()), player.get_playermovedirection(), True)
|
|
pass
|
|
follower.draw()
|
|
i += 1"""
|
|
|
|
|
|
|
|
if(count_hidden_entities == count_spawend_enities):
|
|
print("WIN!!!")
|
|
if(player.get_player_score() > User.gethighscore()):
|
|
print("Neuer Highscore!!!")
|
|
User.update_user_highscore(player.get_player_score())
|
|
menu.enable()
|
|
menu.add.label("WIN", "win", 10)
|
|
menu.mainloop(MAINSCREEN)
|
|
break
|
|
|
|
if(x == 0):
|
|
for y in spawned_entities:
|
|
y.hide()
|
|
pass
|
|
x -= 1
|
|
#print("Objekt-Koordinaten= X: "+str(rect1.x)+" Y: "+str(rect1.y)+"", end="\r")
|
|
|
|
#Displayflip sorgt dafür, dass das Fenster entsprechend der Tickrate aktualisiert wird
|
|
#pygame.display.flip()
|
|
|
|
pygame.display.flip()
|
|
clock.tick(60)
|
|
pygame.quit() |