Mercurial > touhou
annotate pytouhou/game/game.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 | ea21bb37febe |
children | 5cf927cbd9c5 |
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 | 18 from pytouhou.vm.eclrunner import ECLMainRunner |
19 | |
130 | 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 |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
22 from pytouhou.game.item import Item |
97 | 23 |
24 | |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
25 class GameState(object): |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
26 __slots__ = ('resource_loader', 'bullets', 'items', 'players', 'rank', 'difficulty', 'frame', |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
27 'stage', 'boss', 'prng', 'bullet_types', 'item_types', 'characters', 'nb_bullets_max') |
130 | 28 def __init__(self, resource_loader, players, stage, rank, difficulty, |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
29 bullet_types, item_types, characters, nb_bullets_max): |
97 | 30 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
|
31 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
32 self.bullet_types = bullet_types |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
33 self.item_types = item_types |
130 | 34 self.characters = characters |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
35 |
106 | 36 self.bullets = [] |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
37 self.items = [] |
143 | 38 self.nb_bullets_max = nb_bullets_max |
106 | 39 |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
40 self.stage = stage |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
41 self.players = players |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
42 self.rank = rank |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
43 self.difficulty = difficulty |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
44 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
|
45 self.prng = Random() |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
46 self.frame = 0 |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
47 |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
48 |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
49 def change_bullets_into_star_items(self): |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
50 player = self.players[0] #TODO |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
51 item_type = self.item_types[6] |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
52 self.items.extend(Item((bullet.x, bullet.y), item_type, 0.0, item_type.speed, player, self) for bullet in self.bullets) |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
53 self.bullets = [] |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
54 |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
55 |
97 | 56 |
57 class Game(object): | |
130 | 58 def __init__(self, resource_loader, player_states, stage, rank, difficulty, |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
59 bullet_types, item_types, characters, nb_bullets_max=None): |
130 | 60 self.game_state = GameState(resource_loader, player_states, stage, |
61 rank, difficulty, | |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
62 bullet_types, item_types, characters, nb_bullets_max) |
97 | 63 |
130 | 64 self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states] |
97 | 65 self.enemies = [] |
66 | |
67 self.bonuses = [] | |
68 | |
69 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage, | |
70 'stg%denm2.anm' % stage)) | |
71 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage) | |
72 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state) | |
73 | |
74 | |
134
e9ac3640280b
Add support for enemy spawnling enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
130
diff
changeset
|
75 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
|
76 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state, pop_enemy) |
97 | 77 self.enemies.append(enemy) |
78 return enemy | |
79 | |
80 | |
81 def run_iter(self, keystate): | |
82 # 1. VMs. | |
83 self.ecl_runner.run_iter() | |
84 | |
85 # 2. Filter out destroyed enemies | |
123 | 86 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
|
87 |
97 | 88 # 3. Let's play! |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
89 #TODO: check update orders |
130 | 90 for player in self.players: |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
91 player.update(keystate) #TODO: differentiate keystates (multiplayer mode) |
130 | 92 if player.state.x < 8.: |
93 player.state.x = 8. | |
94 if player.state.x > 384.-8: #TODO | |
95 player.state.x = 384.-8 | |
96 if player.state.y < 16.: | |
97 player.state.y = 16. | |
98 if player.state.y > 448.-16: #TODO | |
99 player.state.y = 448.-16 | |
100 | |
97 | 101 for enemy in self.enemies: |
102 enemy.update() | |
106 | 103 |
104 for bullet in self.game_state.bullets: | |
97 | 105 bullet.update() |
106 | |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
107 for item in self.game_state.items: |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
108 item.update() |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
109 |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
110 # 4. Check for collisions! |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
111 #TODO |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
112 for player in self.players: |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
113 px, py = player.x, player.y |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
114 phalf_size = player.hitbox_half_size |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
115 px1, px2 = px - phalf_size, px + phalf_size |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
116 py1, py2 = py - phalf_size, py + phalf_size |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
117 for bullet in self.game_state.bullets: |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
118 half_size = bullet.hitbox_half_size |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
119 bx, by = bullet.x, bullet.y |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
120 bx1, bx2 = bx - half_size, bx + half_size |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
121 by1, by2 = by - half_size, by + half_size |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
122 |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
123 if not (bx2 < px1 or bx1 > px2 |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
124 or by2 < py1 or by1 > py2): |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
125 print('collided!') #TODO |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
126 |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
127 #TODO: enemy-player collision |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
128 #TODO: item-player collision |
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
143
diff
changeset
|
129 |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
134
diff
changeset
|
130 # 5. Cleaning |
97 | 131 self.cleanup() |
132 | |
133 self.game_state.frame += 1 | |
134 | |
135 | |
136 def cleanup(self): | |
137 # Filter out non-visible enemies | |
138 for enemy in tuple(self.enemies): | |
139 if enemy.is_visible(384, 448): #TODO | |
140 enemy._was_visible = True | |
141 elif enemy._was_visible: | |
142 # Filter out-of-screen enemy | |
143 enemy._removed = True | |
144 self.enemies.remove(enemy) | |
145 | |
146 # Filter out-of-scren bullets | |
147 # TODO: was_visible thing | |
123 | 148 self.game_state.bullets = [bullet for bullet in self.game_state.bullets if bullet.is_visible(384, 448)] |
97 | 149 |
150 # Disable boss mode if it is dead/it has timeout | |
151 if self.game_state.boss and self.game_state.boss._removed: | |
152 self.game_state.boss = None | |
153 |