2021-2022-1 20211325《信息安全专业导论》第九周学习总结
2021-2022-1 20211325《信息安全专业导论》第九周学习总结
第九周
学习目标
- 操作系统责任
- 内存与进程管理
- 分时系统
- CPU调度
- 文件、文件系统
- 文件保护
- 磁盘调度
学习资源
- 教材
- 阅读 「反作弊」:任何时候发现同学们有抄袭作业,抄袭代码的情况,即时课程总成绩清零
学习任务
-
加入云班课,参考本周学习资源
-
自学教材
- 计算机科学概论第10,11章 并完成云班课测试
- 《看漫画学Python》第12,16章并完成云班课测试
教材内容总结:
1.操作系统的功能:
1、进程管理:进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位。
2、内存管理:内存可以通过许多媒介实现,例如磁带或是磁盘,或是小阵列容量的微芯片。
3、网络通信:网络通信中最重要的就是网络通信协议。局域网中最常用的有三个网络协议:MICROSOFT的NETBEUI、NOVELL的IPX/SPX和TCP/IP协议。
4.安全机制:主要是指采用现代密码算法对数据进行主动保护,如数据保密、数据完整性、双向强身份认证等。
2.进程管理就是管理进程能知道进程使用CPU,内存,虚拟内存,使用多少,能结束某个进程。能结束不能关闭的程序。
内存管理:
计算机内存管理主要包括四个方面的内容:内存分配与回收、内存地址映射、内存共享与保护和内存扩充。
不同的操作系统环境采用不同的内存管理方法,常用的内存管理方式有这几种:单一连续分区存储管理、固定分区存储管理、可变分区存储管理、分页式存储管理、分段式存储管理。
地址映射:就要求操作系统能够根据程序的实际转载位置和程序的虚拟地址计算出各条指令和数据的物理地址,这个翻译的过程就是地址映射。
内存共享与保护:每道程序有自己私有的空间,为保证程序运行的正确性,该部分空间不允许其他程序访问和修改,这就需要内存保护。
内存扩充:不是指硬件上的扩充,而是指用存储管理软件来实现的逻辑扩充,用到的技术就是虚拟内存。
固定分区存储管理:内存管理最容易让人想到的就是将内存分成若干个连续的分区,在每一个分区中装入不同作业,从而实现多个程序的同时运行。
3.分时操作系统 (time-sharing system),“分时”的含义:分时是指多个用户分享使用同一台计算机。多个程序分时共享硬件和软件资源。分时操作系统是指在一台主机上连接多个带有显示器和键盘的终端,同时允许多个用户通过主机的终端,以交互方式使用计算机,共享主机中的资源。分时操作系统是一个多用户交互式操作系统。分时操作系统,主要分为三类:单道分时操作系统,多道分时操作系统,具有前台和后台的分时操作系统。分时操作系统将CPU的时间划分成若干个片段,称为时间片。操作系统以时间片为单位,轮流为每个终端用户服务。
4.CPU调度是多道程序操作系统的基础。通过在进程间切换 CPU,操作系统可以使得计算机更加高效。
对于单处理器系统,同一时间只有一个进程可以运行;其他进程都应等待,直到 CPU 空闲并可调度为止。多道程序的目标是,始终允许某个进程运行以最大化 CPU 利用率。
5.文件就是存放数据的载体,图像文件存放图像数据,文本文件存放文本数据,等等。
文件系统又叫文件管理系统,就是管理文件的存放,和文件的组织结构的框架,文件的所有操作,如:文件的查看,列表,复制,粘贴,删除,重命名等等,这些都属于文件系统的范畴。
6.通过文件加锁或访问权限控制,防止文件被破坏或不当访问的过程。
7.磁盘调度在多道程序设计的计算机系统中,各个进程可能会不断提出不同的对磁盘进行读/写操作的请求。由于有时候这些进程的发送请求的速度比磁盘响应的还要快,因此我们有必要为每个磁盘设备建立一个等待队列,常用的磁盘调度算法有以下四种:先来先服务算法(FCFS),最短寻道时间优先算法(SSTF),扫描算法(SCAN),循环扫描算法(CSCAN)
##遇到的问题和解决方法
问题1:在编写俄罗斯方块时,遇到插件安装失败情况
解决:清理了多余的解释器
问题二:screen安装不成功
解决:查询CSDN发现screen
from pgzero.actor import Actor from pgzero.keyboard import keyboard from pgzero.loaders import sounds import pgzrun from math import sqrt from math_funcs import * from pkg_resources.extern import packaging SIZE_X = 30 SIZE_Y = 30 N_X = 25 N_Y = 19 WIDTH = SIZE_X * N_X HEIGHT = SIZE_Y * N_Y MAXIMUM_VELOCITY_X = 2 JUMP_INIT_VELOCITY = 3 JUMP_ACCELERATION = 0.3 ACCELERATING_PERIOD = 10 COEFFICIENT_OF_RESTITUTION = 0.5 GRAVITY = 0.2 RADIUS_OF_KID = 5 class Actors(object): """Actor类作人物和子弹等可动物体父类 :param image_name 外部素材图片文件名 :param init_x, init_y 初始位置的方块坐标 :param shape 碰撞箱形状 """ def __init__(self, image_name, init_x, init_y): self.x_velocity = 0 self.y_velocity = 0 self.shape = '' self.act = Actor(image_name, (init_x, init_y)) class Kid(Actors): """ Kid类 用户操纵人物的单实例类 :param jumping,jumped,has_jumped,jump1,jump1 用于实现可长按跳跃与二段跳跃算法的相关变量 :param gravity 实现重力效果 :param visible 可见性用于处理死亡事件 :param rebirth_location 重生点用于处理存档、复活事件 :param shape kid的碰撞箱形状为圆形 :param radius radius为碰撞箱圆半径 :param x_image, y_image 用于处理kid贴图 """ def __init__(self, image_name, init_x, init_y): """初始化实例""" super().__init__(image_name, init_x, init_y) self.visible = True self.rebirth_location = 1 self.gravity = GRAVITY self.shape = 'circle' self.radius = RADIUS_OF_KID self.jumping = False self.jump1 = 0 self.jump2 = 0 self.jumped = False self.has_junmped = False self.x_image = 'right' self.y_image = 'stand' def move_x(self, scene): """处理左右移动事件""" self.layout_check(scene) if keyboard.d and not self.block_check('right', scene): self.x_velocity = MAXIMUM_VELOCITY_X elif keyboard.a and not self.block_check('left', scene): self.x_velocity = -MAXIMUM_VELOCITY_X else: self.x_velocity = 0 self.act.x += self.x_velocity def jump(self, scene): """配合move_y处理跳跃事件""" if self.block_check('up', scene): self.y_velocity = -COEFFICIENT_OF_RESTITUTION * self.y_velocity if keyboard.k and self.jump1 == 0: self.y_velocity = -JUMP_INIT_VELOCITY self.jump1 += 1 self.jumping = True if keyboard.k and self.jump1 != 0 and self.jump2 == 0: self.jump1 += 1 if self.jump1 < ACCELERATING_PERIOD: self.y_velocity -= JUMP_ACCELERATION if not keyboard.k and self.jump1 != 0: self.jumped = True if keyboard.k and self.jumped and self.jump2 == 0: self.y_velocity = -JUMP_INIT_VELOCITY self.jump2 += 1 if keyboard.k and self.jumped and self.jump2 != 0: self.jump2 += 1 if self.jump2 < ACCELERATING_PERIOD: self.y_velocity -= JUMP_ACCELERATION if not keyboard.k and self.jump2 != 0: self.has_junmped = True if self.jump_check(scene) and self.jumped: self.jumped = False self.has_junmped = False self.jumping = False self.jump1 = 0 self.jump2 = 0 def fall(self, scene): """配合move_y处理重力下落""" if self.block_check('down', scene): self.y_velocity = 0 else: self.y_velocity += self.gravity def move_y(self): """配合jumping, fall实现跳跃和重力""" self.act.y += self.y_velocity def image_refresh(self): """根据kid的运动方向刷新贴图""" if self.x_velocity > 0: self.x_image = 'right' elif self.x_velocity < 0: self.x_image = 'left' if self.y_velocity == 0: self.y_image = 'stand' elif self.y_velocity > 0: self.y_image = 'jump' elif self.y_velocity < 0: self.y_image = 'fall' self.act.image = self.y_image + self.x_image def shoot(self): """实现kid的射击动作""" pass def die(self, scene): """处理死亡事件:碰刺或者掉出界面""" if (self.lethal_check(scene) or self.act.y > 570)and not scene.blackcurtain: scene.death += 1 sounds.diesound.play() self.visible = False scene.texts.append(Text("Press R to retry!", center=(WIDTH / 2, HEIGHT / 2 + 150), fontsize=30, color='white')) scene.texts.append(Text("GAME OVER", center=(WIDTH / 2, HEIGHT / 2 - 100), fontsize=100, color='white')) scene.blackcurtain = True if keyboard.r: if scene.blackcurtain: for i in range(len(scene.texts)): scene.texts.pop() scene.blackcurtain = False self.visible = True self.goback_rebirth(scene) def goback_rebirth(self, scene): """处理复活事件""" self.act.x = scene.savers[self.rebirth_location - 1].act.x self.act.y = scene.savers[self.rebirth_location - 1].act.y def jump_check(self, scene): """更新跳跃条件""" for obj in scene.blocks: if isinstance(obj, Block) and obj.jumpable: delta_x = obj.act.x - self.act.x delta_y = obj.act.y - self.act.y delta_x0 = self.radius + obj.size_x / 2 delta_y0 = self.radius + obj.size_y / 2 if 0 < delta_y <= delta_y0 and abs(delta_x) < delta_x0: return True return False def block_check(self, direction, scene): """为掉落、跳跃等事件服务的方块碰撞检测函数""" for obj in scene.blocks: if self.collide_check(obj, direction): return True return False def lethal_check(self, scene): """死亡物碰撞检测事件""" for obj in scene.lethals: if self.collide_check(obj, None): return True return False def layout_check(self, scene): """布局物块的碰撞检测""" for obj in scene.layouts: if self.act.colliderect(obj.act) and keyboard.j: if isinstance(obj, Saver) and obj.visible == True: obj.action(self, scene) if isinstance(obj, Ending): obj.action(scene) if isinstance(obj, Trigger): obj.action() def collide_check(self, obj, direction): """为方块碰撞检测,死亡物碰撞检测提供的不同形状物体的碰撞检测算法""" collide = False if obj.shape == 'rectangle' and obj.collidable: delta_x = obj.act.x - self.act.x delta_y = obj.act.y - self.act.y delta_x0 = self.radius + obj.size_x / 2 delta_y0 = self.radius + obj.size_y / 2 if direction == "right" and 0 < delta_x <= delta_x0 and abs(delta_y) < delta_y0: if abs(delta_x) > abs(delta_y): self.act.x = obj.act.x - delta_x0 collide = True if direction == 'left' and 0 < -delta_x <= delta_x0 and abs(delta_y) < delta_y0: if abs(delta_x) > abs(delta_y): self.act.x = obj.act.x + delta_x0 collide = True if direction == 'down' and 0 < delta_y <= delta_y0 and abs(delta_x) < delta_x0: if abs(delta_x / delta_y) < 1: self.act.y = obj.act.y - delta_y0 collide = True if direction == 'up' and 0 < -delta_y <= delta_y0 and abs(delta_x) < delta_x0: if abs(delta_x / delta_y) < 1: self.act.y = obj.act.y + delta_y0 collide = True if obj.shape == 'triangle': collide = is_circle_intersect_triangle(self.act.x, self.act.y, self.radius, obj.x1, obj.y1, obj.x2, obj.y2, obj.x3, obj.y3) return collide class Bullet(Actors): """子弹类,尚未实现""" def __init__(self, image_name, init_x, init_y, direction): super().__init__(image_name, init_x, init_y) self.direction = direction def move(self): pass def perish(self): pass class Objects(object): """物块、致死物、布局物的父类 :param image_name 外部素材图片文件名 :param init_x, init_y 初始位置的方块坐标 """ def __init__(self, image_name, init_x, init_y): self.act = Actor(image_name, (init_x, init_y)) self.shape = '' self.collidable = True self.jumpable = True self.lethal = False class Block(Objects): """可以踩踏 不能穿过 不致死的物块类,做父类""" def __init__(self, image_name, init_x, init_y): super().__init__(image_name, init_x, init_y) self.collidable = True self.jumpable = True self.lethal = False self.size_x = 0 self.size_y = 0 class Brick(Block): """最一般的标准1×1砖块""" def __init__(self, image_name, init_x, init_y): super().__init__(image_name, init_x, init_y) self.shape = 'rectangle' self.size_x = 30 self.size_y = 30 class Board(Block): """会移动的板子,尚未实现""" pass class Shattered_brick: """踩踏后会碎裂的砖块,尚未实现""" pass class Lethal(Objects): """致死物的父类""" def __init__(self, image_name, init_x, init_y): super().__init__(image_name, init_x, init_y) self.lethal = True class Thorn(Lethal): """最一般的致死物尖刺 :param x1, y1, x2, y2, x3, y3 为圆形与三角形碰撞检测留下的三个顶点坐标参数 """ def __init__(self, image_name, init_x, init_y, direction='up'): """根据刺的方向对刺的图片素材和顶点坐标做初始化""" super().__init__(image_name, init_x, init_y) self.shape = 'triangle' if direction == 'up': self.act.image = self.act.image + 'up' self.x1 = init_x self.y1 = init_y - 10 * sqrt(3) self.x2 = init_x - 15 self.y2 = init_y + 5 * sqrt(3) self.x3 = init_x + 15 self.y3 = init_y + 5 * sqrt(3) elif direction == 'down': self.act.image = self.act.image + 'down' self.x1 = init_x self.y1 = init_y + 10 * sqrt(3) self.x2 = init_x - 15 self.y2 = init_y - 5 * sqrt(3) self.x3 = init_x + 15 self.y3 = init_y - 5 * sqrt(3) elif direction == 'left': self.act.image = self.act.image + 'left' self.x1 = init_x - 10 * sqrt(3) self.y1 = init_y self.x2 = init_x + 5 * sqrt(3) self.y2 = init_y + 15 self.x3 = init_x + 5 * sqrt(3) self.y3 = init_y - 15 elif direction == 'right': self.act.image = self.act.image + 'right' self.x1 = init_x + 10 * sqrt(3) self.y1 = init_y self.x2 = init_x - 5 * sqrt(3) self.y2 = init_y - 15 self.x3 = init_x - 5 * sqrt(3) self.y3 = init_y + 15 class Apple(Lethal): """致死物苹果,尚未实现""" pass class Dart(Lethal): """致死物忍者标,尚未实现""" pass class Vine(Objects): """可刷新跳跃的藤蔓,尚未实现""" pass class Water(Objects): """可以在其中游泳的水,尚未实现""" pass class Layout(Objects): """布局物:出生点、通关点、保存点,触发器的父类""" def __init__(self, image_name, init_x, init_y): super().__init__(image_name, init_x, init_y) self.collidable = False self.jumpable = False self.lethal = False def action(self, *args): """对于布局物需要触发的用于重写的动作函数""" pass class Trigger(Layout): """触发器类,用于设计机关和完成一些选择任务 :param func 在scene模块中设定的函数,传入地址,函数用于实现触发器触发后的功能 """ def __init__(self, image_name, init_x, init_y, func): super().__init__(image_name, init_x, init_y) self.func = func def action(self): """触发器的动作函数,调用scene中设计好的触发动作""" self.func() class Saver(Layout): """保存点类 :param n 保存点标号 :param difficulty 保存点的难度 保存点难度低于场景难度则可见 :param visible 可见性,在不同难度下保存点的可见性不同,对应影响不同难度下的保存点数量 """ def __init__(self, image_name, init_x, init_y, n, difficulty = 0, visible = True): """初始化相关参数""" super().__init__(image_name, init_x, init_y) self.num = n self.difficulty = difficulty self.visible = visible if self.num == 1: self.act.image = 'load' def action(self, kid, scene): """保存点动作函数,重置重生点为当前保存点,将以前保存点改为失效贴图,将当前保存点改为生效贴图""" if self.act.image == 'unload': sounds.loadsound.play() if kid.rebirth_location < self.num: kid.rebirth_location = self.num self.act.image = "load" for i in scene.savers: if i.num < self.num and i.visible: i.act.image = "hasload" class Starting(Layout): """出生点,尚未实现,现以第一个保存点代替""" pass class Ending(Layout): """通关点,切换到下一个场景""" def __init__(self, image_name, init_x, init_y): super().__init__(image_name, init_x, init_y) def action(self, scene): """通关点动作函数,将当前场景的结束属性设为True""" sounds.passsound.play() scene.over = True class Scene(object): """场景类 :param objects 场景内object的汇总 :param bricks,blocks,thorns,lethals,savers,endings,triggers,layouts 场景内object的分类及分类汇总 :param decorations,texts 场景内需要的装饰品、文本 :param blackcurtain 用于处理死亡黑幕 :param kid 场景内的kid :param waiting_j,need_press_j 用于处理等待按j的事件 :param over 当前场景是否结束 :param difficulty 场景的难度 :param brick_image, thorn_image 场景素材图片 :param death 记录当前scene死亡次数 :param sum_death 记录总死亡次数 :param loadfunc Scene的初始化函数 """ def __init__(self, loadfunc): """初始化相关变量""" self.objects = [] self.bricks = [] self.blocks = [] self.thorns = [] self.lethals = [] self.savers = [] self.endings = [] self.triggers = [] self.layouts = [] self.texts = [] self.decorations = [] self.brick_image = '' self.thorn_image = '' self.blackcurtain = False self.kid = None self.waiting_j = False self.need_press_j = False self.over = False self.difficulty = 0 self.death = 0 self.sum_death = 0 self.loadfunc = loadfunc def update(self): """场景内需要实时更新的函数""" self.kid.move_x(self) self.kid.jump(self) self.kid.move_y() self.kid.fall(self) self.kid.die(self) self.kid.image_refresh() def set_style(self, color): """设置场景风格接口""" self.brick_image = color + 'brick' self.thorn_image = color + 'thorn' def set_difficulty(self, difficulty): """用于设定、传递场景难度,改变不同难度下的保存点可见性""" self.difficulty = difficulty for saver in self.savers: if saver.difficulty < self.difficulty: saver.visible = False saver.act.image = 'null' def set_j(self): """处理一些等待j事件""" self.waiting_j = True def set_decorations(self,decorations): """创建装饰物接口""" for dec in decorations: self.decorations.append(Decoration(dec[0], dec[1], dec[2])) def set_texts(self,texts): """创建文本接口""" for text in texts: self.texts.append(Text(text=text[0], center=text[1], align=text[2], fontsize=text[3], color=text[4])) def set_bricks(self,bricks): """创建砖块接口""" for brick in bricks: self.bricks.append(Brick(self.brick_image, get_x(brick[0]), get_y(brick[1]))) def set_thorns(self,thorns): """创建尖刺接口""" for thorn in thorns: self.thorns.append(Thorn(self.thorn_image, get_x(thorn[0]), get_y(thorn[1]), thorn[2])) def set_savers(self,savers): """创建保存点接口""" for saver in savers: self.savers.append(Saver('unload', get_x(saver[0]), get_y(saver[1]), saver[2], saver[3])) def set_endings(self,endings): """创建通关点接口""" for ending in endings: self.endings.append(Ending('ending', get_x(ending[0]), get_y(ending[1]))) def set_triggers(self,triggers): """创建触发器接口""" for trigger in triggers: self.triggers.append(Trigger('null', get_x(trigger[0]), get_y(trigger[1]), trigger[2])) def append_objects(self): """汇总objects接口,用于初始化场景""" self.blocks += self.bricks self.lethals += self.thorns self.layouts += self.savers self.layouts += self.endings self.layouts += self.triggers self.objects = self.blocks + self.lethals + self.layouts def set_kid(self): """创建kid接口""" self.kid = Kid("standright", self.savers[0].act.x, self.savers[0].act.y) def load(self): self.loadfunc(self) class Text(object): """文本类,用于添加到场景中 :param text 文本字符串 :param center 文本中心点像素坐标 :param align 文本对齐方式 :param fontname 文本字体 :param fontsize 文本字号 :param color 文本颜色 """ def __init__(self, text, center=None, align=None, fontname='方正胖头鱼简体', fontsize=24, color='white'): self.text = text self.center = center self.align = align self.fontname = fontname self.fontsize = fontsize self.color = color class Decoration(object): """装饰类,用于添加到场景中 :param decoration 贴图素材文件名 :param x,y 贴图中心点的像素坐标 """ def __init__(self, decoration, x, y): self.decoration = decoration self.x = x self.y = y
from resources import * from math_funcs import * from random import choice """ 场景设计模组 制作流程: def load_scene_sample(scene): # 定义加载函数 scene.set_style(choice(colors)) # 随机进行场景风格化 scene.set_decorations(decorations) # 添加装饰 scene.set_texts(texts) # 添加文本 scene.set_bricks(bricks) # 添加砖块 scene.set_thorns(thorns) # 添加尖刺 scene.set_savers(savers) # 添加保存点 scene.set_endings(endings) # 添加通关点 def trigger_act_i(): pass # 制作触发器动作函数 scene.set_triggers(triggers) # 添加触发器 scene.set_kid() # 初始化kid scene.append_objects() # 汇总objects scenes.append(Scene(load_scene_sample)) # 将新建场景加到场景list中,将加载函数传给场景,以便随时加载 其中 decorations,texts,bricks,thorns,savers,endings,triggers 为tuple组成的tuple或list 每个元素tuple需要包含初始化对应实体的必要参数,具体可见resources.py中的scene类的对应接口方法 具体实例如下 """ VERSION = '1.5' scenes = [] colors = ['yellow', 'red', 'purple', 'orange', 'grey', 'green', 'cyangreen', 'cyanblue',] """------------------------------------SCENE_INIT--------------------------------------""" def load_scene_init(scene): scene.waiting_j = True scene.set_style('purple0') scene.set_decorations(( ('initial_menu', 0, 0), )) scene.set_texts(( ("Welcome to", (WIDTH / 2, 70), 'center', 100, 'white'), ("I wanna be the dinosaur", (WIDTH / 2, 150), 'center', 50, 'white'), ("Press J to continue...", (WIDTH / 2 + 240, 550), 'center', 20, 'white'), )) scene.set_bricks( [(x + 10, 13) for x in range(7)] + [(x + 10, 19) for x in range(7)] + [(10, x + 14) for x in range(5)] + [(16, x + 14) for x in range(5)] ) scene.set_savers(( (13, 18, 1, 4), )) scene.set_kid() scene.append_objects() """-----------------------------------SCENE_START-------------------------------------""" def load_scene_start(scene): scene.set_style(choice(colors)) scene.set_decorations(( ('scene1', 0, 0), )) scene.set_texts(( ("Press A/D to move left and right\nPress K for longer or shorter time and for once or twice to get\n\tfamiliar with the sensitivity of jumping\nPress R to go back to the loaded rebirth point when you need\nPress J at flagpole to load or at cup to choose the difficulty", (WIDTH / 2, 120), 'left', 19, 'white'), ("EASY", (get_x(5.5), get_y(14)), 'center', 16, 'white'), ("NORMAL", (get_x(12), get_y(14)), 'center', 16, 'white'), ("HARD", (get_x(17.5), get_y(14)), 'center', 16, 'white') )) scene.set_bricks( [(x + 1, 19) for x in range(24)] + [(x + 2, 1) for x in range(24)] + [(1, x + 1) for x in range(19)] + [(25, x + 2) for x in range(18)] + [(x + 2, 9) for x in range(23)] + [(x + 2, 15) for x in range(21)] ) scene.set_savers(( (3, 18, 1, 4), (21, 14, 2, 4) )) scene.set_endings(( (4, 14), (10, 14), (16, 14) )) def set_dif_1(): scene.set_difficulty(1) def set_dif_2(): scene.set_difficulty(2) def set_dif_3(): scene.set_difficulty(3) scene.set_triggers(( (4, 14, set_dif_1), (10, 14, set_dif_2), (16, 14, set_dif_3), )) scene.set_kid() scene.append_objects() """--------------------------------------SCENE1----------------------------------------""" def load_scene1(scene): scene.set_style(choice(colors)) scene.set_decorations(( ('scene2', 0, 0), )) scene.set_bricks( [(x + 1, 19) for x in range(24)] + [(x + 2, 1) for x in range(24)] + [(1, x + 1) for x in range(19)] + [(25, x + 2) for x in range(18)] + [(x + 2, 15) for x in range(20)] + [(x + 5, 11) for x in range(20)] + [(x + 2, 6) for x in range(21)] + [ (7, 2), (7, 3), (9, 2), (9, 3), (19, 3), (19, 4), (19, 5), (11, 9), (23, 10), ] ) scene.set_thorns(( (6, 18, 'up'), (8, 18, 'up'), (10, 18, 'up'), (13, 18, 'up'), (16, 18, 'up'), (19, 17, 'up'), (19, 18, 'up'), (20, 18, 'up'), (22, 15, 'right'), (24, 15, 'left'), (19, 12, 'down'), (18, 14, 'up'), (15, 12, 'down'), (14, 14, 'up'), (13, 12, 'down'), (10, 14, 'up'), (9, 14, 'up'), (8, 14, 'up'), (7, 14, 'up'), (4, 14, 'up'), (2, 12, 'right'), (4, 11, 'left'), (8, 10, 'up'), (8, 7, 'down'), (8, 8, 'down'), (13, 10, 'up'), (13, 9, 'up'), (13, 8, 'up'), (15, 10, 'up'), (15, 7, 'down'), (17, 10, 'up'), (18, 10, 'up'), (19, 10, 'up'), (20, 10, 'up'), (21, 10, 'up'), (18, 7, 'down'), (19, 7, 'down'), (23, 6, 'right'), (17, 2, 'down'), (17, 5, 'up'), (16, 5, 'up'), (15, 5, 'up'), (12, 5, 'up'), (10, 5, 'up'), (8, 5, 'up'), (6, 5, 'up'), (17, 4, 'up'), (10, 3, 'right'), (6, 3, 'left') )) scene.set_savers(( (3, 18, 1, 4), (21, 14, 2, 1), (5, 10, 3, 2), (22, 5, 4, 1) )) scene.set_endings(( (3, 5), )) scene.set_kid() scene.append_objects() """--------------------------------------SCENE2----------------------------------------""" def load_scene2(scene): scene.set_style(choice(colors)) scene.set_decorations(( ('scene3', 0, 0), )) scene.set_bricks( [(x + 1, 19) for x in range(24)] + [(x + 1, 1 ) for x in range(24)] + [(1 , y + 1) for y in range(19)] + [(25, y + 1) for y in range(19)] + [(x + 18, 2 ) for x in range(5)] + [(x + 13, 4 ) for x in range(4)] + [(x + 19, 5 ) for x in range(4)] + [(x + 17, 6 ) for x in range(4)] + [(x + 15, 7 ) for x in range(4)] + [(x + 13, 8 ) for x in range(6)] + [(x + 13, 9 ) for x in range(5)] + [(x + 12, 10) for x in range(5)] + [(x + 12, 11) for x in range(4)] + [(x + 5, 11) for x in range(5)] + [(x + 4, 12) for x in range(16)] + [(x + 4, 13) for x in range(12)] + [(x + 10, 14) for x in range(8)] + [(x + 9, 15) for x in range(3)] + [(x + 15, 16) for x in range(6)] + [ (24,2), (16,3), (17,3), (9,7), (10,7), (3,8), (4,8), (8,8), (9,8), (7,9), (8,9), (6,10), (7,10), (8,10), (3,14), (4,14), (6,14), (6,15), (3,15), (19,14), (19,15), (21,13), (21,14), (21,15), (23,13), (23,14), (23,15), (12,17), (13,17), (14,17), (8,16), (9,16), (8,17), (8,18), (2,18) ] ) scene.set_thorns(( (18, 3, 'down'), (19, 3, 'down'), (20, 3, 'down'), (21, 3, 'down'), (22, 3, 'down'), (24, 3, 'left'), (24, 4, 'left'), (24, 5, 'left'), (24, 6, 'left'), (24, 7, 'left'), (24, 8, 'left'), (24, 9, 'left'), (24, 10, 'left'), (21, 10, 'up'), (22, 10, 'up'), (23, 10, 'up'), (17, 4, 'down'), (13, 5, 'down'), (14, 5, 'down'), (9, 6, 'up'), (10, 6, 'up'), (7, 7, 'up'), (7, 8, 'up'), (5, 10, 'up'), (3, 12, 'left'), (11, 11, 'up'), (13, 7, 'up'), (3, 18, 'up'), (5, 18, 'up'), (13, 16, 'up'), (19, 11, 'up'), (19, 13, 'up'), (20, 15, 'up'), (22, 15, 'up'), (16, 18, 'up'), (19, 18, 'up'), (21, 18, 'up'), (22, 18, 'up'), (24, 18, 'up'), (21, 6, 'down'), (22, 6, 'down') )) scene.set_savers(( (5, 15, 1, 4), (10, 9, 2, 1), (23, 2, 3, 2), (17, 10, 4, 1) )) scene.set_endings(( (16, 13), )) scene.set_kid() scene.append_objects() """--------------------------------------SCENE3----------------------------------------""" def load_scene3(scene): scene.set_style(choice(colors)) scene.set_decorations(( ('scene4', 0, 0), )) scene.set_bricks( [(x + 1, 19) for x in range(24)] + [(x + 1, 1 ) for x in range(24)] + [(1 , y + 1) for y in range(19)] + [(25, y + 1) for y in range(19)] + [(x + 17, 3) for x in range(6)] + [(x + 7, 3) for x in range(2)] + [(x + 18, 4) for x in range(2)] + [(x + 13, 8) for x in range(3)] + [(x + 13, 9) for x in range(3)] + [(x + 10, 10) for x in range(13)] + [(x + 17, 11) for x in range(3)] + [(x + 18, 12) for x in range(3)] + [(x + 18, 14) for x in range(5)] + [(x + 12, 13) for x in range(4)] + [(x + 15, 15) for x in range(9)] + [(x + 14, 18) for x in range(3)] + [(x + 10, 18) for x in range(3)] + [(x + 10, 15) for x in range(4)] + [(x + 3, 16) for x in range(3)] + [(x + 3, 15) for x in range(4)] + [(x + 6, 14) for x in range(5)] + [(x + 4, 13) for x in range(6)] + [(x + 5, 12) for x in range(3)] + [ (7, 2), (5,6), (4,7), (18,5), (14,6), (14,7), (20,7), (19,8), (20,8), (18,9), (19,9), (9,8), (9,9), (10,9), (7,10), (2,11), (3,11), (5,11), (6,11), (10,11), (10,12), (9,12), (4,14), (21,13), (15,14), (12,16), (12,17), (16,17), (21,16), (22,16), (22,17), (23,17) ] ) scene.set_thorns(( (24, 2, 'left'), (24, 3, 'left'), (24, 4, 'left'), (24, 5, 'left'), (24, 6, 'left'), (24, 7, 'left'), (2, 12, 'down'), (2, 13, 'down'), (13, 16, 'down'), (14, 11, 'down'), (15, 11, 'down'), (2, 10, 'up'), (5, 10, 'up'), (6, 10, 'up'), (10, 8, 'up'), (11, 9, 'up'), (12, 9, 'up'), (15, 7, 'up'), (16, 9, 'up'), (17, 9, 'up'), (18, 8, 'up'), (19, 7, 'up'), (21, 7, 'up'), (22, 7, 'up'), (8, 11, 'up'), (8, 12, 'up'), (12, 12, 'up'), (16, 13, 'up'), (16, 14, 'up'), (17, 14, 'up'), (4, 18, 'up'), (5, 18, 'up'), (7, 18, 'up'), (8, 18, 'up'), (9, 18, 'up'), (8, 17, 'up'), (9, 17, 'up'), (15, 17, 'up'), (17, 17, 'up'), (17, 18, 'up'), (19, 17, 'up'), (19, 18, 'up'), (18, 18, 'up'), (20, 18, 'up'), (23, 10, 'right'), (24, 13, 'left') )) scene.set_savers(( (8, 15, 1, 4), (14, 5, 2, 1), (24,18, 3, 2), (13,18, 4, 1) )) scene.set_endings(( (20, 13), )) scene.set_kid() scene.append_objects() """--------------------------------------SCENE4----------------------------------------""" def load_scene4(scene): scene.set_style(choice(colors)) scene.set_decorations(( ('scene5', 0, 0), )) scene.set_bricks( [(x + 9, 1) for x in range(3)] + [(x + 7, 3) for x in range(5)] + [(13, y + 3) for y in range(3)] + [(x + 2, 5) for x in range(7)] + [(x + 11, 5) for x in range(2)] + [(1, y + 6) for y in range(3)] + [(1, y + 12) for y in range(3)] + [(x + 8, 10) for x in range(11)] + [(7, y + 11) for y in range(3)] + [(x + 2, 15) for x in range(6)] + [(7, y + 16) for y in range(4)] + [(x + 11, 19) for x in range(3)] + [(x + 15, 19) for x in range(3)] + [(19, y + 1) for y in range(8)] + [(x + 20, 5) for x in range(5)] + [(25, y + 6) for y in range(3)] + [(x + 18, 9) for x in range(3)] + [(x + 17, 13) for x in range(2)] + [(23, y + 9) for y in range(5)] + [(x + 21, 11) for x in range(2)] + [(25, y + 12) for y in range(3)] + [(10, y + 12) for y in range(5)] + [(x + 13, 15) for x in range(12)] + [(13, y + 16) for y in range(2)] + [(x + 14, 17) for x in range(6)] + [(19, y + 18) for y in range(2)] + [ (7, 1), (2, 6), (2, 14), (8, 7), (9, 8), (9, 9), (8, 11), (18, 5), (24, 6), (24, 14), (11, 16) ] ) scene.set_thorns(( (1, 1, 'right'), (1, 2, 'right'), (1, 3, 'right'), (1, 4, 'right'), (2, 1, 'down'), (3, 1, 'down'), (4, 1, 'down'), (5, 1, 'down'), (6, 1, 'down'), (6, 3, 'left'), (8, 1, 'down'), (9, 5, 'up'), (10, 5, 'up'), (12, 1, 'down'), (13, 1, 'down'), (14, 1, 'down'), (14, 4, 'up'), (15, 4, 'up'), (16, 4, 'up'), (14, 7, 'right'), (15, 7, 'right'), (17, 7, 'left'), (18, 7, 'left'), (16, 9, 'up'), (12, 8, 'up'), (12, 9, 'up'), (1, 9, 'right'), (1, 10, 'right'), (1, 11, 'right'), (3, 12, 'right'), (5, 12, 'left'), (6, 12, 'left'), (4, 14, 'up'), (8, 19, 'up'), (9, 19, 'up'), (10, 19, 'up'), (14, 19, 'up'), (18, 19, 'up'), (12, 14, 'left'), (13, 14, 'up'), (14, 11, 'left'), (15, 11, 'down'), (15, 12, 'down'), (19, 13, 'right'), (20, 12, 'down'), (21, 14, 'up'), (21, 9, 'right'), (23, 8, 'up'), (25, 9, 'left'), (25, 10, 'left'), (24, 13, 'up') )) scene.set_savers(( (2, 2, 1, 4), (18, 2, 2, 1), (2, 13, 3, 2), (23, 14, 4, 1) )) scene.set_endings(( (25, 11), )) scene.set_kid() scene.append_objects() """--------------------------------------SCENE5----------------------------------------""" def load_scene5(scene): scene.set_style(choice(colors)) scene.set_decorations(( ('scene6', 0, 0), )) scene.set_bricks( [(x + 15, 19) for x in range(10)] + [(x + 6, 15) for x in range(3)] + [(14, y + 12) for y in range(5)] + [(8, y + 10) for y in range(3)] + [(13, x + 8) for x in range(3)] + [(x + 23, 12) for x in range(3)] + [(x + 5, 7) for x in range(3)] + [(x + 9, 7) for x in range(3)] + [(17, x + 1) for x in range(4)] + [(x + 11, 0) for x in range(6)] + [ (21, 1), (13, 2), (14, 2), (23, 2), (8, 3), (12, 4), (21, 4), (24, 4), (2, 5), (13, 6), (16, 5), (23, 5), (20, 7), (22, 7), (3, 8), (20, 9), (22, 9), (10, 10), (22, 11), (7, 12), (21, 12), (4, 14), (11, 14), (23, 14), (2, 15), (18, 15), (2, 16), (10, 16), (11, 16), (25, 16), (3, 17), (13, 17), (17, 17), (10, 19), (11, 19), (3, 10), (14, 11), (19, 10), (20, 3), (14, 18), (13, 7), (12, 7), ] ) scene.set_thorns(( (9, 19, 'left'), (11, 18, 'up'), (19, 18, 'up'), (20, 18, 'up'), (21, 18, 'up'), (22, 18, 'up'), (23, 18, 'up'), (16, 17, 'left'), (20, 17, 'up'), (22, 17, 'up'), (17, 16, 'up'), (4, 15, 'down'), (10, 15, 'up'), (11, 15, 'up'), (2, 14, 'left'), (3, 14, 'left'), (7, 14, 'up'), (8, 14, 'up'), (13, 14, 'left'), (18, 14, 'up'), (4, 13, 'up'), (6, 12, 'left'), (9, 12, 'right'), (15, 12, 'right'), (22, 12, 'down'), (7, 11, 'up'), (9, 11, 'right'), (25, 11, 'left'), (20, 10, 'down'), (22, 10, 'down'), (25, 10, 'left'), (14, 9, 'right'), (23, 9, 'right'), (25, 9, 'left'), (4, 8, 'down'), (5, 8, 'down'), (6, 8, 'down'), (7, 8, 'down'), (8, 8, 'down'), (9, 8, 'down'), (10, 8, 'down'), (11, 8, 'down'), (28, 8, 'right'), (20, 8, 'left'), (22, 8, 'right'), (25, 8, 'left'), (8, 7, 'up'), (21, 8, 'up'), (25, 7, 'left'), (2, 6, 'down'), (6, 6, 'up'), (7, 6, 'up'), (9, 6, 'up'), (10, 6, 'up'), (23, 6, 'down'), (2, 4, 'up'), (8, 4, 'down'), (16, 4, 'up'), (23, 3, 'down'), (7, 2, 'left'), (9, 2, 'right'), (21, 2, 'down'), (7, 1, 'left'), (9, 1, 'right'), (13, 5, 'up'), (13, 3, 'down'), (15, 5, 'left'), (16, 6, 'down'), (21, 5, 'down'), (8, 16, 'down'), (8, 17, 'down'), (8, 18, 'down'), (12, 8, 'down') )) scene.set_savers(( (3, 16, 1, 4), (8, 9, 2, 1), (11, 6, 3, 2), (19, 9, 4, 1), )) scene.set_endings(( (23, 11), )) scene.set_kid() scene.append_objects() """----------------------------------SCENE_TERMINAL------------------------------------""" def load_scene_terminal(scene): scene.need_press_j = True scene.set_style('red0') scene.set_decorations(( ('scene_terminal', 0, 0), )) scene.set_texts(( ("Thanks for your enjoying\nMore scenes are coming...\n\nPress J to exit", (500, 130), 'center', 30, 'white'), ("I wanna be the dinosaur\nVersion:{}".format(VERSION), (WIDTH / 2, 360), 'center', 40, 'white'), ("Contact us: habibzhu@foxmail.com".format(VERSION), (WIDTH / 2, 440), 'center', 25, 'white'), ("Difficulty:{}".format({'0':'Cheat', '1':'Easy', '2':'Normal', '3':'Hard'}[str(scene.difficulty)]), (200, 490), 'left', 25, 'red'), ("Death:{} times".format(scene.sum_death), (540, 490), 'left', 25, 'red') )) scene.set_bricks( [(x + 2, 9) for x in range(23)] + [(x + 2, 18) for x in range(23)] + [(2, x + 10) for x in range(8)] + [(24, x + 10) for x in range(8)] ) scene.set_thorns( [(x+2, 8, 'up') for x in range(23)] + [(x+2, 19, 'down') for x in range(23)] + [(1, x + 9, 'left') for x in range(10)] + [(25, x + 9, 'right') for x in range(10)] ) scene.set_savers(( (13, 17, 1, 4), )) scene.set_kid() scene.append_objects() """--------------------------------------SCENES----------------------------------------""" scenes.append(Scene(load_scene_init)) scenes.append(Scene(load_scene_start)) scenes.append(Scene(load_scene1)) scenes.append(Scene(load_scene2)) scenes.append(Scene(load_scene3)) scenes.append(Scene(load_scene4)) scenes.append(Scene(load_scene5)) scenes.append(Scene(load_scene_terminal))
from math import * """ 进行三角形与圆形碰撞检测数学算法 方块坐标转换像素坐标匿名函数 """ get_x = lambda x0:30 * x0 - 15 get_y = lambda y0:30 * y0 - 15 def evaluate_point_to_line(x, y, x1, y1, x2, y2): """计算点到线距离""" a = y2 - y1 b = x1 - x2 c = x2 * y1 - x1 * y2 return a * x + b * y + c def is_point_in_triangle(x, y, x1, y1, x2, y2, x3, y3): """判断点是否在三角形内""" d1 = evaluate_point_to_line(x, y, x1, y1, x2, y2) d2 = evaluate_point_to_line(x, y, x2, y2, x3, y3) if d1 * d2 < 0: return False d3 = evaluate_point_to_line(x, y, x3, y3, x1, y1) if d2 * d3 < 0: return False return True def is_circle_intersect_line_seg(x, y, r, x1, y1, x2, y2): """判断圆是否与直线相交""" vx1 = x - x1 vy1 = y - y1 vx2 = x2 - x1 vy2 = y2 - y1 len = sqrt(vx2 ** 2 + vy2 ** 2) vx2 /= len vy2 /= len u = vx1 * vx2 + vy1 * vy2 if u <= 0: x0 = x1 y0 = y1 elif u >= len: x0 = x2 y0 = y2 else: x0 = x1 + vx2 * u y0 = y1 + vx2 * u if (x - x0) ** 2 + (y - y0) ** 2 <= r ** 2: return True def is_circle_intersect_triangle(x, y, r, x1, y1, x2, y2, x3, y3): """判断圆是否与三角形相交""" if is_point_in_triangle(x, y, x1, y1, x2, y2, x3, y3): return True if is_circle_intersect_line_seg(x, y, r, x1, y1, x2, y2): return True if is_circle_intersect_line_seg(x, y, r, x2, y2, x3, y3): return True if is_circle_intersect_line_seg(x, y, r, x3, y3, x1, y1): return True return False
import pgzrun from pgzero.clock import clock from pgzero.keyboard import keyboard from scenes import * def draw(): if scene.decorations and not scene.over: for dec in scene.decorations: screen.blit(dec.decoration, (dec.x, dec.y)) for i in range(len(scene.objects)): scene.objects[i].act.draw() if scene.kid and scene.kid.visible: scene.kid.act.draw() if scene.blackcurtain: screen.blit('blackcurtain', (0,0)) if scene.texts: for text in scene.texts: screen.draw.text(text.text,center=text.center, align=text.align, fontname=text.fontname, fontsize=text.fontsize, color=text.color) def update(): global scene, i if not music.is_playing('bgm'): music.play('bgm') music.set_volume(0.7) scene.update() if scene.waiting_j: if keyboard.j: scene.over = True if scene.over: if i == len(scenes) - 1: exit() else: i += 1 scene = scenes[i] scene.sum_death = scenes[i - 1].death + scenes[i - 1].sum_death scene.difficulty = scenes[i - 1].difficulty scene.load() scene.set_difficulty(scene.difficulty) if scene.need_press_j: clock.schedule_unique(scene.set_j,1) i = 0 scene = scenes[i] scene.load() pgzrun.go()
import random def szys(): sym = ['+', '-', '×', '÷'] f = random.randint(0, 3) n1 = random.randint(1, 20) n2 = random.randint(1, 20) result = 0 if f == 0: result = n1 + n2 elif f == 1: n1, n2 = max(n1, n2), min(n1, n2) result = n1 - n2 elif f == 2: result = n1 * n2 elif f == 3: n1, n2 = max(n1, n2), min(n1, n2) while n1 % n2 != 0: n1 = random.randint(1, 10) n2 = random.randint(1, 10) n1, n2 = max(n1, n2), min(n1, n2) result = int(n1 / n2) print(n1, sym[f], n2, '= ', end='') return result def test(): sym = ['+', '-', '×', '÷'] print('输入所需要的题目数量') n = int(input()) result = [] m = 0 while m <= (n-1): print(m+1, end='、') result .append(szys()) print(' ') m = m+1 m = 0 print('对应的答案:') while m <= (n-1): print(m+1, '、', result[m]) m = m+1 print('选择想要的模式') print('1、进行四则运算') print('2、制作题库') n = int(input()) if n == 1: while True: result = szys() j = input() s = int(j) if s == result: print('right') else: print('error.,the answer is', result) if n == 2: test()
#!/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)], [(0, -1, -1, 0), (0, 1, 0, -1)], [(0, -1, 0, 1), (0, 1, 1, 0)], [(0, 1, 1, 0), (0, 1, 0, -1)], [(0, 0, -1, 1), (0, 1, 0, 0)], [(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('<KeyPress-a>', self.move_left) self.huabu.bind_all('<KeyPress-d>', self.move_right) self.huabu.bind_all('<KeyPress-j>', self.rotate) # self.huabu.bind_all('<KeyPress-k>', self.change) self.huabu.bind_all('<KeyPress-s>', self.quick_drop) self.huabu.bind_all('<Left>', self.move_left) self.huabu.bind_all('<Right>', self.move_right) self.huabu.bind_all('<Up>', self.rotate) self.huabu.bind_all('<Down>', self.quick_drop) self.huabu.bind_all('<KeyPress-space>', 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) # [random.randrange(0, 2) for i in range(16)]) self.base_map.append([0] + [1] * 15) 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()
代码行数(新增/积累) | 博客量(新增/积累) | 学习时间(新增/积累) | 重要成长 | |
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 100/150 | 2/2 | 20/20 | |
第二周 | 250/300 | 2/4 | 30/50 | 对于纯理论性知识的记忆还有待加强 |
第三周 | 100/400 | 1/5 |
40/90 |
对于简单的编码和设计程序判断已基本掌握,但对于在atom中在编写python时的图片插入仍存在问题 |
第四周 | 200/600 | 1/6 | 20/110 | 换用了VScode编辑器,好像是要方便一点,之后学会了BASE64编码,浮点数,还有阿拉伯数字和罗马数字的互相转化。 |
第五周 | 250/850 | 3/9 | 20/130 |
终于不是一脸懵的抄代码了 |
第六周 | 200/1050 | 3/12 | 20/150 | 知道了原来代码并没有什么唯一解,只有更方便更简洁的答案 |
第七周 | 300/1350 | 4/16 | 25/175 | 对伪代码的理解更深入了,这将便于我更准确的书写代码,以及对算法的深入了解将会方便我选择更合适的算法 |
第八周 | 250/1600 | 2/18 | 20/195 | 在项目python数据库编程中的学习中,学会了怎么方便的创建和修改表格,顺便复习了一下字典 |
第九周 | 300/1900 | 4/22 | 25/220 | 完善了项目,自己做了几个游戏玩玩,越来越喜欢计算机了 |