Update of GameObjects module
Code cleanup
This commit is contained in:
@@ -193,7 +193,7 @@ class Player(GameObject_new):
|
||||
|
||||
def move(self, *speed:int):
|
||||
MOVE_SPEED = 1
|
||||
if(self.__test_counter == 15):
|
||||
if(self.__test_counter == 10):
|
||||
self.__test_counter = 0
|
||||
if(self.__movedirection['up']):
|
||||
if(not speed): self._position[1] -= MOVE_SPEED
|
||||
@@ -221,7 +221,7 @@ class Player(GameObject_new):
|
||||
self.__test_counter+=1
|
||||
|
||||
def draw(self):
|
||||
if(self.__test_counter == 15):
|
||||
if(self.__test_counter == 10):
|
||||
self.__snake_body.insert(0, list(self._position))
|
||||
counter = 0
|
||||
for pos in self.__snake_body:
|
||||
@@ -232,7 +232,7 @@ class Player(GameObject_new):
|
||||
counter += 1
|
||||
counter = 0
|
||||
#print(pos)
|
||||
if(self.__test_counter == 15):
|
||||
if(self.__test_counter == 10):
|
||||
self.__snake_body.pop()
|
||||
|
||||
def collide_fruit(self, rect:pygame.Rect):
|
||||
@@ -245,7 +245,6 @@ class Player(GameObject_new):
|
||||
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
|
||||
|
||||
@@ -264,12 +263,28 @@ class Player(GameObject_new):
|
||||
def get_player_rect(self):
|
||||
return self.__player_rect
|
||||
|
||||
def inc_score(self, points:int):
|
||||
self._score += points
|
||||
|
||||
|
||||
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:
|
||||
def __init__(self, name: str, surface: pygame.Surface, surface_size: tuple, init_pos_x, init_pos_y, visibility: bool = True, rect_size:int = 10, color:tuple = (0,0,0), score_points = 0) -> None:
|
||||
super().__init__(name, surface, surface_size, init_pos_x, init_pos_y, visibility)
|
||||
|
||||
pass
|
||||
self.__score_points = score_points
|
||||
self.__rect:pygame.Rect
|
||||
self.__rect_size = rect_size
|
||||
self.__color = color
|
||||
|
||||
def draw(self):
|
||||
self.__rect = pygame.draw.rect(self._surface, self.__color, (self._position[0], self._position[1], self.__rect_size, self.__rect_size))
|
||||
|
||||
def get_rect(self) -> pygame.Rect:
|
||||
return self.__rect
|
||||
|
||||
def get_score_points(self) -> int:
|
||||
return self.__score_points
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
@@ -16,6 +16,7 @@ my_font3 = pygame.font.SysFont('times new roman', 46)
|
||||
WEISS = ( 255, 255, 255)
|
||||
SCHWARZ = (0,0,0)
|
||||
GRUEN = (0, 255, 0)
|
||||
ROT = (255,0,0)
|
||||
|
||||
active = True
|
||||
|
||||
@@ -65,12 +66,6 @@ def apply_name(input):
|
||||
#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()
|
||||
|
||||
@@ -97,19 +92,9 @@ 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()
|
||||
@@ -140,16 +125,19 @@ while active == True:
|
||||
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
|
||||
text_surface_score = my_font.render('Score: '+str(player.get_player_score()), True, (0, 0, 0))
|
||||
text_surface_player = my_font.render("Player: " + User.getusername(), True, SCHWARZ, None)
|
||||
|
||||
MAINSCREEN.blit(text_surface_score, (MAINSCREEN_SIZE[0]/2-text_surface_score.get_size()[0]/2,0)) #Paints the Scoreboard in Top/Center
|
||||
MAINSCREEN.blit(text_surface_player, (10, 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)
|
||||
fruit = Fruit("Fruit", MAINSCREEN, MAINSCREEN_SIZE, init_pos_x=rand.randint(0, MAINSCREEN_SIZE[0]-40), init_pos_y=rand.randint(0, MAINSCREEN_SIZE[1]-40), color=ROT, rect_size=40, score_points=10)
|
||||
spawn_fruit = False
|
||||
|
||||
if(fruit.is_collided() == False):
|
||||
fruit.draw()
|
||||
|
||||
if(fruit):
|
||||
fruit.draw()
|
||||
|
||||
|
||||
player.draw()
|
||||
if(game_started == True and player.collide_self() == False):
|
||||
@@ -158,62 +146,23 @@ while active == True:
|
||||
if(player.collide_self()):
|
||||
game_started == False
|
||||
gameovertext = my_font2.render("Game Over!", True, SCHWARZ)
|
||||
if(player.get_player_score() > User.gethighscore()):
|
||||
User.update_user_highscore(player.get_player_score())
|
||||
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")
|
||||
|
||||
if(player.collide_fruit(fruit.get_rect()) == True and fruit):
|
||||
player.add_body()
|
||||
player.inc_score(fruit.get_score_points())
|
||||
del fruit
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user