티스토리 뷰

개발

2048게임 제작

KWG07(joseph0528) 2021. 2. 28. 09:34
반응형

 

2048게임을 디스코드 봇으로 만들어 봤다

밑에 이모지로 움직일 수 있고 나중에 w, a, s, d를 누르면 움직이게도 할 거고 pygame으로 좀 더 멋있게 꾸밀 것이다.

일단 이게임은 아직 한 번에 한명밖에 할수가 없어서 다른사람이 하고 있으면 기다려야된다. 한번에 여러 게임이 돌아가게 하는 것은 나중에 구현할 것이다.

 

import discord
import emoji
import game_2048
import asyncio
import random
import math
from collections import deque
client=discord.Client()
tile=["<:0:814871915004821504>","<:2:814857792178749440>",
      "<:4:814866255446147124>","<:8:814866255831629874>",
      "<:16:814866255886286889>","<:32:814869453988823070>",
      "<:64:814869454529363988>","<:128:814869454528708609>",
      "<:256:814869454948925470>","<:512:814869454579695646>",
      "<:1024:814869454647197747>","<:2048:814869455107653652>",
      "<:4096:814869454043349042>","<:8192:814869454911438890>",
      "<:16384:814869455078293554>","<:32768:814869453967327262>",
      "<:65536:814869454918778931>","<:131072:814869455464693780>"]
gp=0
gamescore=0
player=""
msg=0
msg2=0
l=[]
def pt():
    global l,gamescore
    pr=""
    rdm=random.randint(1,10)
    val=0
    if rdm==1:val=4
    else:val=2
    xy=[]
    for i in range(4):
        for g in range(4):
            if l[i][g]==0:xy.append([i,g])
    if len(xy)!=0:
        ag=random.randint(0,len(xy)-1)
        l[xy[ag][0]][xy[ag][1]]=val
    for i in range(4):
        for g in range(4):
            if l[i][g]==0:
                pr+=tile[0]
            else:
                pr+=tile[int(math.log2(l[i][g]))]
        pr+="\n"
    #print(pr)
    cq=0
    for i in range(4):
        for g in range(4):
            if l[i][g]==0:cq=1
            else:
                if i==1 or i==2:
                    if l[i][g]==l[i-1][g]:return[pr,0]
                    if l[i][g]==l[i+1][g]:return[pr,0]
                if g==1 or g==2:
                    if l[i][g]==l[i][g-1]:return[pr,0]
                    if l[i][g]==l[i][g+1]:return[pr,0]
    if cq==0:return[pr,1]
    else:return[pr,0]
@client.event
async def on_ready():
    print("Start {0.user}".format(client))
@client.event
async def on_message(message):
    global l,gp,gamescore,player,msg,msg2
    if message.author==client.user:return
    if message.content.startswith("~start 2048"):
        if gp==0:
       # try:
       #     qtyui=player[str(message.author.id)][0]
       #     await message.channel.send("게임 플레이 중입니다")
       # except:
            gamescore=0
            player=str(message.author.id)
            l=[[0 for i in range(4)]for g in range(4)]
            pr1=pt()
            gp=1
            af=""
            #player[str(message.author.id)]=[0,l]
            msg2=await message.channel.send("**"+str(gamescore)+"점**\n")
            msg=await message.channel.send(pr1[0])
            await msg.add_reaction("\U00002B06")#up
            await msg.add_reaction("\U00002B07")#down
            await msg.add_reaction("\U00002B05")#left
            await msg.add_reaction("\U000027A1")#right
        else:
            await message.channel.send("게임 플레이 중입니다")
            #file=open(str(message.guild.id)+'.txt','r')
        #print(file.readlines())
        #str(message.guild.id)+'.txt'
        #while 1:
        #qt=game_2048.game(l)
        #print("#",qt)
        #await message.channel.send(qt)
