forked from bdilly/lastcity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaceship.py
executable file
·71 lines (63 loc) · 2.37 KB
/
spaceship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------
# Author:
# Bruno Dilly <[email protected]>
#
# Copyright (C) 2008 Bruno Dilly
#
# Released under GNU GPL, read the file 'COPYING' for more information
# ----------------------------------------------------------------------
import pygame
from game_entity import GameEntity
from world import World
from vector import Vector
class SpaceShip(GameEntity):
def __init__(self, world, image, location, speed):
GameEntity.__init__(self, world, "spaceship", image, location, speed)
self.altitude = self.world.altitude_max
self.target = None
self.set_destination()
def set_destination(self):
x, y = self.location
w, h = self.image.get_size()
ww, wh = self.world.size
if x > 0:
self.destination = Vector(0 - w/2, y)
else:
self.destination = Vector(ww + w/2, y)
if self.altitude == 0:
self.target = self.world.get_next_target()
if self.target:
# need to destroy the next target
tx, ty = self.target.location
self.future_destination = self.destination
self.destination = Vector(tx, y)
def act(self, time_passed):
GameEntity.act(self, time_passed)
if self.location == self.destination:
self.change_destination()
def change_destination(self):
if self.altitude == 0:
if self.target:
# it reached the target
self.fire(self.target)
self.target = None
self.destination = self.future_destination
else:
self.destroy()
elif not self.world.altitudes[self.altitude-1][1]:
x, y = self.location
self.world.altitudes[self.altitude][1] = False
self.altitude -= 1
self.world.altitudes[self.altitude][1] = True
self.location = Vector(x, self.world.altitudes[self.altitude][0])
self.set_destination()
# flip the image
self.image = pygame.transform.flip(self.image, 1, 0)
def fire(self, target):
# FIXME
self.world.remove_target(target)
def destroy(self):
self.world.altitudes[self.altitude][1] = False
GameEntity.destroy(self)