Some chagens on GameOjects class
Code cleanup Some stability tests
This commit is contained in:
@@ -3,8 +3,8 @@ import random as rand
|
||||
|
||||
class GameObject():
|
||||
__color = (0,0,0)
|
||||
__size_rect_x:int
|
||||
__size_rect_y:int
|
||||
_size_rect_x:int
|
||||
_size_rect_y:int
|
||||
__movespeed = int()
|
||||
__rectobjekt:pygame.Rect
|
||||
__surface:pygame.Surface
|
||||
@@ -16,21 +16,19 @@ class GameObject():
|
||||
__has_follower = False
|
||||
__score:int
|
||||
count = 0
|
||||
|
||||
__movedirection = {"up":False, "down":False, "left":False, "right":False}
|
||||
|
||||
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, rect_size:tuple = (10,10), image = str()) -> None:
|
||||
self.__name = name
|
||||
self.__pos_x = posx_init
|
||||
self.__pos_y = posy_init
|
||||
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._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
|
||||
@@ -39,88 +37,12 @@ class GameObject():
|
||||
self.__score = 0
|
||||
self.__imagepath = image
|
||||
self.__imageloaded = 0
|
||||
if(len(self.__imagepath) > 0):
|
||||
print(self.__imagepath)
|
||||
test = pygame.image.load(self.__imagepath).convert()
|
||||
self.__surface.blit(test, (10,10))
|
||||
|
||||
|
||||
|
||||
#self.__rectobjekt = rect
|
||||
|
||||
def change_direction(self, direction:dict):
|
||||
if(direction):
|
||||
for key in self.__movedirection.keys():
|
||||
self.__movedirection[key] = False
|
||||
self.__movedirection[direction] = True
|
||||
print(direction)
|
||||
pass
|
||||
|
||||
def move(self, *speed:int):
|
||||
MOVE_SPEED = 1
|
||||
#self.__rectobjekt.x += MOVE_SPEED
|
||||
|
||||
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
|
||||
|
||||
|
||||
def move_in_steps(self, *speed:int, steps=10):
|
||||
MOVE_SPEED = 1
|
||||
#self.__rectobjekt.x += MOVE_SPEED
|
||||
|
||||
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
|
||||
|
||||
|
||||
#print("Objekt-Koordinaten "+self.__name+"= X: "+str(self.__rectobjekt.x)+" Y: "+str(self.__rectobjekt.y), end="\r")
|
||||
|
||||
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])
|
||||
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):
|
||||
@@ -129,14 +51,14 @@ class GameObject():
|
||||
|
||||
def get_position(self, debug=False):
|
||||
if(debug):
|
||||
print((self.__pos_x, self.__pos_y))
|
||||
return (self.__pos_x, self.__pos_y)
|
||||
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)
|
||||
return (self._size_rect_x, self._size_rect_y)
|
||||
|
||||
def get_movespeed(self):
|
||||
return self.__movespeed
|
||||
@@ -163,8 +85,8 @@ class GameObject():
|
||||
self.__collided = True
|
||||
|
||||
def set_position(self, pos:tuple):
|
||||
self.__pos_x = pos[0]
|
||||
self.__pos_y = pos[1]
|
||||
self._pos_x = pos[0]
|
||||
self._pos_y = pos[1]
|
||||
|
||||
def update_pos_lastframe(self,x,y,debug=False):
|
||||
if (debug):
|
||||
@@ -176,7 +98,6 @@ class GameObject():
|
||||
if(score_from_entity > 0):
|
||||
self.__score += score_from_entity
|
||||
|
||||
|
||||
def collide(self, GameObject) -> bool:
|
||||
#print(GameObject)
|
||||
#print(GameObject.get_rect())
|
||||
@@ -224,12 +145,57 @@ class Block(GameObject):
|
||||
|
||||
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, rect_size: tuple = (10, 10)) -> None:
|
||||
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, rect_size)
|
||||
|
||||
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, rect_size: tuple = (10, 10), image=str()) -> None:
|
||||
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, 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, rect_size: tuple = (10, 10), image=str()) -> None:
|
||||
super().__init__(name, surface, mainscreensize, posx_init, posy_init, move_speed, is_player, is_rect, rect_size, image)
|
||||
@@ -240,7 +206,6 @@ class GOIMAGE(GameObject):
|
||||
pass
|
||||
|
||||
def draw(self):
|
||||
print(self.get_surface())
|
||||
surface = self.get_surface()
|
||||
surface.blit(self.__image, self.get_position())
|
||||
pass
|
||||
|
||||
@@ -67,27 +67,23 @@ menu.mainloop(MAINSCREEN)
|
||||
def spawn_entities(x:int, list_of_objects:list):
|
||||
count = 0
|
||||
for y in range(x):
|
||||
rect_size = 60
|
||||
rect_size = 80
|
||||
Entity = Block("Entitiy-"+str(count), MAINSCREEN, MAINSCREEN_SIZE, rand.randint(1,MAINSCREEN_SIZE[0]), rand.randint(1,MAINSCREEN_SIZE[1]), rand.randint(1,15), is_rect=True, rect_size=(rect_size,rect_size))
|
||||
Entity.get_name(True)
|
||||
list_of_objects.append(Entity)
|
||||
count += 1
|
||||
|
||||
player = Player("Player", MAINSCREEN, MAINSCREEN_SIZE, 10, 10, 10, is_player=False, is_rect=True, rect_size=(50,50))
|
||||
|
||||
testobj = GameObject("TESTOBJEKT", MAINSCREEN, MAINSCREEN_SIZE, 0,0,0,image="data/models/testmodel.bmp")
|
||||
test = pygame.image.load("data/models/testmodel.bmp").convert()
|
||||
player = Player("Player", MAINSCREEN, MAINSCREEN_SIZE, 10, 10, 10, is_player=False, is_rect=True, rect_size=(80,80))
|
||||
|
||||
goo = GOIMAGE("TESTOBJEKT", MAINSCREEN, MAINSCREEN_SIZE, 0,0,0,image="data/models/testmodel.bmp")
|
||||
|
||||
background = pygame.surface.Surface((640,480))
|
||||
|
||||
|
||||
x = 3600
|
||||
invert = False
|
||||
colided_objects = list()
|
||||
spawned_entities = list()
|
||||
spawn_entities(30, spawned_entities)
|
||||
spawn_entities(150, spawned_entities)
|
||||
count_hidden_entities = 0
|
||||
count_spawend_enities = len(spawned_entities)
|
||||
print("Es wurden " + str(count_spawend_enities)+ " Objekte gespawnt")
|
||||
@@ -105,21 +101,18 @@ while active == True:
|
||||
active = False
|
||||
elif (event.type == pygame.KEYDOWN):
|
||||
if (event.key == pygame.K_UP):
|
||||
print("Keydown")
|
||||
#print("Keydown")
|
||||
player.change_direction("up")
|
||||
elif (event.key == pygame.K_DOWN):
|
||||
print("Keydown")
|
||||
#print("Keydown")
|
||||
player.change_direction("down")
|
||||
elif (event.key == pygame.K_RIGHT):
|
||||
print("Keydown")
|
||||
#print("Keydown")
|
||||
player.change_direction("right")
|
||||
elif (event.key == pygame.K_LEFT):
|
||||
print("Keydown")
|
||||
#print("Keydown")
|
||||
player.change_direction("left")
|
||||
if(x > 0):
|
||||
testx += 1
|
||||
if(testx >= MAINSCREEN_SIZE[0]): testx = (0 - test.get_size()[0])
|
||||
MAINSCREEN.blit(test, (testx,testy))
|
||||
|
||||
goo.draw()
|
||||
|
||||
player.draw()
|
||||
|
||||
Reference in New Issue
Block a user