python俄罗斯方块课程设计报告_用python实现俄罗斯方块

!/usr/bin/python

-- conding:utf-8 --

from tkinter import *

import time

import threading

import random

import math

from tkinter import messagebox

变量定义

BIANCHANG = 19

COLOR = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', '#00C5CD', '#00EE76', '#388E8E', '#556B2F', '#6B8E23',

'#8B2252', '#8B6969', '#A0522D', '#BC8F8F', '#BC8F3F', 'black']

COLUMN = 16

ROW = 30

class fangk:

def init(self, huabu, col, row):

self.huabu = huabu

self.col, self.row = col, row

self.color = COLOR[self.row % 16]

# self.setvisible(1)

self.havefk = False

def setvisible(self, statu):

if statu > 0:

x = self.col * (BIANCHANG + 1) + 2

y = 582 - (ROW - self.row - 1) * (BIANCHANG + 1)

self.fk = self.huabu.create_rectangle(x, y, x + BIANCHANG, y + BIANCHANG, fill=self.color)

self.line1 = self.huabu.create_line(x, y, x, y + BIANCHANG, fill='white')

self.line2 = self.huabu.create_line(x, y, x + BIANCHANG, y, fill='white')

self.havefk = True

elif statu == 0 and self.havefk:

self.huabu.delete(self.fk)

self.huabu.delete(self.line2)

self.huabu.delete(self.line1)

self.havefk = False

else:

return -1

def set_color(self, color):

self.color = color

return self

class elsfk:

def init(self):

self.fk_type = [[(0, 0, 1, 1), (0, 1, 0, 1)], # 正方形

[(0, 0, 0, 0), (1, 0, -1, -2)], # 长条

[(-1, 0, 1, 2), (0, 0, 0, 0)],

[(0, 1, 0, -1), (0, 1, 1, 0)], # 右Z

[(0, -1, -1, 0), (0, 1, 0, -1)],

[(0, -1, 0, 1), (0, 1, 1, 0)], # 左Z

[(0, 1, 1, 0), (0, 1, 0, -1)],

[(0, 0, -1, 1), (0, 1, 0, 0)], # T型

[(0, 0, 0, 1), (0, 1, -1, 0)],

[(0, 1, 0, -1), (0, 0, -1, 0)],

[(0, 0, -1, 0), (0, 1, 0, -1)],

[(0, 1, 1, -1), (0, -1, 0, 0)], # 左钩

[(0, 1, 0, 0), (0, 1, 1, -1)],

[(0, -1, -1, 1), (0, 1, 0, 0)],

[(0, 0, 0, -1), (0, 1, -1, -1)],

[(0, 1, 1, -1), (0, 1, 0, 0)], # 右钩

[(0, -1, 0, 0), (0, 1, 1, -1)],

[(0, -1, -1, 1), (0, -1, 0, 0)],

[(0, 0, 0, 1), (0, 1, -1, -1)]]

# 窗口

self.win = Tk()

self.win.title("俄罗斯方块")

# self.win.attributes("-alpha",0.95)

self.win.geometry('450x610')

self.win.resizable(0, 0)

self.nandu_stat=IntVar()

self.huabu = Canvas(self.win, bg="light grey", height=600, width=COLUMN * (BIANCHANG + 1), takefocus=True)

self.huabu_right = Canvas(self.win, height=100, width=100)

self.pauseBut = Button(self.win, text="暂停", bg='light green', height=1, width=12, font=(10), command=self.pause)

self.pauseBut.place(x=335, y=450)

self.startBut = Button(self.win, text="开始", height=1, width=12, font=(10), command=self.startgame)

self.startBut.place(x=335, y=483)

self.restartBut = Button(self.win, text="重新开始", height=1, width=12, font=(10), command=self.restart)

self.restartBut.place(x=335, y=516)

self.quitBut = Button(self.win, text="退出", height=1, width=12, font=(10), command=self.win.quit) #self.quitgame)

self.quitBut.place(x=335, y=549)

self.lab_score = Label(self.win, text="分数:0", font=(24))

self.lab_score.place(x=335, y=50)

self.lab_grade = Label(self.win, text="等级:1", fg='red', font=(24))

self.lab_grade.place(x=335, y=70)

self.check_box1 = Checkbutton(self.win, text="难度", variable=self.nandu_stat, height=1, width=3)

# 菜单

