Code cleanup

Added Gameover mechanics...
This commit is contained in:
2024-02-01 09:16:07 +01:00
parent d0bbdfd6f1
commit 65ac13612d
2 changed files with 79 additions and 24 deletions

View File

@@ -8,8 +8,10 @@ from modules.GameObject import *
pygame.init()
MAINSCREEN_SIZE = (1024,768)
MAINSCREEN = pygame.display.set_mode(MAINSCREEN_SIZE)
pygame.display.set_caption("Testgame")
my_font = pygame.font.SysFont('arial', 26)
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)
@@ -32,8 +34,15 @@ 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():
@@ -100,7 +109,6 @@ testy = 10
counter = 0
fruit = GameObject("Fruit", MAINSCREEN, MAINSCREEN_SIZE, posx_init=rand.randint(0, MAINSCREEN_SIZE[0]), posy_init=rand.randint(0, MAINSCREEN_SIZE[1]))
#TESTOBJECT = GameObject_new("Testobjekt", MAINSCREEN, MAINSCREEN_SIZE, 0, 0)
#print(help(TESTOBJECT))
@@ -109,6 +117,7 @@ fruit = GameObject("Fruit", MAINSCREEN, MAINSCREEN_SIZE, posx_init=rand.randint(
#Fenster-Hauptschleife+
spawn_fruit = True
game_started = False
while active == True:
MAINSCREEN.fill((255,255,255))
@@ -119,35 +128,49 @@ while active == True:
active = False
elif (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_UP):
#print("Keydown")
game_started = True
player.change_direction("up")
elif (event.key == pygame.K_DOWN):
#print("Keydown")
game_started = True
player.change_direction("down")
elif (event.key == pygame.K_RIGHT):
#print("Keydown")
game_started = True
player.change_direction("right")
elif (event.key == pygame.K_LEFT):
#print("Keydown")
game_started = True
player.change_direction("left")
text_surface = my_font.render('Score: '+str(player.get_player_score()), False, (0, 0, 0))
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(counter == 300):
player.add_body()
counter = 0
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()
player.move(40)
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())
counter += 1
#print("Aktuelle Position: " + str(player.get_position()))
#print("Aktuell kollidierte Objekte: ", str(count_hidden_entities))
#Spawns the Entities