annotate pytouhou/game/game.py @ 142:c7f0fd9d2145

Simple collision detection
author Thibaut Girka <thib@sitedethib.com>
date Sun, 25 Sep 2011 20:56:14 +0200
parents e9ac3640280b
children ea21bb37febe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
14
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
15
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
16 from pytouhou.utils.random import Random
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
17
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
18 from pytouhou.vm.eclrunner import ECLMainRunner
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
19
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
20 from pytouhou.game.player import Player
98
d141c851c598 Rename pytouhou.game.enemymanager to pytouhou.game.enemy
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
21 from pytouhou.game.enemy import Enemy
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
22
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
23
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 class GameState(object):
106
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
25 __slots__ = ('resource_loader', 'bullets', 'players', 'rank', 'difficulty', 'frame',
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
26 'stage', 'boss', 'prng', 'bullet_types', 'characters')
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
27 def __init__(self, resource_loader, players, stage, rank, difficulty,
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
28 bullet_types, characters):
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
29 self.resource_loader = resource_loader
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
30
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
31 self.bullet_types = bullet_types
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
32 self.characters = characters
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
33
106
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
34 self.bullets = []
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
35
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 self.stage = stage
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 self.players = players
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.rank = rank
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.difficulty = difficulty
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 self.boss = None
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
41 self.prng = Random()
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 self.frame = 0
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
43
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
44
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
45
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
46 class Game(object):
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
47 def __init__(self, resource_loader, player_states, stage, rank, difficulty,
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
48 bullet_types, characters):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
49 self.game_state = GameState(resource_loader, player_states, stage,
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
50 rank, difficulty,
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
51 bullet_types, characters)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
52
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
53 self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states]
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
54 self.enemies = []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
55
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
56 self.bonuses = []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
57
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
58 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage,
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
59 'stg%denm2.anm' % stage))
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
60 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
61 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
62
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
63
134
e9ac3640280b Add support for enemy spawnling enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 130
diff changeset
64 def new_enemy(self, pos, life, instr_type, pop_enemy):
e9ac3640280b Add support for enemy spawnling enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 130
diff changeset
65 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state, pop_enemy)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
66 self.enemies.append(enemy)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
67 return enemy
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
68
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
69
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
70 def run_iter(self, keystate):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
71 # 1. VMs.
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
72 self.ecl_runner.run_iter()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
73
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
74 # 2. Filter out destroyed enemies
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
75 self.enemies = [enemy for enemy in self.enemies if not enemy._removed]
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
76
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
77 # 3. Let's play!
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
78 #TODO: check update orders
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
79 for player in self.players:
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
80 player.update(keystate) #TODO: differentiate keystates (multiplayer mode)
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
81 if player.state.x < 8.:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
82 player.state.x = 8.
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
83 if player.state.x > 384.-8: #TODO
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
84 player.state.x = 384.-8
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
85 if player.state.y < 16.:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
86 player.state.y = 16.
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
87 if player.state.y > 448.-16: #TODO
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
88 player.state.y = 448.-16
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
89
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
90 for enemy in self.enemies:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
91 enemy.update()
106
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
92
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
93 for bullet in self.game_state.bullets:
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
94 bullet.update()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
95
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
96 # 4. Check for collisions!
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
97 #TODO
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
98 for player in self.players:
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
99 px, py = player.x, player.y
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
100 phalf_size = player.hitbox_half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
101 px1, px2 = px - phalf_size, px + phalf_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
102 py1, py2 = py - phalf_size, py + phalf_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
103 for bullet in self.game_state.bullets:
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
104 half_size = bullet.hitbox_half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
105 bx, by = bullet.x, bullet.y
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
106 bx1, bx2 = bx - half_size, bx + half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
107 by1, by2 = by - half_size, by + half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
108
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
109 if not (bx2 < px1 or bx1 > px2
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
110 or by2 < py1 or by1 > py2):
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
111 print('collided!') #TODO
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
112
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
113 # 5. Cleaning
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
114 self.cleanup()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
115
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
116 self.game_state.frame += 1
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
117
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
118
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
119 def cleanup(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
120 # Filter out non-visible enemies
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
121 for enemy in tuple(self.enemies):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
122 if enemy.is_visible(384, 448): #TODO
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
123 enemy._was_visible = True
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
124 elif enemy._was_visible:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
125 # Filter out-of-screen enemy
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
126 enemy._removed = True
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
127 self.enemies.remove(enemy)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
128
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
129 # Filter out-of-scren bullets
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
130 # TODO: was_visible thing
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
131 self.game_state.bullets = [bullet for bullet in self.game_state.bullets if bullet.is_visible(384, 448)]
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
132
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
133 # Disable boss mode if it is dead/it has timeout
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
134 if self.game_state.boss and self.game_state.boss._removed:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
135 self.game_state.boss = None
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
136