Changed enemy class
This commit is contained in:
32
Enemy.py
32
Enemy.py
@@ -3,11 +3,37 @@ import GameObject
|
|||||||
import Utils
|
import Utils
|
||||||
|
|
||||||
class Enemy(GameObject.GameObject):
|
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)
|
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
|
||||||
42
test.py
42
test.py
@@ -2,6 +2,7 @@ import pygame
|
|||||||
import GameObject
|
import GameObject
|
||||||
import Enemy
|
import Enemy
|
||||||
import Utils
|
import Utils
|
||||||
|
import random
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
@@ -17,26 +18,7 @@ testimage = Utils.load_image("Rastergrafik.png")
|
|||||||
|
|
||||||
#testimage.set_colorkey((255,0,255), pygame.RLEACCELOK)
|
#testimage.set_colorkey((255,0,255), pygame.RLEACCELOK)
|
||||||
|
|
||||||
objects = list()
|
enemy = Enemy.Enemy("Enemy-1", (screen.get_size()[0]/2) -30,30,35,35, screen,testimage)
|
||||||
|
|
||||||
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)}")
|
|
||||||
|
|
||||||
|
|
||||||
gamestate = True
|
gamestate = True
|
||||||
@@ -69,13 +51,21 @@ while(gamestate):
|
|||||||
pos_y = mouse_pos[1]
|
pos_y = mouse_pos[1]
|
||||||
|
|
||||||
#Rendere alle Objecte in objects Liste
|
#Rendere alle Objecte in objects Liste
|
||||||
for object in objects:
|
enemy.render(screen)
|
||||||
object:GameObject.GameObject
|
|
||||||
|
rand = random.randint(0, 100)
|
||||||
if(pygame.mouse.get_pressed()[0]):
|
|
||||||
object.setpos(mouse_pos[0], mouse_pos[1])
|
if rand == 50:
|
||||||
|
enemy.fire(screen)
|
||||||
|
|
||||||
|
if rand == 20:
|
||||||
|
enemy.move(5)
|
||||||
|
if rand == 50:
|
||||||
|
enemy.move(-5)
|
||||||
|
|
||||||
|
enemy.shoot()
|
||||||
|
|
||||||
|
|
||||||
object.render(screen)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user