-
Notifications
You must be signed in to change notification settings - Fork 8
/
gameField.py
218 lines (194 loc) · 7.46 KB
/
gameField.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
# Screaming Strike game field
# Copyright (C) 2019 Yukio Nozawa <[email protected]>
# License: GPL V2.0 (See copying.txt for details)
import datetime
import random
import bgtsound
import bonusCounter
import collection
import enemy
import gameModes
import globalVars
import item
import itemConstants
import itemVoicePlayer
import player
import window
class GameField():
def initialize(self, x, y, mode, voice, easter=False):
self.gameTimer = window.Timer()
self.paused = False
self.easter = easter
self.logs = []
self.log(_("Game started at %(startedtime)s!") % {"startedtime": datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")})
self.x = x
self.y = y
self.setModeHandler(mode)
self.leftPanningLimit = -100
self.rightPanningLimit = 100
self.lowVolumeLimit = -30
self.highVolumeLimit = 0
self.level = 1
if self.easter:
self.level = 10
self.enemies = []
self.items = []
for i in range(self.level):
self.enemies.append(None)
self.player = player.Player()
self.player.initialize(self)
self.collectionCounter = collection.CollectionCounter()
self.collectionCounter.initialize(globalVars.appMain.collectionStorage)
self.defeats = 0
self.nextLevelup = self.modeHandler.calculateNextLevelup()
self.levelupBonus = bonusCounter.BonusCounter()
self.levelupBonus.initialize()
self.destructing = False
self.destructTimer = window.Timer()
self.itemVoicePlayer = itemVoicePlayer.ItemVoicePlayer()
self.itemVoicePlayer.initialize(voice)
self.destructPowerup = bgtsound.sound()
self.destructPowerup.load(globalVars.appMain.sounds["destructPowerup.ogg"])
self.destruct = bgtsound.sound()
self.destruct.load(globalVars.appMain.sounds["destruct.ogg"])
def setModeHandler(self, mode):
self.modeHandler = gameModes.getModeHandler(mode)
self.modeHandler.initialize(self)
self.log(
_("Playing: %(mode)s, high score: %(highscore)d.") % {
"mode": _(
self.modeHandler.getName() + " mode"),
"highscore": globalVars.appMain.statsStorage.get(
"hs_" + self.modeHandler.getName())})
def setLimits(self, lpLimit, rpLimit):
self.leftPanningLimit = lpLimit
self.rightPanningLimit = rpLimit
def frameUpdate(self):
if globalVars.appMain.keyPressed(window.K_s):
globalVars.appMain.say("%.1f" % self.player.score)
self.collectionCounter.frameUpdate()
self.modeHandler.frameUpdate()
self.levelupBonus.frameUpdate()
for elem in self.items[:]:
if elem is not None and elem.state == itemConstants.STATE_SHOULDBEDELETED:
self.items.remove(elem)
if elem is not None:
elem.frameUpdate()
if self.destructing:
if self.destructTimer.elapsed >= 1800:
self.performDestruction()
return True
# end if destruct
self.player.frameUpdate()
if self.player.lives <= 0:
self.log(_("Game over! Final score: %(score)d") % {"score": self.player.score})
return False
# end if
for i in range(self.level):
if self.enemies[i] is not None and self.enemies[i].state == enemy.STATE_SHOULDBEDELETED:
self.enemies[i] = None
if self.enemies[i] is not None:
self.enemies[i].frameUpdate()
if self.enemies[i] is None:
self.spawnEnemy(i)
# end for
return True
def spawnEnemy(self, slot):
e = enemy.Enemy()
if self.easter:
e.initialize(self, random.randint(0, self.x - 1), random.randint(300, 900), random.randint(90, 91))
else:
e.initialize(self, random.randint(0, self.x - 1), random.randint(300, 900), random.randint(0, globalVars.appMain.getNumScreams() - 1))
self.enemies[slot] = e
def logDefeat(self):
self.defeats += 1
self.nextLevelup -= 1
if self.nextLevelup == 0:
self.levelup()
def log(self, s):
self.logs.append(s)
def getLog(self):
"""Retrieves the list in which the log is written.
:rtype: list
"""
return self.logs
def exportLog(self):
l = self.logs[:]
l.append("")
return "\n".join(l)
def levelup(self):
self.log(_("Leveled up to %(newlevel)d! (Accuracy %(accuracy).1f%%, with %(lives)d hp remaining)") %
{"newlevel": self.level + 1, "accuracy": self.player.hitPercentage, "lives": self.player.lives})
self.processLevelupBonus()
self.level += 1
self.enemies.append(None)
self.nextLevelup = self.modeHandler.calculateNextLevelup()
globalVars.appMain.changeMusicPitch_relative(2)
def processLevelupBonus(self):
if not self.modeHandler.allowLevelupBonus:
return
self.player.addScore(self.player.hitPercentage * self.player.hitPercentage * self.level * self.player.lives * 0.5)
self.levelupBonus.start(int(self.player.hitPercentage * 0.1))
def getCenterPosition(self):
if self.x % 2 == 0:
return int((self.x / 2) + 1)
else:
return int(self.x / 2)
def getPan(self, pos):
return self.leftPanningLimit + (self.rightPanningLimit - self.leftPanningLimit) / (self.x - 1) * pos
def getVolume(self, pos):
return self.highVolumeLimit - (self.highVolumeLimit - self.lowVolumeLimit) / self.y * pos
def getPitch(self, y):
return 70 + (y * 3)
def getX(self):
return self.x
def getY(self):
return self.y
def abort(self):
"""aborts the gameplay."""
self.log(_("Game aborted."))
self.clear()
def clear(self):
self.enemies = []
self.items = []
def startDestruction(self):
if self.destructing:
return False
self.destructPowerup.play()
self.destructTimer.restart()
self.destructing = True
return True
def performDestruction(self):
self.destruct.play()
self.log(_("Activating destruction!"))
for elem in self.enemies:
if elem is not None and elem.state == enemy.STATE_ALIVE:
elem.hit()
self.logDefeat()
for elem in self.items:
if elem.type == itemConstants.TYPE_NASTY:
elem.destroy()
else:
elem.obtain()
self.player.processItemHit(elem)
self.destructing = False
self.log(_("End destruction!"))
# end class GameField
def setPaused(self, p):
"""Pauses / unpauses this field."""
if p == self.paused:
return
self.paused = p
self.destructPowerup.setPaused(p)
self.destruct.setPaused(p)
for elem in self.enemies:
if elem:
elem.setPaused(p)
# end enemies
for elem in self.items:
elem.setPaused(p)
# end items
self.player.setPaused(p)
self.destructTimer.setPaused(p)
self.gameTimer.setPaused(p)