diff --git a/modules/GameObject.py b/modules/GameObject.py index 2975b30..b863871 100644 --- a/modules/GameObject.py +++ b/modules/GameObject.py @@ -172,11 +172,13 @@ class Player(GameObject_new): self._size = size self._score = 0 self._rect:pygame.Rect + self.__snake_body = [[100,100+size], [100,100+size*2], [100,100+size*3], [100,size*5]] 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 + __player_rect:pygame.Rect def user_test_func(self): @@ -221,24 +223,54 @@ class Player(GameObject_new): def draw(self): if(self.__test_counter == 15): self.__snake_body.insert(0, list(self._position)) + counter = 0 for pos in self.__snake_body: - pygame.draw.rect(self._surface, (0,0,0), pygame.Rect(pos[0], pos[1], self._size, self._size)) - print(pos) + if(counter == 0): + self.__player_rect = pygame.draw.rect(self._surface, (0,0,0), pygame.Rect(pos[0], pos[1], self._size, self._size)) + else: + pygame.draw.rect(self._surface, (0,0,0), pygame.Rect(pos[0], pos[1], self._size, self._size)) + counter += 1 + counter = 0 + #print(pos) if(self.__test_counter == 15): self.__snake_body.pop() + + def collide_fruit(self, rect:pygame.Rect): + if(pygame.Rect.colliderect(self.get_player_rect(), rect)): + return True + else: + return False + + def collide_self(self): + count = len(self.__snake_body) + for elements in range(1, count): + if(self.get_position() == self.__snake_body[elements]): + print("Game Over!") + return True + return False def add_body(self): - self.__snake_body.insert(0,list(self._position)) + self.__snake_body.append(list(self._position)) - def get_rect_size(self): - return self._size - - def get_rect(self) -> pygame.Rect: - return self._rect + def get_position(self) -> tuple: + return self.__snake_body[0] def get_player_score(self) -> int: return self._score + def get_positions_snake_body(self) -> list: + return self.__snake_body + + def get_player_rect(self): + return self.__player_rect + + +class Fruit(GameObject_new): + def __init__(self, name: str, surface: pygame.Surface, surface_size: tuple, init_pos_x, init_pos_y, visibility: bool = True) -> None: + super().__init__(name, surface, surface_size, init_pos_x, init_pos_y, visibility) + + pass + """ class GOIMAGE(GameObject): diff --git a/pygame-test.py b/pygame-test.py index 04cfbbb..44ae773 100644 --- a/pygame-test.py +++ b/pygame-test.py @@ -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