213 lines
7.6 KiB
Python
213 lines
7.6 KiB
Python
import pygame
|
|
import random as rand
|
|
|
|
class GameObject():
|
|
_size_rect_x:int
|
|
_size_rect_y:int
|
|
__movespeed = int()
|
|
__rectobjekt:pygame.Rect
|
|
__surface:pygame.Surface
|
|
__mainscreensize:tuple
|
|
__hide = False
|
|
__collided = False
|
|
__is_player = False
|
|
__is_rect = False
|
|
__has_follower = False
|
|
__score:int
|
|
count = 0
|
|
|
|
def __init__(self, name:str, surface, mainscreensize:tuple, posx_init:int=0, posy_init:int=0, move_speed:int=0, is_player:bool = False, is_rect:bool = True, color:tuple = (0,0,0), rect_size:tuple = (10,10), image = str()) -> None:
|
|
self.__name = name
|
|
self._pos_x = posx_init
|
|
self._pos_y = posy_init
|
|
self.pos_array = (posx_init,posy_init)
|
|
self.__inital_pos_x = posx_init
|
|
self.__initial_pos_y = posx_init
|
|
self.__movespeed = move_speed
|
|
self.__is_player = is_player
|
|
self.__is_rect = is_rect
|
|
self._size_rect_x = rect_size[0]
|
|
self._size_rect_y = rect_size[1]
|
|
self.count += 1
|
|
self.__pos_lf_x = 0
|
|
self.__pos_lf_y = 0
|
|
self.__surface = surface
|
|
self.__mainscreensize = mainscreensize
|
|
self.__score = 0
|
|
self.__imagepath = image
|
|
self.__imageloaded = 0
|
|
self.__color = color
|
|
|
|
def draw(self):
|
|
#font = pygame.font.SysFont(None, 70)
|
|
#text = font.render("TEST", True, (0,0,0) )
|
|
if(not self.__hide):
|
|
self.__rectobjekt = pygame.draw.rect(self.__surface, self.__color, [self._pos_x, self._pos_y,self._size_rect_x,self._size_rect_y])
|
|
|
|
def get_name(self, debug=False):
|
|
if(debug):
|
|
print(self.__name)
|
|
return self.__name
|
|
|
|
def get_position(self, debug=False):
|
|
if(debug):
|
|
print((self._pos_x, self._pos_y))
|
|
return (self._pos_x, self._pos_y)
|
|
|
|
def get_rect(self):
|
|
return self.__rectobjekt
|
|
|
|
def get_rect_size(self):
|
|
return (self._size_rect_x, self._size_rect_y)
|
|
|
|
def get_movespeed(self):
|
|
return self.__movespeed
|
|
|
|
def get_playermovedirection(self):
|
|
return self.__movedirection
|
|
|
|
def get_player_score(self) -> int:
|
|
return self.__score
|
|
|
|
def get_surface(self) -> pygame.Surface:
|
|
return self.__surface
|
|
|
|
def has_follower(self):
|
|
return self.__has_follower
|
|
|
|
def set_follower(self):
|
|
self.__has_follower = True
|
|
|
|
def is_collided(self) -> bool:
|
|
return self.__collided
|
|
|
|
def set_collided(self):
|
|
self.__collided = True
|
|
|
|
def set_position(self, pos:tuple):
|
|
self._pos_x = pos[0]
|
|
self._pos_y = pos[1]
|
|
|
|
def update_pos_lastframe(self,x,y,debug=False):
|
|
if (debug):
|
|
print("Letzte Position: " + str((x,y)))
|
|
self.__pos_lf_x = x
|
|
self.__pos_lf_y = y
|
|
|
|
def update_player_score(self, score_from_entity):
|
|
if(score_from_entity > 0):
|
|
self.__score += score_from_entity
|
|
|
|
def collide(self, GameObject) -> bool:
|
|
#print(GameObject)
|
|
#print(GameObject.get_rect())
|
|
"""TODO: Hitbox with Objectsize"""
|
|
|
|
if(pygame.rect.Rect.colliderect(self.__rectobjekt, GameObject.get_rect()) and not self.is_collided()):
|
|
print("Kollision\n") #Funktioniert für das erste!
|
|
self.set_collided()
|
|
return True
|
|
#self.hide() #Löscht das Objekt nach einem Zusammenstoß mit Playerobject
|
|
return False
|
|
|
|
def hide(self):
|
|
if(self.__hide != True):
|
|
print("Gameoject mit der ID " + self.__name + " gelöscht!")
|
|
self.__hide = True
|
|
"""TODO: Delete obeject function"""
|
|
|
|
|
|
|
|
class Block(GameObject):
|
|
__score = int()
|
|
def __init__(self, name: str, surface, mainscreensize: tuple, posx_init: int = 0, posy_init: int = 0, move_speed: int = 0, is_player: bool = False, is_rect: bool = True, color: tuple = (0, 0, 0), rect_size: tuple = (10, 10), image=str()) -> None:
|
|
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, color, rect_size, image)
|
|
self.__score = rand.randint(5, 50)
|
|
|
|
def set_position_player(self, pos:tuple, playermovedirection:list, debug=False):
|
|
if(playermovedirection["up"]):
|
|
super().set_position((pos[0],pos[1]+super().get_rect_size()[1]))
|
|
print(pos)
|
|
if(playermovedirection["down"]):
|
|
super().set_position((pos[0],pos[1]-super().get_rect_size()[1]))
|
|
print(pos)
|
|
if(playermovedirection["left"]):
|
|
super().set_position((pos[0]+super().get_rect_size()[0],pos[1]))
|
|
print(pos)
|
|
if(playermovedirection["right"]):
|
|
super().set_position((pos[0]-super().get_rect_size()[0],pos[1]))
|
|
print(pos)
|
|
|
|
def test(self):
|
|
print(super().get_position())
|
|
|
|
def get_score(self) -> int:
|
|
return self.__score
|
|
|
|
class Player(GameObject):
|
|
#Later handles the Player-Gameobject
|
|
def __init__(self, name: str, surface, mainscreensize: tuple, posx_init: int = 0, posy_init: int = 0, move_speed: int = 0, is_player: bool = False, is_rect: bool = True, color: tuple = (0, 0, 0), rect_size: tuple = (10, 10), image=str()) -> None:
|
|
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, color, rect_size, image)
|
|
|
|
self.__mainscreensize = mainscreensize
|
|
print(self.__mainscreensize)
|
|
|
|
super().set_position((150, 550))
|
|
|
|
__movedirection = {"up":False, "down":False, "left":False, "right":False}
|
|
|
|
def user_test_func(self):
|
|
pass
|
|
|
|
def change_direction(self, direction:dict):
|
|
if(direction):
|
|
for key in self.__movedirection.keys():
|
|
self.__movedirection[key] = False
|
|
self.__movedirection[direction] = True
|
|
#print(direction) for debug purposes
|
|
|
|
def move(self, *speed:int):
|
|
MOVE_SPEED = 1
|
|
#self.__rectobjekt.x += MOVE_SPEED
|
|
objectpos = super().get_position()
|
|
|
|
if(self.__movedirection['up']):
|
|
if(not speed): self._pos_y -= MOVE_SPEED
|
|
else: self._pos_y -= speed[0]
|
|
if(self._pos_y <= -self._size_rect_y):
|
|
self._pos_y = self.__mainscreensize[1]
|
|
|
|
elif(self.__movedirection['down']):
|
|
if(not speed): self._pos_y += MOVE_SPEED
|
|
else: self._pos_y += speed[0]
|
|
if(self._pos_y >= self.__mainscreensize[1]):
|
|
self._pos_y = -self._size_rect_y
|
|
|
|
elif(self.__movedirection['left']):
|
|
if(not speed): self._pos_x -= MOVE_SPEED
|
|
else: self._pos_x -= speed[0]
|
|
if(self._pos_x <= -self._size_rect_x):
|
|
self._pos_x = self.__mainscreensize[0]
|
|
|
|
elif(self.__movedirection['right']):
|
|
if(not speed): self._pos_x += MOVE_SPEED
|
|
else: self._pos_x += speed[0]
|
|
if(self._pos_x >= self.__mainscreensize[0]):
|
|
self._pos_x = -self._size_rect_x
|
|
|
|
|
|
class GOIMAGE(GameObject):
|
|
def __init__(self, name: str, surface, mainscreensize: tuple, posx_init: int = 0, posy_init: int = 0, move_speed: int = 0, is_player: bool = False, is_rect: bool = True, color: tuple = (0, 0, 0), rect_size: tuple = (10, 10), image=str()) -> None:
|
|
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, color, rect_size, image)
|
|
|
|
self.__image = pygame.image.load(image).convert()
|
|
|
|
def move(self):
|
|
pass
|
|
|
|
def draw(self):
|
|
surface = self.get_surface()
|
|
surface.blit(self.__image, self.get_position())
|
|
pass
|
|
|
|
|