Reputation: 259
I am currently working on a space inavders game in kivy. I have a spaceship and it shoots bullets to the enemies. The enemies move to the right and when they hit the border they turn left and it happens again and again. Everything works perfect. I can shoot and enemy is moving. But the problem is I cant create multiple enemies. I have to put a own class for every enemy and write the same code again and again. My idea was that I will create a enemy list and then display them from the list . So that it looks like there are multiple enemies. But I have no ideas how I could do it and I am failing to do it from one day.Also when you another ideas, it would be great if you suggest them. Here is my code.
The enemy class:
class Enemy(Widget):
def collision(self,ball,enemy,enemy1):
if self.collide_widget(ball):
enemy.y = randint(300,500)
enemy1.y = randint(300,500)
ball.x = -1000
ball.y = self.height / self.width
My Main Game Class:
class SapceGame(Widget):
enemy = ObjectProperty(None)
enemy = ObjectProperty(None)
x_change = NumericProperty(3)
y_change = NumericProperty(-50)
def enemy_movement(self, *args):
self.enemy.x -= self.x_change
if self.enemy.x >= self.width -64:
self.x_change = 3
self.enemy.y += self.y_change
elif self.enemy.x <=0:
self.x_change = -3
self.enemy.y += self.y_change
elif self.enemy.y < 0:
print('Game Over')
def update(self,dt):
self.enemy_movement()
My kv file:
<Enemy>:
size: 64,64
canvas:
Rectangle:
pos:self.pos
size: self.size
source:'alien.png'
<SpaceGame>:
ball: pong_ball
ship: space_ship
enemy: enemy_ship
Enemy:
id:enemy_ship
x:root.random_generator(2,root.width-64)
y:root.random_generator(300,550)
the App class
class SpaceApp(App):
def build(self):
game = PongGame()
# pro second 60 frames are shown
Clock.schedule_interval(game.update, 1.0/60.0)
return game
SpaceApp().run()
Now when I want to create more enemies I have to write the same code again and again.
Upvotes: 2
Views: 205
Reputation: 38962
One of the main concepts of object oriented programming is that each object handles its own behavior. So, your Enemy
object should know how to move. Then, you just need to keep a list of Enemy
objects, and tell each one to do its move behavior on each update.
Here is modified version of your code that does that:
from random import randint
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import ObjectProperty, NumericProperty, ListProperty
from kivy.uix.widget import Widget
class Enemy(Widget):
x_change = NumericProperty(3)
y_change = NumericProperty(-50)
def collision(self,ball,enemy,enemy1):
if self.collide_widget(ball):
enemy.y = randint(300,500)
enemy1.y = randint(300,500)
ball.x = -1000
ball.y = self.height / self.width
def move(self, *args):
self.x -= self.x_change
if self.x >= self.parent.width -64:
self.x_change = 3
self.y += self.y_change
elif self.x <=0:
self.x_change = -3
self.y += self.y_change
if self.y < 0:
print('Game Over')
App.get_running_app().stop_game()
class SpaceGame(Widget):
enemies = ListProperty([])
def add_enemy(self, *args):
enemy = Enemy()
enemy.pos = (randint(0, self.width - 64), self.height - 64)
self.add_widget(enemy)
self.enemies.append(enemy)
def update(self,dt):
for enemy in self.enemies:
enemy.move()
Builder.load_string('''
<Enemy>:
size_hint: None, None
size: 64,64
canvas:
Rectangle:
pos:self.pos
size: self.size
source:'alien.png'
''')
class SpaceApp(App):
def build(self):
game = SpaceGame()
# add enemies at 5 second intervals
self.new_enemy_event = Clock.schedule_interval(game.add_enemy, 5)
# pro second 60 frames are shown
self.updateEvent = Clock.schedule_interval(game.update, 1.0/60.0)
return game
def stop_game(self):
self.updateEvent.cancel()
self.new_enemy_event.cancel()
SpaceApp().run()
Upvotes: 2