self.initgame()

# self.test = True

#for i in range(12):

# self.base_map[29 - i] = [1] * 15 + [0] * 1

#self.base_map[28][2] = 0

#self.base_map[24][5] = 0

#self.base_map[20][9] = 0

self.menu = Menu(self.win)

self.win.config(menu=self.menu)

self.startMenu = Menu(self.menu)

self.menu.add_cascade(label='游戏', menu=self.startMenu)

self.startMenu.add_command(label='开始', command=self.startgame)

self.startMenu.add_separator()

self.startMenu.add_command(label='重新开始', command=self.restart)

self.exitMenu = Menu(self.menu)

self.menu.add_cascade(label='退出', command=self.quitgame)

self.setMenu = Menu(self.win)

self.menu.add_cascade(label='设置', menu=self.setMenu)

self.setMenu.add_command(label='颜色', command=self.set_color)

# self.setMenu.add_command(label='难度', command=self.set_nandu)

# self.helpMenu.add_command(label='How to play', command=self.rule)

# self.helpMenu.add_separator()

# self.helpMenu.add_command(label='About...', command=self.about)

# self.huabu.focus_set()

self.huabu.bind_all('', self.move_left)

self.huabu.bind_all('', self.move_right)

self.huabu.bind_all('', self.rotate)

# self.huabu.bind_all('', self.change)

self.huabu.bind_all('', self.quick_drop)

self.huabu.bind_all('', self.move_left)

self.huabu.bind_all('', self.move_right)

self.huabu.bind_all('', self.rotate)

self.huabu.bind_all('', self.quick_drop)

self.huabu.bind_all('', self.down_straight)

self.huabu.place(x=2, y=2)

self.huabu_right.place(x=335, y=200)

self.check_box1.place(x=335,y=100)

self.fangkuai_map = [[fangk(self.huabu, i, j) for i in range(COLUMN)] for j in range(ROW)]

# self.startgame()

self.win.mainloop()

def set_nandu(self):

self.nandu_stat = not self.nandu_stat

def nandu(self):

if self.nandu_line > 10:

self.nandu_line = 0

self.base_map.pop(0)

self.base_map.append([0] + [1] * 15) # [random.randrange(0, 2) for i in range(16)])

self.color_map.pop(0)

self.color_map.append([random.randrange(0, 17) for i in range(16)])

self.combind()

self.draw_map()

self.win.update()

def set_color(self):

self.muti_color = not self.muti_color

def pause(self):

messagebox.showinfo("暂停", "游戏暂停中")

def restart(self):

messagebox.askquestion("重新开始", "确定要重新开始游戏吗?")

for i in self.huabu.find_all():

self.huabu.delete(i)

self.initgame()

self.startgame()

def cal_score(self, row):

self.score = self.score + [row * 10, int(row * 10 * (1 + row / 10))][self.last_row == row]

self.lab_score.config(text="分数:" + str(self.score))

self.last_row = row

self.sum_row += row

self.grade = self.sum_row // 50 + 1

self.lab_grade.config(text="等级:" + str(self.grade))

if self.nandu_stat:

self.nandu_line += row

self.nandu()

def initgame(self):

self.map = [[0] * COLUMN for _ in range(ROW)]

self.map_before = [[0] * COLUMN for _ in range(ROW)]

self.base_map = [[0] * COLUMN for _ in range(ROW)]

self.color_map = [[0] * COLUMN for _ in range(ROW)]

self.score = 0

self.lock_operation = False

self.speed = 20

self.last_row = 0

self.sum_row = 0

self.grade = 1

self.interval = 0

# self.nandu_stat = True

self.nandu_line = 0

self.next_fangk_type = random.randrange(0, 19)

self.next_color = random.randrange(0, 17)

self.lab_score.config(text="分数:0")

self.lab_grade.config(text="等级:1")

self.muti_color = True # 设置是否启用多色彩,还未弄

def quitgame(self):

q = messagebox.askquestion("退出", "确定要退出吗?")

if q == 'yes': self.win.destroy(); exit()

def startgame(self):

self.check_box1.config(state=DISABLED)

self.startBut.config(state=DISABLED)

self.next_fk()

while not self.lock_operation:

time.sleep(0.05)

if self.interval == 0: self.drop()

self.interval = (self.interval + 1) % (22 - self.grade * 2)

self.win.update()