@client.event
async def on_reaction_add(reaction,user):
    global l,gp,gamescore,msg,msg2
    if user.bot==1:return None
    if str(reaction.emoji)=="\U00002B06":#up
        if gp==1 and player==str(user.id):
            #print("##")
            #await msg.delete()
            ch=[[0 for i in range(4)]for g in range(4)]
            for i in range(1,4):
                for g in range(4):
                    if l[i][g]!=0:
                        for t in range(i-1,-1,-1):
                            if l[t][g]==0:
                                l[t][g]=l[t+1][g]
                                l[t+1][g]=0
                            else:
                                if l[t][g]==l[t+1][g] and ch[t][g]==0:
                                    l[t][g]*=2
                                    gamescore+=l[t][g]
                                    l[t+1][g]=0
                                    ch[t][g]=1
                                break
            pr1=pt()
            #await msg.edit(content=pr1[0])
            #reaction.message.remove("\U00002B06")
            #await reaction.delete()
            msg1=msg
            msg3=msg2
            msg2=await reaction.message.channel.send("**"+str(gamescore)+"점**\n")
            msg=await reaction.message.channel.send(pr1[0])
            await msg.add_reaction("\U00002B06")#up
            await msg.add_reaction("\U00002B07")#down
            await msg.add_reaction("\U00002B05")#left
            await msg.add_reaction("\U000027A1")#right
            await msg1.delete()
            await msg3.delete()
            if pr1[1]==1:
                Embed=discord.Embed(title="Game over",description="Your score is "+str(gamescore),color=0x62c1cc)
                await message.channel.send(embed=Embed)
                gp=0
    if str(reaction.emoji)=="\U00002B07":#down
        if gp==1 and player==str(user.id):
            #print("##")
            #await msg.delete()
            ch=[[0 for i in range(4)]for g in range(4)]
            for i in range(2,-1,-1):
                for g in range(4):
                    if l[i][g]!=0:
                        for t in range(i+1,4):
                            if l[t][g]==0:
                                l[t][g]=l[t-1][g]
                                l[t-1][g]=0
                            else:
                                if l[t][g]==l[t-1][g] and ch[t][g]==0:
                                    l[t][g]*=2
                                    gamescore+=l[t][g]
                                    l[t-1][g]=0
                                    ch[t][g]=1
                                break
            pr1=pt()
            #await msg.edit(content=pr1[0])
            msg1=msg
            msg3=msg2
            msg2=await reaction.message.channel.send("**"+str(gamescore)+"점**\n")
            msg=await reaction.message.channel.send(pr1[0])
            await msg.add_reaction("\U00002B06")#up
            await msg.add_reaction("\U00002B07")#down
            await msg.add_reaction("\U00002B05")#left
            await msg.add_reaction("\U000027A1")#right
            await msg1.delete()
            await msg3.delete()
            if pr1[1]==1:
                Embed=discord.Embed(title="Game over",description="Your score is "+str(gamescore),color=0x62c1cc)
                await message.channel.send(embed=Embed)
                gp=0
    if str(reaction.emoji)=="\U00002B05":#left
        if gp==1 and player==str(user.id):
            #print("##")
            #await msg.delete()
            ch=[[0 for i in range(4)]for g in range(4)]
            for i in range(4):
                for g in range(1,4):
                    if l[i][g]!=0:
                        for t in range(g-1,-1,-1):
                            if l[i][t]==0:
                                l[i][t]=l[i][t+1]
                                l[i][t+1]=0
                            else:
                                if l[i][t]==l[i][t+1] and ch[i][t]==0:
                                    l[i][t]*=2
                                    gamescore+=l[i][t]
                                    l[i][t+1]=0
                                    ch[i][t]=1
                                break
            pr1=pt()
            msg1=msg
            msg3=msg2
            #await msg.edit(content=pr1[0])
            msg2=await reaction.message.channel.send("**"+str(gamescore)+"점**\n")
            msg=await reaction.message.channel.send(pr1[0])
            await msg.add_reaction("\U00002B06")#up
            await msg.add_reaction("\U00002B07")#down
            await msg.add_reaction("\U00002B05")#left
            await msg.add_reaction("\U000027A1")#right
            await msg1.delete()
            await msg3.delete()
            if pr1[1]==1:
                Embed=discord.Embed(title="Game over",description="Your score is "+str(gamescore),color=0x62c1cc)
                await message.channel.send(embed=Embed)
                gp=0
    if str(reaction.emoji)=="\U000027A1":#right
        if gp==1 and player==str(user.id):
            #print("##")
            #await msg.delete()
            ch=[[0 for i in range(4)]for g in range(4)]
            for i in range(4):
                for g in range(2,-1,-1):
                    if l[i][g]!=0:
                        for t in range(g+1,4):
                            if l[i][t]==0:
                                l[i][t]=l[i][t-1]
                                l[i][t-1]=0
                            else:
                                if l[i][t]==l[i][t-1] and ch[i][t]==0:
                                    l[i][t]*=2
                                    gamescore+=l[i][t]
                                    l[i][t-1]=0
                                    ch[i][t]=1
                                break
            pr1=pt()
            msg1=msg
            msg3=msg2
            #await msg.edit(content=pr1[0])
            msg2=await reaction.message.channel.send("**"+str(gamescore)+"점**\n")
            msg=await reaction.message.channel.send(pr1[0])
            await msg.add_reaction("\U00002B06")#up
            await msg.add_reaction("\U00002B07")#down
            await msg.add_reaction("\U00002B05")#left
            await msg.add_reaction("\U000027A1")#right
            await msg1.delete()
            await msg3.delete()
            if pr1[1]==1:
                Embed=discord.Embed(title="Game over",description="Your score is "+str(gamescore),color=0x62c1cc)
                await message.channel.send(embed=Embed)
                gp=0
    
client.run("token")

 

이모지는 따로 제작해서 파일을 테스트 서버에 올려놓은 상태라 다른 방에 이모지를 추가 안 해도 된다.

코드를 좀 더 간단하게 만들 수는 있지만 귀찮아서 그러지는 않았다.

게임하는 도중 오류가 생기면 댓글에 써주세요

따로 봇을 만들고 코드를 복사하셔서 하시는 걸 추천드립니다

반응형

'개발' 카테고리의 다른 글

오랜만에 올리는 디코 봇 현황  (1) 2021.08.08
디스코드 봇 업데이트  (0) 2021.05.15
디스코드 봇 업데이트  (0) 2021.02.08
디스코드 봇 개발  (1) 2021.01.25
tile게임 만들기  (0) 2021.01.15
댓글