This repository was archived by the owner on Mar 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsnakes.py
More file actions
214 lines (172 loc) · 7.31 KB
/
snakes.py
File metadata and controls
214 lines (172 loc) · 7.31 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
# coding=utf-8
import asyncio
import logging
import random
import aiohttp
from typing import Any, Dict
from bs4 import BeautifulSoup
import discord
from discord.ext.commands import AutoShardedBot, Context, command
log = logging.getLogger(__name__)
class Snakes:
"""
Snake-related commands
"""
python_info = '''
Python (Programming Language)
\n
Python is powerful... and fast;\n
plays well with others;\n
runs everywhere;\n
is friendly & easy to learn;\n
is Open-Source.
-------------------------------
Created by: Guido Van Rossum \n
Founded: 20th of February, 1991 \n
Official website: https://python.org
'''
def __init__(self, bot: AutoShardedBot):
self.inputs = []
self.bot = bot
async def get_snek(self, name: str = None) -> Dict[str, Any]:
"""
Go online and fetch information about a snake
The information includes the name of the snake, a picture of the snake, and various other pieces of info.
What information you get for the snake is up to you. Be creative!
If "python" is given as the snake name, you should return information about the programming language, but with
all the information you'd provide for a real snake. Try to have some fun with this!
:param name: Optional, the name of the snake to get information for - omit for a random snake
:return: A dict containing information on a snake
"""
if name.lower() == 'python':
name = self.python_info
return name
@command()
async def get(self, ctx: Context, name: str = None, ):
"""
Go online and fetch information about a snake
This should make use of your `get_snek` method, using it to get information about a snake. This information
should be sent back to Discord in an embed.
:param ctx: Context object passed from discord.py
:param name: Optional, the name of the snake to get information for - omit for a random snake
"""
site = 'https://en.wikipedia.org/wiki/' + name
async with aiohttp.ClientSession() as session:
async with session.get(site) as resp:
text = await resp.text()
soup = BeautifulSoup(text, 'lxml')
title = soup.find('h1').text
description = soup.find('p').text
table = soup.find('table').text
list(table)
table = table[120:]
table = ''.join(table)
description = description + '\n' + table
em = discord.Embed(title=title, description=description)
if name.lower() == 'python':
await ctx.send(await self.get_snek(name))
else:
await ctx.send(embed=em)
# await ctx.send(name)
# Any additional commands can be placed here. Be creative, but keep it to a reasonable amount!
@command()
async def snake(self, ctx: Context, x=8, y=8):
snake = [] # define snake (where snake sections are stored)
head = [x // 2, y] # define head (where current snake head is stored)
apple = (random.randrange(x), random.randrange(y)) # define apple (where current apple position is stored)
userID = ctx.author.id
facing = 0
snake.append(head)
running = True
board = "\n " + ":black_large_square:" * x + ":black_large_square::black_large_square:"
for yAxis in range(y):
board += "\n:black_large_square:"
for xAxis in range(x):
if head == [xAxis, yAxis]:
board += ":snake:"
else:
board += ":white_large_square:"
board += ":black_large_square:"
board += "\n" + ":black_large_square:" * x + ":black_large_square::black_large_square:"
Board = discord.Embed(title="Snake!", description=board)
snakeBoard = await ctx.send(embed=Board)
while running:
directionChange = True
for mess in self.inputs:
if mess.author.id == userID:
if mess.content == "a":
if directionChange:
facing = (facing - 1) % 4
directionChange = False
await mess.delete()
elif mess.content == "d":
if directionChange:
facing = (facing + 1) % 4
directionChange = False
await mess.delete()
break
self.inputs = []
if facing == 0:
head[1] -= 1
elif facing == 1:
head[0] += 1
elif facing == 2:
head[1] += 1
else:
head[0] -= 1
if head[0] < 0 or head[1] < 0 or head[0] > x - 1 or head[1] > y - 1:
await ctx.send(str(ctx.author.mention) + " become a wall :cry:")
running = False
# check if
for snakeTail in snake:
if snakeTail == tuple(head):
await ctx.send(str(ctx.author.mention) + " ate himself :open_mouth: ")
running = False
# check if an apple was eaten
for snakeTail in snake:
if snakeTail == apple:
# if so it generates a new apple
apple = (random.randrange(x), random.randrange(y))
break
# if no apple is eaten then the else will run.
# So when an apple is eaten the last tuple to be added to list (snake) will not be removed
# this effectively makes the snake one section longer.
else:
snake.pop(0)
# add the current snake head to the list snake
snake.append(tuple(head))
# add title
board = """"""
# add top of board
board += "\n " + ":black_large_square:" * (x + 2)
for yAxis in range(y):
# add side of board
board += "\n:black_large_square:"
for xAxis in range(x):
# add snake sections
for snakeTail in snake:
if snakeTail == (xAxis, yAxis):
board += ":snake:"
break
else:
# add apple
if apple == (xAxis, yAxis):
board += ":apple:"
# add background
else:
board += ":white_large_square:"
# add side of board
board += ":black_large_square:"
# add bottom of board
board += "\n" + ":black_large_square:" * (x + 2)
# edit message then wait for next frame
Board = discord.Embed(title="Snake!", description=board)
await snakeBoard.edit(embed=Board)
await asyncio.sleep(0.9)
# get user snake inputs
async def on_message(self, message):
if message.content in ("a", "d"):
self.inputs.append(message)
def setup(bot):
bot.add_cog(Snakes(bot))
log.info("Cog loaded: Snakes")