Changed some classes

This commit is contained in:
Christian
2024-08-08 15:10:10 +02:00
parent 5d42915e56
commit 2171f51145
3 changed files with 36 additions and 18 deletions

View File

@@ -2,16 +2,12 @@ import pygame
import GameObject import GameObject
import Utils import Utils
class Enemy(object): class Enemy(GameObject.GameObject):
def __init__(self, name, pos_x, pos_y, image) -> None: def __init__(self, name, pos_x, pos_y, width, height, image=None) -> None:
spawned_enemy = list() super().__init__(name, pos_x, pos_y, width, height, image)
self.name = name
self.image = image
self.pos_x = pos_x
self.pos_y = pos_y
self.obj = GameObject.GameObject("Enemy", 0, 0, image)
pass
def render(self, screen:pygame.Surface):
screen.blit(self.image, (self.pos_x, self.pos_y))
#def render(self):
# pass

View File

@@ -1,22 +1,34 @@
import pygame import pygame
class GameObject(object): class GameObject(object):
def __init__(self, name, pos_x, pos_y, image=None,) -> None: objects = list()
def __init__(self, name, pos_x, pos_y, width, height, image=None) -> None:
self.name = name self.name = name
self.pos_x = pos_x self.pos_x = pos_x
self.pos_y = pos_y self.pos_y = pos_y
self.width = width
self.height = height
self.image = image self.image = image
self.objects.append(self)
pass pass
def get_objectinfo(self):
print(F"Gameobject-Info: \n",
F"Name: {self.name}\n",
F"Position x: {self.pos_x} y: {self.pos_y}\n",
F"Size = width: {self.width} height: {self.height}"
)
def render(self, screen:pygame.Surface): def render(self, screen:pygame.Surface):
if(self.image is not None): if(self.image is not None):
screen.blit(self.image, (self.pos_x, self.pos_y)) screen.blit(self.image, (self.pos_x, self.pos_y, ))
else: else:
print("Kein Image hinterlegt!") print("Kein Image hinterlegt!")
def setpos(self, pos_x, pos_y): def setpos(self, pos_x, pos_y):
self.pos_x = pos_x self.pos_x = pos_x
self.pos_y = pos_y self.pos_y = pos_y
def setsize(self, width, height):
self.width = width
self.height = height

14
test.py
View File

@@ -19,7 +19,15 @@ testimage = Utils.load_image("Rastergrafik.png")
objects = list() objects = list()
enemy1 = Enemy.Enemy("Penner", 500, 500, image_enemy) c = 0
y = 10
for x in range(10):
objects.append(Enemy.Enemy("Penner", 500, y, 35, 35, image_enemy))
y += 35
print(F"{len(GameObject.GameObject.objects)}")
gamestate = True gamestate = True
@@ -54,13 +62,15 @@ while(gamestate):
#Rendere alle Objecte in objects Liste #Rendere alle Objecte in objects Liste
for object in objects: for object in objects:
object:GameObject.GameObject object:GameObject.GameObject
if(pygame.mouse.get_pressed()[0]): if(pygame.mouse.get_pressed()[0]):
object.setpos(mouse_pos[0], mouse_pos[1]) object.setpos(mouse_pos[0], mouse_pos[1])
object.render(screen) object.render(screen)
enemy1.render(screen)
#if(mouse_pos <= (image.topleft+image.size) and mouse_pos >= image.topleft): #if(mouse_pos <= (image.topleft+image.size) and mouse_pos >= image.topleft):
# print("HIIIIITTT!!!!") # print("HIIIIITTT!!!!")