Skip to content
Snippets Groups Projects
Commit 52a07a68 authored by Jakob Kirsch's avatar Jakob Kirsch
Browse files

asteroids split now

parent 910ffd2a
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3 #!/usr/bin/python3
''' '''
To add: To add:
- fuel canisters - fuel canisters
- shield - shield
''' '''
...@@ -25,226 +25,241 @@ CIRCLE = max(SCREEN_WIDTH, SCREEN_HEIGHT) ...@@ -25,226 +25,241 @@ CIRCLE = max(SCREEN_WIDTH, SCREEN_HEIGHT)
DISTANCE = 128 DISTANCE = 128
CONTROLS = { CONTROLS = {
"run" : arcade.key.W, "run" : arcade.key.W,
"left" : arcade.key.A, "left" : arcade.key.A,
"right" : arcade.key.D, "right" : arcade.key.D,
"shoot" : arcade.key.SPACE "shoot" : arcade.key.SPACE
} }
class Game(arcade.Window): class Game(arcade.Window):
def __init__(self): def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, NAME) super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, NAME)
self.all_sprites_list = None self.all_sprites_list = None
self.asteroid_list = None self.asteroid_list = None
self.shoot_list = None self.shoot_list = None
self.player_list = None self.player_list = None
self.score = 0 self.score = 0
self.running = 0 self.running = 0
self.player_sprite = None self.player_sprite = None
self.time = 0 self.time = 0
self.view_left = 0 self.view_left = 0
self.view_bottom = 0 self.view_bottom = 0
self.slide = False self.slide = False
arcade.set_background_color(arcade.color.BLACK) arcade.set_background_color(arcade.color.BLACK)
# Add asteroid # Add asteroid
def asteroid(self): def asteroid(self):
x = random.randint(int(self.player_sprite.center_x - CIRCLE), int(self.player_sprite.center_x + CIRCLE)) x = random.randint(int(self.player_sprite.center_x - CIRCLE), int(self.player_sprite.center_x + CIRCLE))
y = random.randint(int(self.player_sprite.center_y - CIRCLE), int(self.player_sprite.center_y + CIRCLE)) y = random.randint(int(self.player_sprite.center_y - CIRCLE), int(self.player_sprite.center_y + CIRCLE))
if ((x - self.player_sprite.center_x) ** 2 + (y - self.player_sprite.center_y) ** 2) < (DISTANCE) ** 2: if ((x - self.player_sprite.center_x) ** 2 + (y - self.player_sprite.center_y) ** 2) < (DISTANCE) ** 2:
return return
asteroid_sprite = arcade.Sprite(random.choice(["images/big_asteroid.png"] * 3 + ["images/small_asteroid.png"] * 7), SPRITE_SCALING) asteroid_sprite = arcade.Sprite(random.choice(["images/big_asteroid.png"] * 3 + ["images/small_asteroid.png"] * 7), SPRITE_SCALING)
if asteroid_sprite.texture == arcade.load_texture("images/big_asteroid.png"): if asteroid_sprite.texture == arcade.load_texture("images/big_asteroid.png"):
asteroid_sprite.name = "big" asteroid_sprite.name = "big"
else: else:
asteroid_sprite.name = "small" asteroid_sprite.name = "small"
asteroid_sprite.set_position(x,y) asteroid_sprite.set_position(x,y)
asteroid_sprite.change_x = random.randint(-AS_SPEED, AS_SPEED) / 1000 asteroid_sprite.change_x = random.randint(-AS_SPEED, AS_SPEED) / 1000
asteroid_sprite.change_y = random.randint(-AS_SPEED, AS_SPEED) / 1000 asteroid_sprite.change_y = random.randint(-AS_SPEED, AS_SPEED) / 1000
asteroid_sprite.change_angle = random.randint(-ROT_SPEED, ROT_SPEED) / 1000 asteroid_sprite.change_angle = random.randint(-ROT_SPEED, ROT_SPEED) / 1000
self.all_sprites_list.append(asteroid_sprite) self.all_sprites_list.append(asteroid_sprite)
self.asteroid_list.append(asteroid_sprite) self.asteroid_list.append(asteroid_sprite)
def setup(self): def setup(self):
self.score = 0 self.score = 0
self.running = 0 self.running = 0
self.view_left = 0 self.view_left = 0
self.view_bottom = 0 self.view_bottom = 0
self.slide = False self.slide = False
arcade.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT) arcade.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
self.low_x = 0 self.low_x = 0
self.low_y = 0 self.low_y = 0
self.slide = False self.slide = False
self.all_sprites_list = arcade.SpriteList() self.all_sprites_list = arcade.SpriteList()
self.asteroid_list = arcade.SpriteList() self.asteroid_list = arcade.SpriteList()
self.shoot_list = arcade.SpriteList() self.shoot_list = arcade.SpriteList()
self.player_list = arcade.SpriteList() self.player_list = arcade.SpriteList()
self.player_sprite = arcade.AnimatedWalkingSprite(SPRITE_SCALING) self.player_sprite = arcade.AnimatedWalkingSprite(SPRITE_SCALING)
self.player_sprite.textures.append(arcade.load_texture("images/player_off.png")) self.player_sprite.textures.append(arcade.load_texture("images/player_off.png"))
self.player_sprite.textures.append(arcade.load_texture("images/player_on.png")) self.player_sprite.textures.append(arcade.load_texture("images/player_on.png"))
self.player_sprite.set_texture(0) self.player_sprite.set_texture(0)
self.player_sprite.left = SCREEN_WIDTH / 2 + self.player_sprite.width self.player_sprite.left = SCREEN_WIDTH / 2 + self.player_sprite.width
self.player_sprite.bottom = SCREEN_HEIGHT / 2 + self.player_sprite.height self.player_sprite.bottom = SCREEN_HEIGHT / 2 + self.player_sprite.height
self.player_list.append(self.player_sprite) self.player_list.append(self.player_sprite)
for i in range(0, 5000): for i in range(0, 5000):
star = arcade.Sprite("images/star.png", SPRITE_SCALING) star = arcade.Sprite("images/star.png", SPRITE_SCALING)
star.set_position(random.randint(int(-10000), int(10000)), random.randint(int(-10000), int(10000))) star.set_position(random.randint(int(-10000), int(10000)), random.randint(int(-10000), int(10000)))
self.all_sprites_list.append(star) self.all_sprites_list.append(star)
def update(self, delta_time): def update(self, delta_time):
self.player_list.update() self.player_list.update()
self.time += 1 self.time += 1
if self.time % 50 == 0: if self.time % 50 == 0:
self.asteroid() self.asteroid()
'''for asteroid in self.asteroid_list: '''for asteroid in self.asteroid_list:
if asteroid.center_x < self.view_left - 300 or asteroid.center_x > self.view_left + SCREEN_WIDTH + 300: if asteroid.center_x < self.view_left - 300 or asteroid.center_x > self.view_left + SCREEN_WIDTH + 300:
self.asteroid_list.remove(asteroid) self.asteroid_list.remove(asteroid)
self.all_sprites_list.remove(asteroid) self.all_sprites_list.remove(asteroid)
if asteroid.center_y < self.view_bottom - 300 or asteroid.center_y > self.view_bottom + SCREEN_HEIGHT + 300: if asteroid.center_y < self.view_bottom - 300 or asteroid.center_y > self.view_bottom + SCREEN_HEIGHT + 300:
self.asteroid_list.remove(asteroid) self.asteroid_list.remove(asteroid)
self.all_sprites_list.remove(asteroid)''' self.all_sprites_list.remove(asteroid)'''
if not self.slide: if not self.slide:
self.player_sprite.change_y = self.running * SPEED * math.cos(math.radians(self.player_sprite.angle)) self.player_sprite.change_y = self.running * SPEED * math.cos(math.radians(self.player_sprite.angle))
self.player_sprite.change_x = self.running * -SPEED * math.sin(math.radians(self.player_sprite.angle)) self.player_sprite.change_x = self.running * -SPEED * math.sin(math.radians(self.player_sprite.angle))
self.all_sprites_list.update() self.all_sprites_list.update()
if self.slide: if self.slide:
low_x = 0.1 low_x = 0.1
low_y = 0.1 low_y = 0.1
check_x = self.player_sprite.change_x check_x = self.player_sprite.change_x
check_y = self.player_sprite.change_y check_y = self.player_sprite.change_y
if self.player_sprite.change_x < 0: if self.player_sprite.change_x < 0:
low_x *= -1 low_x *= -1
check_x *= -1 check_x *= -1
if self.player_sprite.change_y < 0: if self.player_sprite.change_y < 0:
low_y *= -1 low_y *= -1
check_y *=-1 check_y *=-1
if math.fabs(self.player_sprite.change_x) > 0: if math.fabs(self.player_sprite.change_x) > 0:
self.player_sprite.change_x /= SLIDE_SPEED self.player_sprite.change_x /= SLIDE_SPEED
if math.fabs(self.player_sprite.change_y) > 0: if math.fabs(self.player_sprite.change_y) > 0:
self.player_sprite.change_y /= SLIDE_SPEED self.player_sprite.change_y /= SLIDE_SPEED
if math.fabs(self.player_sprite.change_x) < low_x: if math.fabs(self.player_sprite.change_x) < low_x:
self.player_sprite.change_x = 0 self.player_sprite.change_x = 0
if math.fabs(self.player_sprite.change_y) < low_y: if math.fabs(self.player_sprite.change_y) < low_y:
self.player_sprite.change_y = 0 self.player_sprite.change_y = 0
if math.fabs(check_x) < low_x and math.fabs(check_y) < low_y: if math.fabs(check_x) < low_x and math.fabs(check_y) < low_y:
self.player_sprite.change_x = 0 self.player_sprite.change_x = 0
self.player_sprite.change_y = 0 self.player_sprite.change_y = 0
self.slide = False self.slide = False
changed = False changed = False
# Scroll left # Scroll left
left_bndry = self.view_left + VIEWPORT_MARGIN left_bndry = self.view_left + VIEWPORT_MARGIN
if self.player_sprite.left < left_bndry: if self.player_sprite.left < left_bndry:
self.view_left -= left_bndry - self.player_sprite.left self.view_left -= left_bndry - self.player_sprite.left
changed = True changed = True
# Scroll right # Scroll right
right_bndry = self.view_left + SCREEN_WIDTH - RIGHT_MARGIN right_bndry = self.view_left + SCREEN_WIDTH - RIGHT_MARGIN
if self.player_sprite.right > right_bndry: if self.player_sprite.right > right_bndry:
self.view_left += self.player_sprite.right - right_bndry self.view_left += self.player_sprite.right - right_bndry
changed = True changed = True
# Scroll up # Scroll up
top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN
if self.player_sprite.top > top_bndry: if self.player_sprite.top > top_bndry:
self.view_bottom += self.player_sprite.top - top_bndry self.view_bottom += self.player_sprite.top - top_bndry
changed = True changed = True
# Scroll down # Scroll down
bottom_bndry = self.view_bottom + VIEWPORT_MARGIN bottom_bndry = self.view_bottom + VIEWPORT_MARGIN
if self.player_sprite.bottom < bottom_bndry: if self.player_sprite.bottom < bottom_bndry:
self.view_bottom -= bottom_bndry - self.player_sprite.bottom self.view_bottom -= bottom_bndry - self.player_sprite.bottom
changed = True changed = True
# If we need to scroll, go ahead and do it. # If we need to scroll, go ahead and do it.
if changed: if changed:
arcade.set_viewport(self.view_left, arcade.set_viewport(self.view_left,
SCREEN_WIDTH + self.view_left, SCREEN_WIDTH + self.view_left,
self.view_bottom, self.view_bottom,
SCREEN_HEIGHT + self.view_bottom) SCREEN_HEIGHT + self.view_bottom)
# Collision checking # Collision checking
if arcade.check_for_collision_with_list(self.player_sprite, self.asteroid_list): if arcade.check_for_collision_with_list(self.player_sprite, self.asteroid_list):
self.setup() self.setup()
for shoot in self.shoot_list: for shoot in self.shoot_list:
for asteroid in self.asteroid_list: for asteroid in self.asteroid_list:
if arcade.check_for_collision(asteroid, shoot): if arcade.check_for_collision(asteroid, shoot):
if asteroid.name == "big": if asteroid.name == "big":
self.score += 10 for i in range(0, 2):
else: x, y = asteroid.center_x, asteroid.center_y
self.score += 5 asteroid_sprite = arcade.Sprite("images/small_asteroid.png", SPRITE_SCALING)
self.asteroid_list.remove(asteroid) asteroid_sprite.set_position(x,y)
self.all_sprites_list.remove(asteroid) asteroid_sprite.change_x = random.randint(-AS_SPEED, AS_SPEED) / 1000
self.all_sprites_list.remove(shoot) asteroid_sprite.change_y = random.randint(-AS_SPEED, AS_SPEED) / 1000
self.shoot_list.remove(shoot) asteroid_sprite.change_angle = random.randint(-ROT_SPEED, ROT_SPEED) / 1000
asteroid_sprite.name = "small"
def on_draw(self): self.all_sprites_list.append(asteroid_sprite)
arcade.start_render() self.asteroid_list.append(asteroid_sprite)
self.all_sprites_list.draw() self.asteroid_list.remove(asteroid)
self.player_list.draw() self.all_sprites_list.remove(asteroid)
output = f"Score: {self.score}" self.all_sprites_list.remove(shoot)
arcade.draw_text(output, self.view_left + 50, self.view_bottom + SCREEN_HEIGHT - 70, arcade.color.WHITE, 14) self.shoot_list.remove(shoot)
self.score += 2
def on_key_press(self, key, modifier): continue
if key == CONTROLS["run"]: else:
self.slide = False self.score += 5
self.running = 1 self.asteroid_list.remove(asteroid)
self.player_sprite.change_y = SPEED * math.cos(math.radians(self.player_sprite.angle)) self.all_sprites_list.remove(asteroid)
self.player_sprite.change_x = -SPEED * math.sin(math.radians(self.player_sprite.angle)) self.all_sprites_list.remove(shoot)
self.player_sprite.set_texture(1) self.shoot_list.remove(shoot)
if key == CONTROLS["left"]: def on_draw(self):
self.player_sprite.change_angle = ROTATE_SPEED arcade.start_render()
self.all_sprites_list.draw()
self.player_list.draw()
if key == CONTROLS["right"]: output = f"Score: {self.score}"
self.player_sprite.change_angle = -ROTATE_SPEED arcade.draw_text(output, self.view_left + 50, self.view_bottom + SCREEN_HEIGHT - 70, arcade.color.WHITE, 14)
if key == CONTROLS["shoot"]: def on_key_press(self, key, modifier):
shoot = arcade.Sprite("images/laser.png", 0.05) if key == CONTROLS["run"]:
shoot.change_y = SPEED * math.cos(math.radians(self.player_sprite.angle)) * 1.5 self.slide = False
shoot.change_x = -SPEED * math.sin(math.radians(self.player_sprite.angle)) * 1.5 self.running = 1
shoot.angle = self.player_sprite.angle self.player_sprite.change_y = SPEED * math.cos(math.radians(self.player_sprite.angle))
shoot.center_x = self.player_sprite.center_x self.player_sprite.change_x = -SPEED * math.sin(math.radians(self.player_sprite.angle))
shoot.center_y = self.player_sprite.center_y self.player_sprite.set_texture(1)
self.shoot_list.append(shoot)
self.all_sprites_list.append(shoot) if key == CONTROLS["left"]:
sound = arcade.sound.Sound("sounds/shot.wav") self.player_sprite.change_angle = ROTATE_SPEED
sound.play(volume=0.1, pan=((180 < (self.player_sprite.angle) < 360) - (0 < (self.player_sprite.angle) < 180)))
def on_key_release(self, key, modifier): if key == CONTROLS["right"]:
if key == CONTROLS["run"]: self.player_sprite.change_angle = -ROTATE_SPEED
self.running = 0
self.slide = True if key == CONTROLS["shoot"]:
self.player_sprite.set_texture(0) shoot = arcade.Sprite("images/laser.png", 0.05)
if key == CONTROLS["left"]: shoot.change_y = SPEED * math.cos(math.radians(self.player_sprite.angle)) * 1.5
self.player_sprite.change_angle = 0 shoot.change_x = -SPEED * math.sin(math.radians(self.player_sprite.angle)) * 1.5
if key == CONTROLS["right"]: shoot.angle = self.player_sprite.angle
self.player_sprite.change_angle = -0 shoot.center_x = self.player_sprite.center_x
shoot.center_y = self.player_sprite.center_y
self.shoot_list.append(shoot)
self.all_sprites_list.append(shoot)
sound = arcade.sound.Sound("sounds/shot.wav")
sound.play(volume=0.1, pan=((180 < (self.player_sprite.angle) < 360) - (0 < (self.player_sprite.angle) < 180)))
def on_key_release(self, key, modifier):
if key == CONTROLS["run"]:
self.running = 0
self.slide = True
self.player_sprite.set_texture(0)
if key == CONTROLS["left"]:
self.player_sprite.change_angle = 0
if key == CONTROLS["right"]:
self.player_sprite.change_angle = -0
def main(): def main():
game = Game() game = Game()
game.setup() game.setup()
arcade.run() arcade.run()
try: try:
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
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