Changed enemy class

This commit is contained in:
Christian
2024-08-08 21:30:34 +02:00
parent 874252e94e
commit a990714452
2 changed files with 45 additions and 29 deletions

View File

@@ -3,11 +3,37 @@ import GameObject
import Utils
class Enemy(GameObject.GameObject):
def __init__(self, name, pos_x, pos_y, width, height, image=None) -> None:
shots_fired = list()
def __init__(self, name, pos_x, pos_y, width, height, screen, image=None) -> None:
super().__init__(name, pos_x, pos_y, width, height, image)
def fire(self, screen):
print("Schieße!!!!")
shot = Projectile(" ", self.pos_x+(self.width/2),self.pos_y+self.height,10,5, screen)
self.shots_fired.append(shot)
def shoot(self):
if(len(self.shots_fired) > 0):
for shots in self.shots_fired:
shots:Projectile
shots.animate()
def move(self, x=0, y=0):
if(x != 0):
self.pos_x += x
if(y != 0):
self.pos_y += y
#def render(self):
# pass
class Projectile(GameObject.GameObject):
def __init__(self, name, pos_x, pos_y, width, height, screen, image=None) -> None:
super().__init__(name, pos_x, pos_y, width, height, image)
self.screen = screen
self.speed = 10
def animate(self):
self.rect = pygame.draw.rect(self.screen, (255,255,0), (self.pos_x, self.pos_y, self.width, self.height))
self.pos_y += self.speed

40
test.py
View File

@@ -2,6 +2,7 @@ import pygame
import GameObject
import Enemy
import Utils
import random
pygame.init()
@@ -17,26 +18,7 @@ testimage = Utils.load_image("Rastergrafik.png")
#testimage.set_colorkey((255,0,255), pygame.RLEACCELOK)
objects = list()
c = 0
y = 10
for x in range(10):
obj = Enemy.Enemy(F"Penner-{x}", 500, y, 35, 35, image_enemy)
objects.append(obj)
obj = GameObject.GameObject("Test", y,0,0,0, testimage)
objects.append(obj)
y += 35
for object in GameObject.GameObject.objects:
object:GameObject
if(object.name == "Penner-9"):
print("Vorletzter!")
object.setpos(400,400)
print(object)
#rint(F"{len(GameObject.GameObject.objects)}")
enemy = Enemy.Enemy("Enemy-1", (screen.get_size()[0]/2) -30,30,35,35, screen,testimage)
gamestate = True
@@ -69,13 +51,21 @@ while(gamestate):
pos_y = mouse_pos[1]
#Rendere alle Objecte in objects Liste
for object in objects:
object:GameObject.GameObject
enemy.render(screen)
rand = random.randint(0, 100)
if rand == 50:
enemy.fire(screen)
if rand == 20:
enemy.move(5)
if rand == 50:
enemy.move(-5)
enemy.shoot()
if(pygame.mouse.get_pressed()[0]):
object.setpos(mouse_pos[0], mouse_pos[1])
object.render(screen)