前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >14天学习训练营导师课程|Pygame学习笔记-俄罗斯方块项目代码解析1

14天学习训练营导师课程|Pygame学习笔记-俄罗斯方块项目代码解析1

作者头像
破晓之翼
发布2022-12-03 09:11:31
2530
发布2022-12-03 09:11:31
举报
文章被收录于专栏:产品能力

学习知识点

在学习完老师的对应的课程后,第一个小目标是做一个俄罗斯方块,PY的对应语法知识后期还要加强学习。就像学习Java等语言思路,开始哪怕自己不会上手写,先参照别人的代码,梳理对应的实现逻辑和设计思路,然后从读懂开始,一步一步的分析,吃透。

在这里插入图片描述
在这里插入图片描述

做题/项目思路

  1. 引入常量
  2. 引入包。
  3. 初始化Pygame。
  4. 设置窗体,显示的尺寸及大小。
  5. 捕获事件,设定每一种事件的定义及内容。
在这里插入图片描述
在这里插入图片描述

分析做这个游戏第一步,进行要素设计的时候的思维导图整理如下:

在这里插入图片描述
在这里插入图片描述

源码分析

代码语言:javascript
复制
#定义变量
from tkinter import *
from time import sleep
from random import *
from tkinter import messagebox
class Teris:
def __init__(self):
代码语言:javascript
复制
`#导入包
import random
import sys
import time
import numpy
import pygame
#方块颜色列表
self.color = ['red','orange','yellow','purple','blue','green','pink']
#Set a core squre and any shape can be drawn by the relative location.
#字典 存储形状对应7种形状 元组存储坐标
self.shapeDict = {1:[(0,0),(0,-1),(0,-2),(0,1)], # shape I
2:[(0,0),(0,-1),(1,-1),(1,0)], # shape O
3:[(0,0),(-1,0),(0,-1),(1,0)], # shape T T型
4:[(0,0),(0,-1),(1,0),(2,0)], # shape J 右长倒L盖子
5:[(0,0),(0,-1),(-1,0),(-2,0)], # shape L
6:[(0,0),(0,-1),(-1,-1),(1,0)], # shape Z
7:[(0,0),(-1,0),(0,-1),(1,-1)]} # shape S
代码语言:javascript
复制
#旋转坐标控制
self.rotateDict = {(0,0):(0,0),(0,1):(-1,0),(0,2):(-2,0),(0,-1):(1,0),
(0,-2):(2,0),(1,0):(0,1),(2,0):(0,2),(-1,0):(0,-1),
(-2,0):(0,-2),(1,1):(-1,1),(-1,1):(-1,-1),
(-1,-1):(1,-1),(1,-1):(1,1)}
代码语言:javascript
复制
# 初始高度,宽度 核心块位置
self.coreLocation = [4,-2]
self.height,self.width = 20,10
self.size = 32
# Map can record the location of every square.i宽 j高
self.map = {}
#全部置0
for i in range(self.width):
for j in range(-4,self.height):
self.map[(i,j)] = 0
#添加边界
for i in range(-4,self.width+4):
self.map[(i,self.height)] = 1
for j in range(-4,self.height+4):
for i in range(-4,0):
self.map[(i,j)] = 1
for j in range(-4,self.height+4):
for i in range(self.width,self.width+4):
self.map[(i,j)] = 1
# 初始化分数0 默认不加快 按下时加快
self.score = 0
self.isFaster = False
# 创建GUI界面
self.root = Tk()
self.root.title("Teris")
self.root.geometry("500x645")
self.area = Canvas(self.root,width=320,height=640,bg='white')
self.area.grid(row=2)
self.pauseBut = Button(self.root,text="Pause",height=2,width=13,font=(18),command=self.isPause)
self.pauseBut.place(x=340,y=100)
self.startBut = Button(self.root,text="Start",height=2,width=13,font=(18),command=self.play)
self.startBut.place(x=340,y=20)
self.restartBut = Button(self.root,text="Restart",height=2,width=13,font=(18),command=self.isRestart)
self.restartBut.place(x=340,y=180)
self.quitBut = Button(self.root,text="Quit",height=2,width=13,font=(18),command=self.isQuit)
self.quitBut.place(x=340,y=260)
self.scoreLabel1 = Label(self.root,text="Score:",font=(24))
self.scoreLabel1.place(x=340,y=600)
self.scoreLabel2 = Label(self.root,text="0",fg='red',font=(24))
self.scoreLabel2.place(x=410,y=600)
#按键交互
self.area.bind("<Up>",self.rotate)
self.area.bind("<Left>",self.moveLeft)
self.area.bind("<Right>",self.moveRight)
self.area.bind("<Down>",self.moveFaster)
self.area.bind("<Key-w>",self.rotate)
self.area.bind("<Key-a>",self.moveLeft)
self.area.bind("<Key-d>",self.moveRight)
self.area.bind("<Key-s>",self.moveFaster)
self.area.focus_set()
#菜单
self.menu = Menu(self.root)
self.root.config(menu=self.menu)
self.startMenu = Menu(self.menu)
self.menu.add_cascade(label='Start',menu=self.startMenu)
self.startMenu.add_command(label='New Game',command=self.isRestart)
self.startMenu.add_separator()
self.startMenu.add_command(label='Continue',command=self.play)
self.exitMenu = Menu(self.menu)
self.menu.add_cascade(label='Exit',command=self.isQuit)
self.helpMenu = Menu(self.root)
self.menu.add_cascade(label='Help',menu=self.helpMenu)
self.helpMenu.add_command(label='How to play',command=self.rule)
self.helpMenu.add_separator()
self.helpMenu.add_command(label='About...',command=self.about)
#先将核心块的所在位置在map中的元素设为1,通过self.shapeDict获取其余方块位置,将map中对应元素设为1。
def getLocation(self): map[(core[0],core[1])] = 1
for i in range(4): map[((core[0]+getNew[i][0]),
(core[1]+getNew[i][1]))]=1
#判断方块下移一格后对应位置map中的元素是否为一,是,则不可移动,返回False;否,可以移动,返回True。
def canMove(self):
for i in range(4):
if map[(core[0]+getNew[i][0]),(core[1]+1+getNew[i][1])] == 1:
return False
return True
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 学习知识点
  • 做题/项目思路
  • 源码分析
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档