-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
240 lines (220 loc) · 10.2 KB
/
Game.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import PySimpleGUI as sg
import random
from PIL import Image
import io
import GenerateArt as gna
import GenerateText as gnt
import RNGHash as rng
ttk_style = 'clam'
#The enemy object
class Enemy():
def __init__(self):
self.health = 4
self.name = self.randomName()
self.path = "out.gif"
self.armed = None
self.block_tedency = random.randrange(2,10)
self.block = None;
self.quote = gnt.getRandText() + "\n\n"
#Enemy's random name
def randomName(self):
name = ""
consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','y','z']
vowels = ['a','e','i','o','u']
nameLength = random.randrange(3,10)
for i in range(nameLength):
odds = random.randrange(1,100)
if odds % 2 == 0:
name += consonants[random.randrange(len(consonants))]
else:
name += vowels[random.randrange(len(vowels))]
return name
#Player's object
class Player():
def __init__(self):
self.health = 4
self.armed = None
self.block = None
self.score = 0
#GUI
class GameScreen():
def __init__(self):
self.player = Player()
self.enemy = Enemy()
self.reset()
#Resets GUI
def reset(self):
self.enemy = Enemy()
if self.player.health == 0:
self.player = Player()
else:
score = self.player.score
self.player = Player()
self.player.score = score
gna.start_generator(2)
sg.theme('Black')
self.col1 = [
[sg.Image(key = "-IMAGE-")],
[sg.Text(self.enemy.name.capitalize(), font='sans-serif 20')],
[sg.Text(self.enemy.quote, font='sans-serif 12 italic')],
[sg.Button(' ', disabled = True, key ="-nextenemy-", visible=False, use_ttk_buttons=True)]]
self.col2 = [
self.healthBar("EN",self.enemy.health),#0
[sg.Text("\n\n")],
[sg.Text("Enemy Action", key = '-enemyevent-', font='sans-serif 15', text_color = "red")],
[sg.Text("----------"), sg.Text("Defeat " + self.enemy.name.capitalize(), key = '-outcome-', font='sans-serif 15')],
[sg.Text("Your Action", key = '-playerevent-', font='sans-serif 15', text_color = "#00FF48")],
[sg.Text("\n\n")],
[sg.Text('Score: ' + str(self.player.score), key = '-score-', font='sans-serif 15')],
self.healthBar("US",self.player.health),#5
[sg.Button("Reload", key = '-action-', use_ttk_buttons=True), sg.Button('Block', key = '-block-', use_ttk_buttons=True)],
[sg.Button('Change seed', key = '-seed-', use_ttk_buttons=True)]
]
self.layout = [[sg.Column(self.col1, size=(350, 400), element_justification='center'), sg.Column(self.col2, size=(350, 400))]]
self.window = sg.Window('Shotgun Art Game', self.layout, finalize = True, ttk_theme=ttk_style)
self.loop()
#Healthbar
def healthBar(self, character, health):
data = []
for i in range(4):
if i < health:
data.append(sg.Image(character+"_FULL.png", key = "-" + character + "hbar" + str(i) + "-"))
return data
#Main game loop
def loop(self):
while True:
event, values = self.window.read(timeout = 100)
self.window.Element('-IMAGE-').update_animation(self.enemy.path, time_between_frames=200)
if event == sg.WIN_CLOSED:
self.window.close()
break
elif event == '-nextenemy-':
self.window.close()
self.reset()
elif event == '-seed-':
gna.changeSeedWindow()
else:
if self.player.armed == None:
self.player.armed = False
self.enemy.armed = False
self.enemy.block = False
self.contest(event)
#Player action logic
def playerAction(self, event):
if event == '-action-':
self.player.block = False
if self.player.armed == False:
self.window['-action-'].Update("Attack")
self.window['-playerevent-'].Update("♻️ You reloaded!")
self.player.armed = True
self.enemyAction()
elif self.player.armed == True:
self.window['-action-'].Update("Reload")
self.window['-playerevent-'].Update("⚔️ You attacked!")
self.player.armed = False
self.enemyAction()
elif event == '-block-':
self.player.block = True
self.window['-playerevent-'].Update("🛡 You blocked!")
self.enemyAction()
#Enemy action logic
def enemyAction(self):
action = random.randrange(1,self.enemy.block_tedency+1)
if action == self.enemy.block_tedency:
self.window['-enemyevent-'].Update("🛡 Enemy blocked!")
self.enemy.block = True
else:
self.enemy.block = False
if self.enemy.armed == False:
self.window['-enemyevent-'].Update("♻️ Enemy reloaded!")
self.enemy.armed = True
else:
self.window['-enemyevent-'].Update("⚔️ Enemy attacked!")
self.enemy.armed = False
#Logic for when player takes damage/loses game
def playerHit(self):
if self.player.health - 1 == 0:
self.player.health -= 1
self.player.score -= 50
self.window['-UShbar' + str(self.player.health) +'-'].Update("US_EMPTY.png")
self.window['-outcome-'].Update("😭 You lose! Game Over -50\nFinal Score:" + str(self.player.score))
self.window['-score-'].Update("Score: " + str(self.player.score))
self.window['-nextenemy-'].Update("Play again?")
self.window['-nextenemy-'].Update(disabled = False)
self.window['-nextenemy-'].Update(visible = True)
self.window['-action-'].Update(disabled = True)
self.window['-block-'].Update(disabled = True)
self.player.block = None
self.enemy.block = None
else:
self.player.health -= 1
self.player.score -= 10
self.window['-score-'].Update("Score: " + str(self.player.score))
self.window['-UShbar' + str(self.player.health) +'-'].Update("US_EMPTY.png")
self.playerHbar = self.healthBar("US",self.player.health)
self.player.block = None
self.enemy.block = None
#Logic for when enemy takes damage/player winning
def enemyHit(self):
if self.enemy.health - 1 == 0:
self.enemy.health -= 1
self.player.score += 20
self.window['-ENhbar' + str(self.enemy.health) +'-'].Update("EN_EMPTY.png")
self.window['-outcome-'].Update("💪 You won! +20\nCurrent score: " + str(self.player.score))
self.window['-nextenemy-'].Update(disabled = False)
self.window['-nextenemy-'].Update(visible = True)
self.window['-nextenemy-'].Update("Next enemy?")
self.window['-action-'].Update(disabled = True)
self.window['-block-'].Update(disabled = True)
self.window['-score-'].Update("Score: " + str(self.player.score))
self.player.block = None
self.enemy.block = None
else:
self.enemy.health -= 1
self.player.score += 10
self.window['-score-'].Update("Score: " + str(self.player.score))
self.window['-ENhbar' + str(self.enemy.health) +'-'].Update("EN_EMPTY.png")
self.player.block = None
self.enemy.block = None
#Logic for when player and enemy get hit
def bothHit(self):
if self.player.health - 1 == 0:
self.enemy.health -= 1
self.window['-ENhbar' + str(self.enemy.health) +'-'].Update("EN_EMPTY.png")
self.playerHit()
elif self.enemy.health - 1 == 0:
self.player.health -= 1
self.window['-UShbar' + str(self.player.health) +'-'].Update("US_EMPTY.png")
self.enemyHit()
else:
self.enemyHit()
self.playerHit()
#Decide outcome based on player and enemy moves
def contest(self, event):
self.playerAction(event)
if self.enemy.block == True and self.player.block == True: #both players block
self.window['-outcome-'].Update("🛡🛡 Both of you blocked!")
elif self.enemy.block == True and self.player.block == False: #enemy blocks, player does not
if self.player.armed == False: #player can't shoot
self.window['-outcome-'].Update("🛡😈 Enemy blocked the attack!")#must reload if not
else:#player can shoot
self.window['-outcome-'].Update("😐👿 Nothing happened!")#enemy is blocking
elif self.enemy.block == False and self.player.block == True: #enemy does not block, player does
if self.enemy.armed == False: #enemy can't shoot
self.window['-outcome-'].Update("😎🛡 You blocked the attack!") #enemy reloads
else: #enemy can shoot
self.window['-outcome-'].Update("😐👿 Nothing happened!") #player is blocking
elif self.enemy.block == False and self.player.block == False: #neither block
if self.enemy.armed == False and self.player.armed == True: #enemy is unarmed, player is armed
self.window['-outcome-'].Update("🤕💥 You are hit!") #enemy is hit
self.playerHit()
elif self.enemy.armed == True and self.player.armed == False:
self.window['-outcome-'].Update("💥👿 Enemy is hit!")
self.enemyHit()
elif self.enemy.armed == True and self.player.armed == True:
self.window['-outcome-'].Update("♻️♻️ Both of you reloaded!")
elif self.enemy.armed == False and self.player.armed == False: #both are reloading
self.window['-outcome-'].Update("💥💥 Both of you are hit!")
self.bothHit()
else:
self.window['-outcome-'].Update("")