import java.awt.*
@SuppressWarnings("serial")
public class Game_block extends JLabel {
private int value
public Game_block() {
super("go!")
value = 0
setFont(new Font("font", Font.PLAIN, 40))
setBackground(Color.black)
}
public int getValue() {
return value
}
public void setValue(int value) {
this.value = value
String Text = String.valueOf(value)
if (value != 0){
if(value == 2)
setText("夏")
}
if (value != 0){
if(value == 4)
setText("商")
}
if (value != 0){
if(value == 8)
setText("周")
}
if (value != 0){
if(value == 16)
setText("秦")
}
if (value != 0){
if(value == 32)
setText("汉")
}
if (value != 0){
if(value == 64)
setText("三国")
}
if (value != 0){
if(value == 128)
setText("南北朝")
}
if (value != 0){
if(value == 256)
setText("隋")
}
if (value != 0){
if(value == 1024)
setText("五代十国")
}
if (value != 0){
if(value == 2048)
setText("宋辽")
}
if (value != 0){
if(value == 4096)
setText("宋金")
}
if (value != 0){
if(value == 8192)
setText("元")
}
if (value != 0){
if(value == 16384)
setText("明")
}
if (value != 0){
if(value == 32768)
setText("清")
}
if (value != 0){
if(value == 65536)
setText("现代")
}
else
setText("2048")
setColor()
}
public void setColor() {
if (this.value == 0)
setBackground(Color.black)
else if (this.value == 2)
setBackground(new Color(238, 228, 218))
else if (this.value == 4)
setBackground(new Color(248, 232, 248))
else if (this.value == 8)
setBackground(new Color(243, 177, 116))
else if (this.value == 16)
setBackground(new Color(243, 177, 126))
else if (this.value == 32)
setBackground(new Color(248, 149, 90))
else if (this.value == 64)
setBackground(new Color(249, 94, 50))
else if (this.value == 128)
setBackground(new Color(239, 207, 108))
else if (this.value == 256)
setBackground(new Color(239, 207, 99))
else if (this.value == 512)
setBackground(new Color(239, 203, 82))
else if (this.value == 1024)
setBackground(new Color(239, 199, 57))
else if (this.value == 2048)
setBackground(new Color(239, 195, 41))
else if (this.value == 4096)
setBackground(new Color(255, 60, 57))
else if (this.value == 8192)
setBackground(new Color(258, 60, 59))
else if (this.value == 16384)
setBackground(new Color(262, 62, 59))
else if (this.value == 32768)
setBackground(new Color(262, 64, 62))
else if (this.value == 65536)
setBackground(new Color(264, 64, 66))
}
}
______________________________________________________
import java.awt.*
import java.awt.event.KeyEvent
import java.awt.event.KeyListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.*
@SuppressWarnings("serial")
public class Game_my2048 extends JFrame implements KeyListener {
Game_block[] block
JPanel panel
boolean numFlag
int moveFlag
int a //a 的引入是为了防止连加的情况出现
public Game_my2048() {
numFlag = true
moveFlag = 0
block = new Game_block[16]
setTitle("2048朝代版")
setSize(400, 400)
setLocation(500, 200)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
panel = (JPanel) getContentPane()
panel.setLayout(new GridLayout(4, 4, 5, 5))
addBlock()
for (int i = 0i <2i++)
appearBlock()
this.addKeyListener(this)
this.setVisible(true)
}
public void addBlock() {
for (int i = 0i <16i++) {
block[i] = new Game_block()
block[i].setHorizontalAlignment(JLabel.CENTER)
// 不透明
block[i].setOpaque(true)
panel.add(block[i])
}
}
public void appearBlock() {
while(numFlag) {
int index = (int) (Math.random() * 16)
if (block[index].getValue() == 0) {
if (Math.random() <0.5)
block[index].setValue(2)
else
block[index].setValue(4)
break
}
}
}
public void judgeAppear() {
int sum = 0
for (int i = 0i <16i++) {
if (block[i].getValue() != 0)
sum++
}
if (sum == 16)
numFlag = false
}
public void upBlock() {
for (int i = 12i <16i++) {
int index = i
for (int j = i - 4j >= i - 12j -= 4) {
int valueI = block[index].getValue(), valueJ = block[j]
.getValue()
if (valueJ == 0) {
block[index].setValue(0)
block[j].setValue(valueI)
} else {
if (valueI == valueJ) {
block[index].setValue(0)
block[j].setValue(valueI + valueJ)
if (valueI + valueJ == 4096)
win()
numFlag = true
moveFlag = 0
} else if (numFlag == false)
moveFlag += 1
}
index = j
}
}
}
public void downBlock() {
for (int i = 0i <4i++) {
a = 5
int index = i
for (int j = i + 4j <= i + 12j += 4) {
int valueI = block[index].getValue(), valueJ = block[j]
.getValue()
if (valueJ == 0) {
block[index].setValue(0)
block[j].setValue(valueI)
} else {
if (valueI == valueJ) {
block[index].setValue(0)
block[j].setValue(valueI + valueJ)
if (valueI + valueJ == 4096)
win()
numFlag = true
moveFlag = 0
} else if (numFlag == false)
moveFlag += 1
}
index = j
}
}
}
public void rightBlock() {
for (int i = 0i <= 12i += 4) {
int index = i
for (int j = i + 1j <= i + 3j++) {
int valueI = block[index].getValue(), valueJ = block[j]
.getValue()
if (valueJ == 0) {
block[index].setValue(0)
block[j].setValue(valueI)
} else {
if (valueI == valueJ) {
block[index].setValue(0)
block[j].setValue(valueI + valueJ)
if (valueI + valueJ == 4096)
win()
numFlag = true
moveFlag = 0
} else if (numFlag == false)
moveFlag += 1
}
index = j
}
}
}
public void leftBlock() {
for (int i = 3i <= 15i += 4) {
int index = i
for (int j = i - 1j >= i - 3j--) {
int valueI = block[index].getValue(), valueJ = block[j]
.getValue()
if (valueJ == 0) {
block[index].setValue(0)
block[j].setValue(valueI)
} else {
if (valueI == valueJ) {
block[index].setValue(0)
block[j].setValue(valueI + valueJ)
if (valueI + valueJ == 4096)
win()
numFlag = true
moveFlag = 0
} else if (numFlag == false)
moveFlag += 1
}
index = j
}
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
upBlock()
judgeAppear()
appearBlock()
over()
break
case KeyEvent.VK_DOWN:
downBlock()
judgeAppear()
appearBlock()
over()
break
case KeyEvent.VK_LEFT:
leftBlock()
judgeAppear()
appearBlock()
over()
break
case KeyEvent.VK_RIGHT:
rightBlock()
judgeAppear()
appearBlock()
over()
break
}
}
public void over() {
if (!numFlag &&moveFlag >= 36) {
block[4].setText("G")
block[5].setText("A")
block[6].setText("M")
block[7].setText("E")
block[8].setText("O")
block[9].setText("V")
block[10].setText("E")
block[11].setText("R")
block[11].addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
reStart()
}
})
}
}
public void win() {
block[0].setText("Y")
block[1].setText("O")
block[2].setText("U")
block[13].setText("W")
block[14].setText("I")
block[15].setText("N")
block[15].addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
reStart()
}
})
}
public void reStart(){
numFlag=true
moveFlag=0
for(int i=0i<16i++)
block[i].setValue(0)
for (int i = 0i <2i++)
appearBlock()
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String args[]) {
JFrame.setDefaultLookAndFeelDecorated(true)
new Game_my2048()
}
}
__________________________________________________________________________
这个代码却少很多功能的
话不多说,直接上菜
为了方便大家copy,我就不分段解释了
import turtle, random
class BackGround(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
self.ht()
def draw_block(self):
self.shape('bg.gif') # 画出背景方块
for i in allpos:
self.goto(i)
self.stamp()
self.color('white', 'white') # 画出其他背景
self.goto(-215, 120)
self.begin_fill()
self.goto(215, 120)
self.goto(215, 110)
self.goto(-215, 110)
self.end_fill()
self.shape('title.gif')
self.goto(-125, 210)
self.stamp()
self.shape('score.gif')
self.goto(125, 245)
self.stamp()
self.shape('top_score.gif')
self.goto(125, 170)
self.stamp()
# 游戏失败及达成2048的提示文字
def judge(self):
global flag_win, flag_win_lose_text
self.color('blue')
judge = 0 # 判断是否还有位置可以移动
for i in block_dic.values():
for j in block_dic.values():
if i.num == 0 or i.num == j.num and i.distance(j) == 100:
judge += 1
if judge == 0: # 无位置可移动,游戏失败
self.write(' GAME OVER\n重新开始请按空格键', align='center', font=('黑体', 30, 'bold'))
flag_win_lose_text = False
if flag_win is True: # 此条件让2048达成的判断只能进行一次
for k in block_dic.values():
if k.num == 2048: # 游戏达成
flag_win = False
self.write(' 达成2048\n继续游戏请按回车键', align='center', font=('黑体', 30, 'bold'))
flag_win_lose_text = False
def win_lose_clear(self):
global flag_win_lose_text
self.clear()
flag_win_lose_text = True
def show_score(self): # 分值的显示
global score, top_score
if score >top_score:
top_score = score
with open('.\\score.txt', 'w') as f:
f.write(f'{top_score}')
self.color('white')
self.goto(125, 210)
self.clear()
self.write(f'{score}', align='center', font=('Arial', 20, 'bold'))
self.goto(125, 135)
self.write(f'{top_score}', align='center', font=('Arial', 20, 'bold'))
# 数字方块类
class Block(turtle.Turtle):
def __init__(self):
super().__init__()
self.ht()
self.penup()
self.num = 0
def draw(self):
self.clear()
dic_draw = {2: '#eee6db', 4: '#efe0cd', 8: '#f5af7b',
16: '#fb9660', 32: '#f57d5a', 64: '#f95c3d',
128: '#eccc75', 256: '#eece61', 512: '#efc853',
1024: '#ebc53c', 2048: '#eec430', 4096: '#aeb879',
8192: '#aab767', 16384: '#a6b74f'}
if self.num >0: # 数字大于0,画出方块
self.color(f'{dic_draw[self.num]}') # 选择颜色
self.begin_fill()
self.goto(self.xcor()+48, self.ycor()+48)
self.goto(self.xcor()-96, self.ycor())
self.goto(self.xcor(), self.ycor()-96)
self.goto(self.xcor()+96, self.ycor())
self.goto(self.xcor(), self.ycor()+96)
self.end_fill()
self.goto(self.xcor()-48, self.ycor()-68)
if self.num >4: # 按照数字选择数字的颜色
self.color('white')
else:
self.color('#6d6058')
self.write(f'{self.num}', align='center', font=('Arial', 27, 'bold'))
self.goto(self.xcor(), self.ycor()+20)
class Game():
def init(self):
back = BackGround() # 实例画出游戏的背景
back.draw_block()
for i in allpos: # 画出16个海龟对应16个数字块
block = Block()
block.goto(i)
block_dic[i] = block
game.grow()
def restart(self): # 重开游戏的方法
global score, flag_win_lose_text
score = 0
for i in block_dic.values():
i.num = 0
i.clear()
win_lose_text.clear()
game.grow()
flag_win_lose_text = True # 此flag为游戏达成或失败出现提示语后的判断,要提示语被clear后才能继续move
def grow(self): # 随机出现一个2或4的数字块
block_list = []
for i in allpos:
if block_dic[i].num == 0:
block_list.append(block_dic[i]) # 挑出空白方块的海龟
turtle_choice = random.choice(block_list) # 随机选中其中一个海龟
turtle_choice.num = random.choice([2, 2, 2, 2, 4]) # 赋属性num=2/4
turtle_choice.draw()
win_lose_text.judge()
show_score_text.show_score()
ms.update()
def move_up(self):
allpos1 = allpos[::4] # 切片为四列
allpos2 = allpos[1::4]
allpos3 = allpos[2::4]
allpos4 = allpos[3::4]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_down(self):
allpos1 = allpos[-4::-4]
allpos2 = allpos[-3::-4]
allpos3 = allpos[-2::-4]
allpos4 = allpos[-1::-4]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_left(self):
allpos1 = allpos[:4]
allpos2 = allpos[4:8]
allpos3 = allpos[8:12]
allpos4 = allpos[12:16]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_right(self):
allpos1 = allpos[-1:-5:-1]
allpos2 = allpos[-5:-9:-1]
allpos3 = allpos[-9:-13:-1]
allpos4 = allpos[-13:-17:-1]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_move(self, allpos1, allpos2, allpos3, allpos4):
if flag_win_lose_text is True:
count1 = self.move(allpos1) # 四列或四行依次移动
count2 = self.move(allpos2)
count3 = self.move(allpos3)
count4 = self.move(allpos4)
if count1 or count2 or count3 or count4: # 判断是否有方块移动,有才能继续出现新的数字块
self.grow()
def move(self, pos_list):
num_list = [] # 为某一列或行的数字块海龟的坐标
for i in pos_list:
num_list.append(block_dic[i].num) # 把这些海龟的NUM形成list
new_num_list, count = self.list_oper(num_list) # 只是list_oper的方法形成新的list
for j in range(len(new_num_list)): # 把新的list依次赋值给对应的海龟.num属性并调用draw()方法
block_dic[pos_list[j]].num = new_num_list[j]
block_dic[pos_list[j]].draw()
return count
def list_oper(self, num_list): # num_list的 *** 作,假设其为【2,0,2,2】
global score
count = True
temp = []
new_temp = []
for j in num_list:
if j != 0:
temp.append(j) # temp=[2,2,2]
flag = True
for k in range(len(temp)):
if flag:
if k <len(temp)-1 and temp[k] == temp[k+1]:
new_temp.append(temp[k]*2)
flag = False
score += temp[k]
else:
new_temp.append(temp[k]) # new_temp=[4,2]
else:
flag = True
for m in range(len(num_list)-len(new_temp)):
new_temp.append(0) # new_temp=[4,2,0,0]
if new_temp == num_list:
count = False # 此变量判断num_list没有变化,数字块无移动
return(new_temp, count)
if __name__ == '__main__':
ms = turtle.Screen() # 主窗口的设置
ms.setup(430, 630, 400, 50)
ms.bgcolor('gray')
ms.title('2048')
ms.tracer(0)
ms.register_shape('bg.gif')
ms.register_shape('title.gif')
ms.register_shape('score.gif')
ms.register_shape('top_score.gif')
block_dic = {} # 放数字方块海龟的字典,位置坐标为key,对应海龟为value
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
flag_win = True # 达成2048的判断,让达成的文字仅出现一次
flag_win_lose_text = True # 用来判断失败或成功的提示文字是否有被清除,不清除不能继续移动方块
score = 0
with open('.\\score.txt', 'r') as f:
top_score = int(f.read()) # 读取score中的数据
show_score_text = BackGround()
win_lose_text = BackGround()
game = Game()
game.init()
ms.listen()
ms.onkey(game.move_up, 'Up')
ms.onkey(game.move_down, 'Down')
ms.onkey(game.move_left, 'Left')
ms.onkey(game.move_right, 'Right')
ms.onkey(win_lose_text.win_lose_clear, 'Return')
ms.onkey(game.restart, 'space')
ms.mainloop()
这是游戏界面:
欢迎挑战最高分。
要运行出来,必须本地要有这些文件:bg.gif,score.gif,title.gif,top_score.gif,score.txt
我把这些文件放在了群里,还有一些学习的资料,群号642109462,欢迎对python感兴趣的进群讨论。
支持作者的,可以关注和点赞。感谢你们!
一看就知道是作业,具体代码还是自己写吧,我可以给一些思路。构建一个类来存储矩阵的状态,以及 *** 作(上下左右)的代码,一个简单的实现方案是只需要写向一个方向移动的代码,然后通过转换矩阵的方向来实现其他的方向。
面向对象嘛你,可以父类定义N*N矩阵,只实现一个移动方向,子类通过继承实现4*4矩阵,和各个移动方向。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)