大一python实验答案_20192114 《Python程序设计》实验四报告

20192108《Python程序设计》实验四报告

课程:《Python程序设计》

班级: 1921

学号:20192114

实验教师:王志强

实验日期:2020年6月13日

必修/选修: 公选课

1.实验内容

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。

课代表和各小组负责人收集作业(源代码、视频、综合实践报告)

(1)程序能运行,功能丰富。(需求提交源代码,并建议录制程序运行的视频)10分

(2)综合实践报告,要体现实验分析、设计、实现过程、结果等信息,格式规范,逻辑清晰,结构合理。10分。

(3)在实践报告中,需要对全课进行总结,并写课程感想体会、意见和建议等。5分

2. 实验过程及结果

前几天TapTap上2048极速版排行榜登顶,我决定用python编写一个2048小游戏

实验过程

1.先导入所需的库

curses 用来在终端上显示图形界面

random 模块用来生成随机数

collections 提供了一个字典的子类 defaultdict。可以指定 key 值不存在时,value 的默认值。

import random

import curses

import collections

2.定义

定义

class Matrix(object)

class Grid(object)

3.设置

上下左右与得分

def getScore():

def left(self):

def right(self):

def up(self):

def down(self):

4.在终端上显示图形界面

并不会,寻求百度帮助

实验结果

1939662-20200614124748328-1007771775.png

完整代码如下

"""

作者:杜晨玄

文件名称:t04

时间:2020.6.13

2048

"""

from Tkinter import *

from random import randint

from random import choice

import tkMessageBox

class Grid(object):

def __init__(self, master=None, height=4, width=4, offset=10, grid_width=200, bg="#696969"):

self.height = height

self.width = width

self.offset = offset

self.grid_width = grid_width

self.bg = bg

self.canvas = Canvas(master, width=self.width*self.grid_width + 2*self.offset,height=self.height*self.grid_width+2*self.offset, bg=self.bg)

self.initial()

def initial(self):

for i in range(0,4):

for j in range(0,4):

x = i * self.grid_width + self.offset

y = j * self.grid_width + self.offset

self.canvas.create_rectangle(x+10, y+10, x + self.grid_width-10, y + self.grid_width-10, fill="#808080",outline=self.bg)

self.canvas.pack(side=RIGHT, fill=Y)

def draw(self, pos, color, text):

x = pos[0] * self.grid_width + self.offset

y = pos[1] * self.grid_width + self.offset

self.canvas.create_rectangle(x+10, y+10, x + self.grid_width-10, y + self.grid_width-10, fill=color, outline=self.bg)

ft1 = ('Comic Sans MS', 50, "bold")

self.canvas.create_text(pos[0] * 200 + 110, pos[1] * 200 + 110, text=text, font=ft1)

class Matrix(object):

def __init__(self, grid):

self.grid = grid

self.matrix = [[0 for i in range(4)] for i in range(4)]

self.matrix_o = [[0 for i in range(4)] for i in range(4)]

self.vacancy = []

self.gamewin = False

self.color ={0:"#808080", 2:"#FFFACD", 4:"#F5DEB3", 8:"#F0E68C", 16:"#FFFF00", 32:"#FFD700", 64:"#FFA500", 128:"#FF8C00",

256:"#CD5C5C", 512:"#FF6347", 1024:"#FF0000", 2048:"#00FFFF"}

def initial(self):

self.matrix = [[0 for i in range(4)] for i in range(4)]

self.void()

self.generate()

self.generate()

self.draw()

self.gamewin = False

for i in range(0, 4):

for j in range(0, 4):

self.matrix_o[i][j] = self.matrix[i][j]

def draw(self):

for i in range (0, 4):

for j in range (0, 4):

pos = (i, j)

text = str(self.matrix[i][j])

color = self.color[self.matrix[i][j]]

self.grid.draw(pos, color, text)

def void(self):

self.vacancy = []

for x in range(0,4):

for y in range(0,4):

if self.matrix[x][y] == 0:

self.vacancy.append((x,y))

return len(self.vacancy)

#生成2或4

def generate(self):

pos=choice(self.vacancy)

if randint(0,5)==4:

self.matrix[pos[0]][pos[1]] = 4

else:

self.matrix[pos[0]][pos[1]] = 2

del self.vacancy[self.vacancy.index((pos[0], pos[1]))]

#左移

def up(self):

ss = 0

for i in range(0, 4):

for j in range(0, 3):

s = 0

if not self.matrix[i][j] == 0:

for k in range(j + 1, 4):

if not self.matrix[i][k] == 0:

if self.matrix[i][j] == self.matrix[i][k]:

ss = ss + self.matrix[i][k]

self.matrix[i][j] = self.matrix[i][j] * 2

if self.matrix[i][j] == 2048:

self.gamewin = True

self.matrix[i][k] = 0