def flash(self, del_rows):

self.lock_operation = True

for times in range(6):

for j in del_rows:

for i in self.fangkuai_map[j]:

i.setvisible(int(0.5 + times % 2 * 0.5))

self.win.update()

time.sleep(0.2)

self.lock_operation = False

def next_fk(self):

self.cur_color = self.next_color

self.cur_fk_type = self.next_fangk_type

self.next_color = random.randrange(0, 17)

self.next_fangk_type = random.randrange(0, 19)

for i in self.huabu_right.find_all():

self.huabu_right.delete(i)

for i in range(4):

fangk(self.huabu_right, 2 + self.fk_type[self.next_fangk_type][0][i],

2 - self.fk_type[self.next_fangk_type][1][i]).set_color(COLOR[self.next_color]).setvisible(1)

self.cur_fk = self.fk_type[self.cur_fk_type]

self.cur_location = [{'x': 7, 'y': 1}, {'x': 7, 'y': 0}][self.cur_fk_type in (2, 11, 17)]

self.combind()

self.draw_map()

if not self.test_map():

messagebox.showinfo("失败", "游戏失败了")

self.lock_operation = True

def rotate(self, event):

if not self.lock_operation:

if self.cur_fk_type != 0:

temp = self.cur_fk_type

self.cur_fk_type = [(self.cur_fk_type - 7) // 4 * 4 + self.cur_fk_type % 4 + 7,

(self.cur_fk_type - 1) // 2 * 2 + self.cur_fk_type % 2 + 1][

self.cur_fk_type in range(1, 7)]

self.cur_fk = self.fk_type[self.cur_fk_type]

if self.cur_location['x'] + min(self.cur_fk[0]) + 1 <= 0 or self.cur_location['x'] + max(

self.cur_fk[0]) >= COLUMN or not self.test_map() or self.cur_location['y'] + min(

self.cur_fk[1]) + 1 < 0:

print('testmap')

self.cur_fk_type = temp

self.cur_fk = self.fk_type[self.cur_fk_type]

self.combind()

self.draw_map()

def combind(self):

self.map = [a[:] for a in self.base_map]

for i in range(len(self.cur_fk[1])):

x = self.cur_location['x'] + self.cur_fk[0][i]

y = self.cur_location['y'] - self.cur_fk[1][i]

self.map[y][x] = 1

self.color_map[y][x] = self.cur_color

def test_map(self):

for i in range(len(self.cur_fk[0])):

x = self.cur_location['x'] + self.cur_fk[0][i]

y = self.cur_location['y'] - self.cur_fk[1][i]

if self.base_map[y][x] > 0: return False

return True

def draw_map(self):

for i in range(ROW):

for j in range(COLUMN):

if self.map[i][j] != self.map_before[i][j]:

self.fangkuai_map[i][j].set_color(COLOR[self.color_map[i][j]]).setvisible(self.map[i][j])

self.map_before = [i[:] for i in self.map]

self.win.update()

def quick_drop(self, event):

if not self.lock_operation: self.drop()

def drop(self):

self.cur_location['y'] += 1

if self.cur_location['y'] - min(self.cur_fk[1]) < ROW and self.test_map():

self.combind()

self.draw_map()

return True

else:

self.cur_location['y'] -= 1

self.base_map = [i[:] for i in self.map]

self.delete_row()

self.draw_map()

self.next_fk()

return False

def delete_row(self):

del_row = []

for i in range(max(self.cur_fk[1]) - min(self.cur_fk[1]) + 1):

if self.base_map[self.cur_location['y'] - min(self.cur_fk[1]) - i] == [1] * COLUMN:

del_row.append(self.cur_location['y'] - min(self.cur_fk[1]) - i)

if not del_row == []:

self.flash(del_row)

self.base_map = [r for r in self.base_map if not r == [1] * COLUMN]

self.base_map = ([[0] * COLUMN] * (30 - len(self.base_map))) + self.base_map

self.cal_score(len(del_row))

def move_left(self, event):

if not self.lock_operation:

self.cur_location['x'] -= 1

if self.cur_location['x'] + min(self.cur_fk[0]) + 1 > 0 and self.test_map():

self.combind()

self.draw_map()

else:

self.cur_location['x'] += 1

def move_right(self, event):

if not self.lock_operation:

self.cur_location['x'] += 1

if self.cur_location['x'] + max(self.cur_fk[0]) < COLUMN and self.test_map():

self.combind()

self.draw_map()

else:

self.cur_location['x'] -= 1

def down_straight(self, event):

while not self.lock_operation and self.drop(): pass

# def change(self, event):

# self.cur_fk_type = (self.cur_fk_type + 1) % 18

# self.cur_fk = self.fk_type[self.cur_fk_type]

# self.combind()

# self.draw_map()

elsfk()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/245553.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

intent android 匿名,Android 匿名启动activity 启动系统activity

一般我们使用Intent 进行activity跳转时我们都知道需要跳转的activity的名字,例如&#xff1a;Intent intentnew Intent(FirstActivity.this,SecondActitivy.class);startActivity(intent);当SecondActitivy.class和FirstActivity不再同一个App的时候,我们就需要用到匿名启动&a…

opencv python教程简书_Python-OpenCV —— 基本操作一网打尽

OpenCV是一个基于BSD许可&#xff08;开源&#xff09;发行的跨平台计算机视觉库&#xff0c;可以运行在Linux、Windows、MacOS操作系统上。它轻量级而且高效——由一系列 C 函数和少量C类构成&#xff0c;同时提供了Python、Ruby、MATLAB等语言的接口&#xff0c;实现了图像处…

android listview 数据同步,android中ListView数据刷新时的同步方法

本文实例讲述了android中ListView数据刷新时的同步方法。分享给大家供大家参考。具体实现方法如下&#xff1a;public class Main extends BaseActivity {private static final String TAG "tag";private static final int STATUS_CHANGE 0;ExpandableListView mEl…

python __reduce__魔法方法_Python魔法方法指南

(译)Python魔法方法指南 简介 本指南归纳于我的几个月的博客&#xff0c;主题是 魔法方法 。 什么是魔法方法呢&#xff1f;它们在面向对象的Python的处处皆是。它们是一些可以让你对类添加“魔法”的特殊方法。 它们经常是两个下划线包围来命名的&#xff08;比如 __init__ &a…

opengles 顶点数组 android,OpenGLES顶点属性、顶点数组和缓冲区对象

顶点属性数据可以用一个顶点数组对每个顶点指定&#xff0c;也可以将一个常量值用于一个图元的所有顶点OpenGLES支持最少16个顶点属性。准确查询顶点数量方法如下&#xff1a;GLint maxVertexAttribs;glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);一、指定顶点…

java 实体类 临时注解_JPA:Java持久层API--配置流程

一、JPA概述1.1 JPA是什么JPA &#xff08;Java Persistence API&#xff09; Java持久化API。是一套Sun公司 Java官方制定的ORM 方案,是规范&#xff0c;是标准 &#xff0c;sun公司自己并没有实现 关注点&#xff1a; ORM &#xff0c;标准 概念 &#xff08;关键字&#xf…

android新架构,Android新架构组件 LifeCycles 简介

一、前言为了使开发者能尽快在 Android 平台上开发出高质量的项目&#xff0c;Android 官方推出了 Android Jetpack 项目&#xff0c;旨在从基础&#xff0c;架构&#xff0c;行为以及界面 4 大方面体系化地为我们提供组件级别的支持。当然&#xff0c;在实际开发过程中&#x…

领域驱动设计 pdf_什么是领域驱动设计?

什么是领域驱动设计&#xff1f;你可能使用领域驱动设计(DDD)开发了一些项目。你可能很满意&#xff0c; 使用领域模型来开发领域业务。并且得意地展示给你的同事看&#xff0c;他们会说“666”。但有的时候你使用领域模型你总觉得哪儿有点不对劲。你会嘀咕你可能遗漏了什么。 …

Android四级缓存,RecyclerView 源码四级缓存原理

入口我们从使用功能上去读取源码&#xff0c;通常的用法是这个样子-> 我们设置layoutmanager&#xff0c;GridLayouManager 继承LinearLayoutManager&#xff0c;所以我们就LinearLayoutManager 为基准查看rv.layoutManager GridLayoutManager(this,5)rv.addItemDecoration…

织梦自定义html文本,织梦自定义标签dede:sql根据自定义字段填的文章id获取相关文章...

这篇文章主要为大家详细介绍了织梦自定义标签dede:sql根据自定义字段填的文章id获取相关文章&#xff0c;具有一定的参考价值&#xff0c;感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。有的时候我们需要通过织梦的dede:sql据自定义字段填的文章id获取相关文…

