224 lines
7.0 KiB
Python
224 lines
7.0 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("Snake_v1")
|
|
my_font = pygame.font.SysFont('times new roman', 26)
|
|
my_font2 = pygame.font.SysFont('times new roman', 32)
|
|
my_font3 = pygame.font.SysFont('times new roman', 46)
|
|
|
|
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!!!
|
|
|
|
Make a live scoreboard
|
|
|
|
"""
|
|
|
|
#Behandelt später die Hauptschleife und Klassenobjekte
|
|
class Game():
|
|
__state = {"win":False, "lose":False, "paused":False}
|
|
def __init__(self) -> None:
|
|
pass
|
|
|
|
def scoreboard(self):
|
|
pass
|
|
|
|
def spawn_fruit(self):
|
|
pass
|
|
|
|
def spawn_player(self):
|
|
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)
|
|
|
|
player = Player("Player", MAINSCREEN, MAINSCREEN_SIZE, 100, 100, size=40)
|
|
|
|
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
|
|
|
|
|
|
|
|
background = pygame.surface.Surface((640,480))
|
|
|
|
x = 3600
|
|
invert = False
|
|
colided_objects = list()
|
|
spawned_entities = list()
|
|
spawn_entities(2, 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
|
|
|
|
counter = 0
|
|
|
|
|
|
#TESTOBJECT = GameObject_new("Testobjekt", MAINSCREEN, MAINSCREEN_SIZE, 0, 0)
|
|
#print(help(TESTOBJECT))
|
|
#TESTOBJECT.printall_attributes()
|
|
|
|
#Fenster-Hauptschleife+
|
|
|
|
spawn_fruit = True
|
|
game_started = False
|
|
|
|
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):
|
|
game_started = True
|
|
player.change_direction("up")
|
|
elif (event.key == pygame.K_DOWN):
|
|
game_started = True
|
|
player.change_direction("down")
|
|
elif (event.key == pygame.K_RIGHT):
|
|
game_started = True
|
|
player.change_direction("right")
|
|
elif (event.key == pygame.K_LEFT):
|
|
game_started = True
|
|
player.change_direction("left")
|
|
|
|
text_surface = my_font.render('Score: '+str(player.get_player_score()), True, (0, 0, 0))
|
|
MAINSCREEN.blit(text_surface, (MAINSCREEN_SIZE[0]/2-text_surface.get_size()[0]/2,0)) #Paints the Scoreboard in Top/Center
|
|
|
|
if(spawn_fruit == True):
|
|
fruit = GameObject("Fruit", MAINSCREEN, MAINSCREEN_SIZE, posx_init=rand.randint(0, MAINSCREEN_SIZE[0]), posy_init=rand.randint(0, MAINSCREEN_SIZE[1]), rect_size=(40,40), color=GRUEN)
|
|
spawn_fruit = False
|
|
|
|
if(fruit.is_collided() == False):
|
|
fruit.draw()
|
|
|
|
|
|
player.draw()
|
|
if(game_started == True and player.collide_self() == False):
|
|
player.move(40)
|
|
|
|
if(player.collide_self()):
|
|
game_started == False
|
|
gameovertext = my_font2.render("Game Over!", True, SCHWARZ)
|
|
MAINSCREEN.blit(gameovertext, (MAINSCREEN_SIZE[0]/2-gameovertext.get_size()[0]/2, MAINSCREEN_SIZE[1]/2-gameovertext.get_size()[1]/2))
|
|
|
|
if(player.collide_fruit(fruit.get_rect()) == True and fruit.is_collided() == False):
|
|
fruit.set_collided()
|
|
#print("Kollision")
|
|
player.add_body()
|
|
spawn_fruit=True
|
|
|
|
|
|
|
|
#print(player.get_position())
|
|
#print(player.get_player_rect())
|
|
|
|
#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"""
|
|
|
|
#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(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() |