-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
215 lines (188 loc) Β· 7.72 KB
/
Copy pathmain.py
File metadata and controls
215 lines (188 loc) Β· 7.72 KB
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
import os
import discord
from discord.ext import commands
from keep_alive import keep_alive
import numpy as np
import random
wall = "β¬"
innerWall = "β¬"
energy = "π"
snakeHead = "π"
snakeBody = "π¨"
snakeLoose = "π΅"
def getGameGrid():
str = ""
for item in snakeMatrix:
# print(item)
for i in item:
if(i == 0):
str += wall
elif i == 1:
str += innerWall
elif i == 2:
str += snakeHead
elif i == 3:
str += snakeBody
elif i == 4:
str += energy
else:
str += snakeLoose
str += "\n"
return str
def generateRandomEnergy():
snakeMatrix[random.randint(1,10)][random.randint(1,10)] = 4
def checkEnergy(i, j):
# print("i : {}".format(i))
# print("j : {}".format(j))
# print("Pos Val : {}".format(snakeMatrix[i][j]))
return snakeMatrix[i][j] == 4
def handleEnergy(i, j):
global points
# print(checkEnergy(i, j))
if(checkEnergy(i, j)):
generateRandomEnergy()
points += 1
def updateSnakePosition(i, j, k, l):
snakeMatrix[i][j] = 2
snakeMatrix[k][l] = 1
def isOuterBoundary(i, j):
global isOut
if(i == 0 or j == 0 or i == 11 or j == 11):
# print("Out")
snakeHeadPos = np.argwhere(snakeMatrix == 2)[0]
snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]] = 5
isOut = True
print("isOut : {}".format(isOut))
return True
return False
def moveUp():
# print("Up")
snakeHeadPos = np.argwhere(snakeMatrix == 2)[0]
# print(snakeHeadPos)
# print(snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]])
if(not isOuterBoundary(snakeHeadPos[0]-1, snakeHeadPos[1])):
handleEnergy(snakeHeadPos[0]-1, snakeHeadPos[1])
updateSnakePosition(snakeHeadPos[0]-1, snakeHeadPos[1], snakeHeadPos[0], snakeHeadPos[1])
# snakeMatrix[snakeHeadPos[0]-1][snakeHeadPos[1]] = 2
# snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]] = 1
def moveLeft():
# print("Left")
snakeHeadPos = np.argwhere(snakeMatrix == 2)[0]
if(not isOuterBoundary(snakeHeadPos[0], snakeHeadPos[1]-1)):
handleEnergy(snakeHeadPos[0], snakeHeadPos[1]-1)
updateSnakePosition(snakeHeadPos[0], snakeHeadPos[1]-1, snakeHeadPos[0], snakeHeadPos[1])
# snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]-1] = 2
# snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]] = 1
def moveRight():
# print("Right")
snakeHeadPos = np.argwhere(snakeMatrix == 2)[0]
if(not isOuterBoundary(snakeHeadPos[0], snakeHeadPos[1]+1)):
handleEnergy(snakeHeadPos[0], snakeHeadPos[1]+1)
updateSnakePosition(snakeHeadPos[0], snakeHeadPos[1] + 1, snakeHeadPos[0], snakeHeadPos[1])
# snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]+1] = 2
# snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]] = 1
def moveDown():
# print("Down")
snakeHeadPos = np.argwhere(snakeMatrix == 2)[0]
if(not isOuterBoundary(snakeHeadPos[0]+1, snakeHeadPos[1])):
handleEnergy(snakeHeadPos[0]+1, snakeHeadPos[1])
updateSnakePosition(snakeHeadPos[0]+1, snakeHeadPos[1], snakeHeadPos[0], snakeHeadPos[1])
# snakeMatrix[snakeHeadPos[0]+1][snakeHeadPos[1]] = 2
# snakeMatrix[snakeHeadPos[0]][snakeHeadPos[1]] = 1
def reset():
global snakeMatrix, isOut, points
isOut = False
# print("Reset")
snakeMatrix = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
])
points = 0
generateRandomEnergy()
def getNormalEmbededData(title, description):
return discord.Embed(title=title, description=description, color=discord.Color.green())
def getErrorEmbededData(title, description):
return discord.Embed(title=title, description=description, color=discord.Color.red())
async def sendMessage(message):
embedVar=getNormalEmbededData(title="Pick Apple Game", description="{}".format(getGameGrid()))
embedVar.add_field(name="Your Score", value=points, inline=True)
await message.channel.send(embed=embedVar)
client = discord.Client()
@client.event
async def on_ready():
print("You have logged in as {0.user}".format(client))
# await channel.send('hello')
@client.event
async def on_message(message):
# print("Message Channel : {}".format(message.channel))
# print(os.environ['CHANNEL_ID'])
gameChannel = client.get_channel(int(os.environ['CHANNEL_ID']))
# print("gameChannel Channel : {}".format(gameChannel))
if(message.author == client.user):
return
if gameChannel == message.channel:
if(message.content.startswith('!hello')):
# print(message.channel)
reset()
embedVar = getNormalEmbededData(title="Welcome *{0.author}* to our Useless Game Channel ! Lets Play Game".format(message), description="β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬β¬β¬πβ¬β¬\nβ¬β¬π¨π¨β¬β¬β¬β¬β¬β¬β¬\nβ¬β¬β¬π¨π¨π¨π¨β¬β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬π¨π¨β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬β¬π¨π¨π΅β¬\nβ¬β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬\nβ¬β¬β¬β¬β¬β¬β¬β¬β¬β¬β¬\n\n` w ` -> Move Left\n` d ` -> Move Right\n` w ` -> Move Up\n` s ` -> Move Down\n` r ` -> Reset")
await message.channel.send(embed=embedVar)
elif(message.content.startswith('r')):
print("Reset")
reset()
embedVar=getNormalEmbededData(title="Pick Apple Game", description="{}\nGame has been reset. You can start playing new game".format(getGameGrid()))
embedVar.add_field(name="Your Score", value=points, inline=True)
await message.channel.send(embed=embedVar)
elif(isOut):
embedVar=getErrorEmbededData(title="Game Over", description="Scored Point : {}".format(points))
await message.channel.send(embed=embedVar)
elif(message.content.startswith('w')):
moveUp()
await sendMessage(message)
elif(message.content.startswith('a')):
moveLeft()
await sendMessage(message)
elif(message.content.startswith('s')):
moveDown()
await sendMessage(message)
elif(message.content.startswith('d')):
moveRight()
await sendMessage(message)
else:
embedVar = getErrorEmbededData(title="*Error*", description="Invalid Input Detected ! Please enter a valid input. \n ` w ` -> Move Left\n` d ` -> Move Right\n` w ` -> Move Up\n` s ` -> Move Down\n` r ` -> Reset")
await message.channel.send(embed=embedVar)
else:
print("Wrong Channel")
@commands.command(name='test')
async def check(self, ctx):
if ctx.channel.name == 'game':
await ctx.send("Response message")
my_secret = os.environ['TOKEN']
keep_alive()
points = 0
isOut = False
snakeMatrix = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
])
generateRandomEnergy()
client.run(my_secret)