python 杀死子进程_Python:当父异常终止时,如何杀死子进程?

小编典典 呵呵&#xff0c;我昨天自己在研究这个&#xff01;假设您无法更改子程序&#xff1a; 在Linux上&#xff0c;prctl(PR_SET_PDEATHSIG,...)可能是唯一可靠的选择。&#xff08;如果绝对有必要终止子进程&#xff0c;那么您可能希望将终止信号设置为SIGKILL而不是SIGTE…

html评论置顶功能,微信公众号精选留言评论怎么置顶显示?功能在哪里设置?...

微信公众号精选留言怎么置顶&#xff1f;微信公众号留言功能新增了置顶精选留言的设置&#xff0c;那么微信公众号留言功能在哪里设置呢&#xff1f;下文小乐哥给大家介绍一下&#xff01;微信公众号精选留言怎么置顶&#xff1f;微信公众平台悄然上线了一个新功能&#xff0c;…

python函数增强代码可读性_写Python必须知道的这几个代码技巧!你会吗?

Day09 函数的初始 函数&#xff1a;函数是以功能为导向&#xff0c;一个函数封装一个功能。登录&#xff0c;注册&#xff0c;文件的改的操作。。。 函数减少代码的重复性&#xff0c;增强了代码的可读性&#xff1b; 获取任意一个字符串的元素的个数 s1 "xiaomingxiaoho…

shell脚本发邮件内容html,[转]Shell脚本中发送html邮件的方法

作为运维人员&#xff0c;免不了要编写一些监控脚本&#xff0c;并将监控结果及时的发送出来。那么通过邮件发送是比较常用的一种通知方式了。通常的&#xff0c;如果需要发送的内容是简单的文本文件&#xff0c;那么使用/bin/mailx就可以了&#xff0c;但是如果想要发送更复杂…

learn python app v3_‎App Store 上的“Learn Python and Scratch”

Learn “Python and Scratch Programming” from AI driven coach and satisfy your thirst for knowledge. App offers bite sized videos, quizzes and AI driven coach to help you become smarter and become great. Just 60 minutes a week can help you become great in …

计算机应用基础知识竞赛题,计算机基础知识题库

随着科学技术的进步&#xff0c;计算机已逐渐渗入人们的生活中&#xff0c;相应的计算机知识是需要具备的&#xff0c;那么你对计算机基础知识了解多少呢?以下是由学习啦小编整理关于计算机基础知识题库的内容&#xff0c;希望大家喜欢!计算机基础知识题库一、单选题1、 第一台…

inspect python_python之inspect模块

inspect模块主要提供了四种用处&#xff1a; 1.对是否是模块、框架、函数进行类型检查 2.获取源码 3.获取类或者函数的参数信息 4.解析堆栈 一、type and members 1. inspect.getmembers(object[, predicate]) 第二个参数通常可以根据需要调用如下16个方法&#xff1b; 返回值为…

HTML打开网页拒绝访问,192.168.1.1拒绝访问怎么办?

问&#xff1a;为什么设置路由器时&#xff0c;在浏览器中输入192.168.1.1&#xff0c;结果显示拒绝访问&#xff0c;这个问题怎么解决&#xff1f;答&#xff1a;如果是在设置路由器的时候&#xff0c;登录192.168.1.1被拒绝访问&#xff0c;多半是你自己操作有问题导致的&…

python中goto的用法_python3里用goto

python里用goto也是小Pa最近做的项目里的一个需求。python不像C有自带的goto, 需要用额外的包&#xff0c;目前为止&#xff0c;小pa只看到2个goto的包: 这2个小Pa都下载试用过&#xff0c;goto因为开发的时候比较早&#xff0c;对于python3的支持不太好&#xff0c;不推荐使用…

delphi打印html文件路径,Delphi获取文件名、不带扩展名文件名、文件所在路径、上级文件夹路径的方法...

1.获取不带扩展名的文件名方法&#xff0c;利用ChangeFileExt函数修改传入参数的扩展为空&#xff0c;并不会对文件本身产生变更。ChangeFileExt(ExtractFileName(‘D:\KK\Test\123.txt‘),‘‘); //返回 1232.获取上级文件夹路径的方法。ExtractFileDir(‘D:\KK\Test\‘)‘..‘…