Skip to content
Snippets Groups Projects
Commit 4a44b08b authored by Jittat Fakcharoenphol's avatar Jittat Fakcharoenphol
Browse files

random astroids

parent 94e351fa
No related branches found
No related tags found
No related merge requests found
import arcade.key
from random import randint
from random import randint, random
class Model:
def __init__(self, world, x, y, angle):
......@@ -51,9 +51,34 @@ class Gold(Model):
def random_location(self):
self.x = randint(0, self.world.width - 1)
self.y = randint(0, self.world.height - 1)
class Asteroid(Model):
def __init__(self, world, x, y, vx, vy):
super().__init__(world, x, y, 0)
self.vx = vx
self.vy = vy
self.angle = randint(0,359)
def random_direction(self):
self.vx = 5 * random()
self.vy = 5 * random()
def animate(self, delta):
if (self.x < 0) or (self.x > self.world.width):
self.vx = - self.vx
if (self.y < 0) or (self.y > self.world.height):
self.vy = - self.vy
self.x += self.vx
self.y += self.vy
self.angle += 3
class World:
NUM_ASTEROID = 10
def __init__(self, width, height):
self.width = width
self.height = height
......@@ -61,15 +86,29 @@ class World:
self.ship = Ship(self, 100, 100)
self.gold = Gold(self, 400, 400)
self.asteroids = []
for i in range(World.NUM_ASTEROID):
asteroid = Asteroid(self, 0, 0, 0, 0)
asteroid.random_direction()
self.asteroids.append(asteroid)
self.score = 0
def animate(self, delta):
self.ship.animate(delta)
if self.ship.hit(self.gold, 10):
self.gold.random_location()
self.score += 1
for asteroid in self.asteroids:
asteroid.animate(delta)
if self.ship.hit(asteroid, 10):
self.score -= 1
asteroid.x = 0
asteroid.y = 0
asteroid.random_direction()
def on_key_press(self, key, key_modifiers):
if key == arcade.key.SPACE:
self.ship.switch_direction()
......@@ -31,15 +31,21 @@ class SpaceGameWindow(arcade.Window):
self.ship_sprite = ModelSprite('images/ship.png',model=self.world.ship)
self.gold_sprite = ModelSprite('images/gold.png',model=self.world.gold)
self.asteroid_sprites = []
for asteroid in self.world.asteroids:
self.asteroid_sprites.append(ModelSprite('images/ship.png',scale=0.5,model=asteroid))
def on_draw(self):
arcade.start_render()
self.gold_sprite.draw()
self.ship_sprite.draw()
for sprite in self.asteroid_sprites:
sprite.draw()
arcade.draw_text(str(self.world.score),
self.width - 30, self.height - 30,
self.width - 60, self.height - 30,
arcade.color.WHITE, 20)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment