annotate pytouhou/game/item.py @ 150:4f46717390aa

Introduce items, implement ECL instruction 83
author Thibaut Girka <thib@sitedethib.com>
date Tue, 04 Oct 2011 23:09:41 +0200
parents
children 5cf927cbd9c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from math import cos, sin, atan2, pi
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 class Item(object):
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 def __init__(self, pos, item_type, angle, speed, player, game_state):
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 self._game_state = game_state
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 self._sprite = item_type.sprite
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 self._removed = False
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 self._item_type = item_type
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 self.hitbox_half_size = item_type.hitbox_size / 2.
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 self.frame = 0
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 self.player = player
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self.x, self.y = pos
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 self.angle = angle
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 self.speed = speed
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 dx, dy = cos(angle) * speed, sin(angle) * speed
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 self.delta = dx, dy
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self._sprite.angle = angle
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 def update(self):
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 dx, dy = self.delta
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 if self.player is not None:
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 self.angle = atan2(self.player.y - self.y, self.player.x - self.x)
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 else:
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 pass #TODO: item falls!
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 self.x += dx
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 self.y += dy
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52