• 大小: 8KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: Python
  • 标签: python  

资源简介

2048小游戏的python语言编写,已经运行成功,按F10开始游戏,通过键盘的上下左右来进行游戏,具体游戏规则可百度,感兴趣的可以看下

资源截图

代码片段和文件信息

import pyHook
import random
import time
class Game():
    def __init__(selfxnum=4ynum=4):
        self.xnum=xnum
        self.ynum=ynum
        self.score=0
        self.randdata=[24]
        self.data=[[0 for i in range(0xnum)]for i in range(0ynum)]
    def trans(selflista):
        listb=[[row[i] for row in lista]for i in range(len(lista[0]))]
        return listb
    def createdata(self):
        self.thisdata=random.choice(self.randdata)
        zeros=[]
        for i in range(0len(self.data)):
            for j in range(0len(self.data[0])):
                if(self.data[i][j]==0):
                    zeros.append((ij))
        self.thisposition=random.choice(zeros)
        self.data[self.thisposition[0]][self.thisposition[1]]=self.thisdata
    def lmerge(self):
        for i in range(0len(self.data)):
            for j in range(1len(self.data[0])):
                if(self.data[i][j]==self.data[i][j-1]):
                    self.data[i][j-1]=self.data[i][j-1]*2
                    self.score=self.data[i][j-1]+self.score
                    self.data[i][j]=0
    def rmerge(self):
        for i in range(0len(self.data)):
            for j in range(len(self.data[0])-10-1):
                if(self.data[i][j]==self.data[i][j-1]):
                    self.data[i][j]=self.data[i][j]*2
                    self.score=self.data[i][j]+self.score
                    self.data[i][j-1]=0
    def umerge(self):
        trans_data=self.trans(self.data)
        for i in range(0len(trans_data)):
            for j in range(1len(trans_data[0])):
                if(trans_data[i][j]==trans_data[i][j-1]):
                    trans_data[i][j-1]=trans_data[i][j-1]*2
                    self.score=trans_data[i][j-1]+self.score
                    trans_data[i][j]=0
        self.data=self.trans(trans_data) 
    def dmerge(self):
        trans_data=self.trans(self.data)
        for i in range(0len(trans_data)):
            for j in range(len(trans_data[0])-10-1):
                if(trans_data[i][j]==trans_data[i][j-1]):
                    trans_data[i][j]=trans_data[i][j]*2
                    self.score=trans_data[i][j]+self.score
                    trans_data[i][j-1]=0
        self.data=self.trans(trans_data)
    def left(self):
        for i in range(0len(self.data)):
            thisline=self.data[i]
            can_movepos=None
            for j in range(0len(thisline)):
                if(thisline[j]==0):
                    if(j!=0):
                        if(self.data[i][j]==self.data[i][j-1]):
                            pass
                        else:
                            can_movepos=j
                    else:
                         can_movepos=j
                else:
                    if( can_movepos==None):
                        pass
                    else:
                        if(can_movepos                            self.data[i][can_mov

评论

共有 条评论