s = 1

break

else:

break

if s == 1:

break

for i in range(0, 4):

s = 0

for j in range(0, 3):

if self.matrix[i][j - s] == 0:

self.matrix[i].pop(j - s)

self.matrix[i].append(0)

s = s + 1

return ss

#右移

def down(self):

for i in range(0, 4):

self.matrix[i].reverse()

ss = self.up()

for i in range(0, 4):

self.matrix[i].reverse()

return ss

#上移

def left(self):

ss = 0

for i in range(0, 4):

for j in range(0, 3):

s = 0

if not self.matrix[j][i] == 0:

for k in range(j + 1, 4):

if not self.matrix[k][i] == 0:

if self.matrix[j][i] == self.matrix[k][i]:

ss = ss + self.matrix[k][i]

self.matrix[j][i] = self.matrix[j][i] * 2

if self.matrix[j][i] == 2048:

self.gamewin = True

self.matrix[k][i] = 0

s = 1

break

else:

break

if s == 1:

break

for i in range(0, 4):

s = 0

for j in range(0, 3):

if self.matrix[j-s][i] == 0:

for k in range(j-s, 3):

self.matrix[k][i] = self.matrix[k+1][i]

self.matrix[3][i] = 0

s = s+1

return ss

#下移

def right(self):

ss = 0

for i in range(0, 4):

for j in range(0, 3):

s = 0

if not self.matrix[3-j][i] == 0:

k = 3-j-1

while k >= 0:

if not self.matrix[k][i] == 0:

if self.matrix[3-j][i] == self.matrix[k][i]:

ss = ss + self.matrix[k][i]

self.matrix[3-j][i] = self.matrix[3-j][i] * 2

if self.matrix[3-j][i] == 2048:

self.gamewin = True

self.matrix[k][i] = 0

s = s+1

break

else:

break

k = k -1

if s == 1:

break

for i in range(0, 4):

s = 0

for j in range(0, 3):

if self.matrix[3-j+s][i] == 0:

k = 3-j+s

while k > 0:

self.matrix[k][i] = self.matrix[k-1][i]

k = k-1

self.matrix[0][i] = 0

s = s+1

return ss

class Game(Frame):

def __init__(self, master):

Frame.__init__(self, master)

self.grid = Grid(master)

self.matrix = Matrix(self.grid)

self.score = 0

self.status = ['run', 'stop']

self.grid.canvas.bind_all("", self.key_release)

self.m = StringVar()

self.ft1 = ('Times New Roman', 40, "bold")

self.m1 = Message(master, textvariable=self.m, aspect=5000, font=self.ft1, bg="#696969")

self.m1.pack(side=LEFT, fill=Y)

self.m.set("Score:"+str(self.score))

self.initial()

def initial(self):

self.score = 0

self.m.set("Score:"+str(self.score))

self.matrix.initial()

def key_release(self, event):

key = event.keysym

if key == "Up":

ss = self.matrix.up()

self.run(ss)

elif key == "Down":

ss = self.matrix.down()

self.run(ss)

elif key == "Left":

ss = self.matrix.left()

self.run(ss)

elif key == "Right":

ss = self.matrix.right()

self.run(ss)

def run(self, ss):

if not self.matrix.matrix == self.matrix.matrix_o:

self.score = self.score + int(ss)

self.m.set("Score:" + str(self.score))

if self.matrix.gamewin == True:

self.matrix.draw()

message = tkMessageBox.showinfo("芜湖,你就是龙!", "你的分数: %d" % self.score)

if message == 'ok':

self.initial()

else:

self.matrix.void()

self.matrix.generate()

for i in range(0, 4):

for j in range(0, 4):

self.matrix.matrix_o[i][j] = self.matrix.matrix[i][j]

self.matrix.draw()

else:

v = self.matrix.void()

if v < 1:

message = tkMessageBox.showinfo("gg", "你的分数: %d" % self.score)

if message == 'ok':

self.initial()

if __name__ == '__main__':

root = Tk()

game = Game(root)

game.mainloop()

码云链接如下

运行结果如图

3. 实验过程中遇到的问题和解决过程

问题1:windows系统在安装curses库时,使用conda或者pip安装,总是失败

问题1解决方案:百度

问题2:运行显示Redirection is not supported.

问题2解决方案:百度

问题3:图形界面不会搞

问题3解决方案:百度,从github找了几个参考

其他(感悟、思考等)

这学期同时学c和python,一开始有点混乱,但是后面就好起来了,c语言的文件操作和python的差不多,很有帮助

网上有很多python的资源这对于学习python帮助很大,老师的讲解也十分到位、清晰

参考资料

个人博客 jerwang.cn

标签:20192114,matrix,Python,self,ss,range,grid,程序设计,def

来源: https://www.cnblogs.com/20192114dcx/p/13124436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值