-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
293 lines (231 loc) · 9.65 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import discord
import numpy
import asyncio
import time
from game import Game
from level import Extra
client = discord.Client()
started = False
topscore = 0
topscoreplayer = "no one yet"
nextmoves = []
player = 0
single = True
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
global started
global topscore
global nextmoves
global single
global topscoreplayer
if message.author == client.user:
return
if message.content.startswith('$help'):
await message.delete(delay=2)
helpmsg = sendHelp()
await message.channel.send(embed=helpmsg, delete_after=60)
elif message.content.startswith('$instructions'):
await message.delete(delay=2)
msg = sendInstruct()
await message.channel.send(embed=msg, delete_after=45)
elif message.content.startswith('$pacplay') and not started: #CHANGE TO $PACPLAY
startmsg = discord.Embed(title="Let's play!")
await message.channel.send(embed=startmsg, delete_after=5)
await message.delete(delay=2)
started = True
nextmoves = []
maingame = Game(topscore)
await message.channel.send(embed=maingame.game, delete_after=3)
if single:
player = message.author
await gameloop(message, maingame, player)
elif message.content.startswith('$stop') and started:
started = False
await message.delete(delay=2)
elif message.content.startswith('$topscore') and not started:
await message.delete(delay=2)
best = discord.Embed(title=f"This channel's top score: {topscore}, by {topscoreplayer}!")
await message.channel.send(embed=best)
elif message.content.startswith('$single') and not started:
await message.delete(delay=2)
single = True
mode = discord.Embed(title="Discord-Pac is now in Single Player mode")
await message.channel.send(embed=mode, delete_after=45)
elif message.content.startswith('$multi') and not started:
await message.delete(delay=2)
single = False
mode = discord.Embed(title="Discord-Pac is now in Channel Wide mode")
await message.channel.send(embed=mode, delete_after=45)
elif started and len(nextmoves) < 3:
if message.content.startswith('w'):
nextmoves.append(Extra[15])
await message.delete(delay=2)
elif message.content.startswith('a'):
nextmoves.append(Extra[14])
await message.delete(delay=2)
elif message.content.startswith('s'):
nextmoves.append(Extra[16])
await message.delete(delay=2)
elif message.content.startswith('d'):
nextmoves.append(Extra[13])
await message.delete(delay=2)
aboutText = """
Thanks for playing Discord-Pac!
This bot was built by Minha using the discord.py library and NumPy, as practice for building more complex bots in the future.
It lacks a lot of feature originally planned and will contain bugs since this is a work in progress!
There are plans to continue development and polishing when time allows!
For any problems or questions, you can contact me at: [email protected]
"""
helpText = """
$instructions = a guide to this version of the game! (recommended before playing)
$pacplay = start a round of Pac-man!
$stop = stop the game at any time!
$single = changes to single player mode! (Default) (unimplemented)
$multi = changes to channel-wide mode! (unimplemented)
$topscore = shows this channel's high score!
$help = view list of commands, information about the bot, etc.\n
More features to come!
"""
instructText = """
This game is in semi-real-time.*
You control \U0001F60F!
Look out for: \U0001F47B \U0001F47D \U0001F47E \U0001F916!
Collect dots ("\U000025AB") to increase you score!
Powerups ("\U000025FB") make you temporarily invincible and allow you to eat your enemies for more points!.\n
Change the direction of Pac by sending a new messages:
"w" (up)
"a" (left)
"s" (down)
"d" (right)
and the game will record up to 3 next turns which will execute when possible.
These turns cannot be changed so choose wisely!
Good luck!
"""
def sendHelp():
helpmsg = discord.Embed(title="Discord-Pac, a Pac-Man rebuild in Discord!", description="Built by really big dog :) (Minha)")
helpmsg.add_field(name="Commands", value=helpText, inline=False)
helpmsg.add_field(name="About Discord-Pac", value=aboutText, inline=False)
helpmsg.add_field(name="I'm also currently looking for work! Email me your career opportunities!", value="Enjoy Discord-Pac!", inline=False)
return helpmsg
def sendInstruct():
instructmsg = discord.Embed(title="Thanks for playing Pac-Man!")
instructmsg.add_field(name="HOW TO PLAY:", value=instructText, inline=False)
instructmsg.set_footer(text="*moves may take up to 1-2 frames for the game to register, I'm working on fixing this!")
return instructmsg
#this is the only thing that interfaces between the class and main
async def gameloop(message, maingame, player):
global started
global topscore
global single
global topscoreplayer
"""
To Implementing:
Random fruit drops
Scoreboard
"""
testtime = [0, 0]
while started:
start = time.time()
await asyncio.gather(
moveshit(maingame),
boardupdate(maingame),
waittime(1)
)
await message.channel.send(embed=maingame.game, delete_after=2)
end = time.time()
testtime[0] += (end - start)
testtime[1] += 1
if maingame.Pac.state == "dead":
if maingame.lives > 1:
maingame.lives -= 1
await asyncio.gather(
message.channel.send(f"You died! Lives left: {maingame.lives}", delete_after=5),
waittime(5),
resetpos(maingame)
)
else:
highscore = ""
if maingame.score > topscore:
topscore = maingame.score
highscore = "A new Top Score!"
topscoreplayer = player
started = False
seconds = int(testtime[0])%60
minutes = int((int(testtime[0])-seconds)/60)
desc = f"Your score was {maingame.score}, achieved in {minutes} minutes and {seconds} seconds! "
gameover = discord.Embed(title="Game Over!", description=desc+highscore)
contact = "For any problems or questions, you can contact me at [email protected]."
gameover.add_field(name="Thanks for playing!", value=contact, inline=False)
await message.channel.send(embed=gameover)
print(f"Avg time: {testtime[0]/testtime[1]}")
started = False
async def moveshit(main):
global nextmoves
prevspaces = []
prevspaces.append(main.Pac.coord.copy())
if len(nextmoves)>=1:
move = nextmoves[0]
if main.Pac.direction == move:
nextmoves.pop(0)
else:
move = main.Pac.direction
main.Pac.prevspace = main.Pac.coord.copy()
changes = main.Pacmove(main.Pac, move)
if len(nextmoves) >= 1 and prevspaces[0] == main.Pac.coord:
nextmoves.pop(0)
if changes[0]: #direction changed
main.Pac.direction = nextmoves[0]
nextmoves.pop(0)
if changes[1]: #ate something
main.score += changes[2]
main.grid[main.Pac.coord[0], main.Pac.coord[1]] = 5
if changes[3]: #statechanged
main.Pac.changeemoji(changes[4])
#ghost updates
for ghost in main.Ghosts:
if ghost.counter == 0:
prevspaces.append(ghost.coord.copy())
ghost.prevspace = ghost.coord.copy()
ghostmove = main.Ghostmove(ghost, main.Pac)
if ghostmove[0]: #collide with Pac
if main.Pac.state == "powerup":
main.score += ghostmove[3]
else:
main.Pac.state = "dead"
if ghostmove[1]: #state changed
ghost.changeemoji(ghostmove[2])
else:
ghost.counter -= 1
if main.Pac.state == "dead":
main.Pac.changeemoji("dead")
charlist = [main.Ghosts[0], main.Ghosts[1], main.Ghosts[2], main.Ghosts[3], main.Pac]
main.emojiupdate(charlist, prevspaces)
async def boardupdate(main):
global nextmoves
global topscore
nextturns = ''
if len(nextmoves)>0:
nextturns = ''.join(nextmoves)
main.game.set_field_at(4, name=f"Score: {main.score}, High score: {topscore}", value=f"Next moves: {nextturns}", inline=False)
async def waittime(time):
await asyncio.sleep(time)
async def resetpos(main):
chars = [main.Pac]
clearlist = [main.Pac.coord]
for ghost in main.Ghosts:
chars.append(ghost)
clearlist.append(ghost.coord)
main.emojiupdate(chars, clearlist, False)
main.Pac.coord = main.Pac.origin
main.Pac.direction = "\U000025B6"
main.Pac.state = "begin"
main.Pac.changeemoji(main.Pac.state)
for ghost in main.Ghosts:
ghost.coord = ghost.origin
ghost.direction = "\U0001F53C"
ghost.state = "random"
ghost.changeemoji(ghost.state)
client.run('YOUR TOKEN HERE')