From d0bbdfd6f1f2a7b07ba62f0ea54aa93660b25170 Mon Sep 17 00:00:00 2001 From: administrator Date: Tue, 30 Jan 2024 21:18:27 +0100 Subject: [PATCH] Code cleanup Repositioning Scoreboard to center of top --- modules/GameObject.py | 5 +++++ pygame-test.py | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/GameObject.py b/modules/GameObject.py index fd3d83a..2975b30 100644 --- a/modules/GameObject.py +++ b/modules/GameObject.py @@ -171,11 +171,13 @@ class Player(GameObject_new): ) -> None: self._size = size self._score = 0 + self._rect:pygame.Rect super().__init__(name, surface, surface_size, init_pos_x, init_pos_y, visibility) __movedirection = {"up":False, "down":False, "left":False, "right":False} __snake_body = [[100,100], [100,140], [100,180], [100,220]] __test_counter = 0 + def user_test_func(self): pass @@ -231,6 +233,9 @@ class Player(GameObject_new): def get_rect_size(self): return self._size + def get_rect(self) -> pygame.Rect: + return self._rect + def get_player_score(self) -> int: return self._score diff --git a/pygame-test.py b/pygame-test.py index 7ad6885..04cfbbb 100644 --- a/pygame-test.py +++ b/pygame-test.py @@ -9,7 +9,7 @@ pygame.init() MAINSCREEN_SIZE = (1024,768) MAINSCREEN = pygame.display.set_mode(MAINSCREEN_SIZE) pygame.display.set_caption("Testgame") -my_font = pygame.font.SysFont('times new roman', 26) +my_font = pygame.font.SysFont('arial', 26) WEISS = ( 255, 255, 255) SCHWARZ = (0,0,0) @@ -100,11 +100,16 @@ 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)) #TESTOBJECT.printall_attributes() #Fenster-Hauptschleife+ + +spawn_fruit = True + while active == True: MAINSCREEN.fill((255,255,255)) for event in pygame.event.get(): @@ -127,15 +132,21 @@ while active == True: player.change_direction("left") text_surface = my_font.render('Score: '+str(player.get_player_score()), False, (0, 0, 0)) - MAINSCREEN.blit(text_surface, (0,0)) + MAINSCREEN.blit(text_surface, (MAINSCREEN_SIZE[0]/2-text_surface.get_size()[0]/2,0)) #Paints the Scoreboard in Top/Center - player.draw() - player.move(40) + if(counter == 300): player.add_body() counter = 0 + if(fruit.is_collided() == False): + fruit.draw() + + + player.draw() + player.move(40) + counter += 1 #print("Aktuelle Position: " + str(player.get_position())) #print("Aktuell kollidierte Objekte: ", str(count_hidden_entities))