饼块游戏的制作
作者:互联网
饼块游戏的介绍
饼块游戏其实是一个我自创的一个小游戏,用户在程序运行后按下‘p’键即可开始游戏,游戏计时根据程序绘制出来的饼块数量的多少来决定。效果图如下:
此游戏将会用到四个不同的库——sys(提供与解释器交互的函数集合库)、math(许多关于数学的函数模块的集合库)、time(提供关于时间的测量的应用的函数集合库)与 pygame(用以写游戏的python模块集合库)
目录
2.Pygame,sys,math,time的导入及初始设置
1.Pygame的下载(已下载可跳过)
Windows徽标键+R,会弹出一个运行框,输入‘cmd’,回车,输入‘pip install pygame’
如果要检查是否已经安装pygame,按下Windows徽标键+R,在运行框内输入‘cmd’,回车,输入‘python’,回车后再输入‘import pygame’,弹出版本号即已安装。
2.Pygame,sys,math,time的导入及初始设置
####查####
import pygame,sys,math,time
pygame.init()#初始化
sn = pygame.display.set_mode((600,500))#定义窗口大小
mt = pygame.font.Font(None,60)#(字体,大小)
pygame.display.set_caption('饼块游戏')
为了以防程序报错,我在导入时使用的是import(使用from···import···在后期循环内会报错,详见后文)。然后使用display.set_mode((,))去自定义窗口的大小,再用font.Font(,)去自定义字体与其大小,以便于后期直接调用这种自定义字体,最后还用到了display.set_caption('')来设置左上角标题。
3.while循环的设置
while 1:
for event in pygame.event.get():#重复遍历用户输入键的值
if event.type in (pygame.QUIT,pygame.KEYDOWN):#检测用户是否点击窗口右上角的关闭或者按下任何的键
sys.exit()#结束程序运行
pygame.display.update()#用来刷新屏幕进行显示
跟着if后面的这一串在后期需要改动(因为此游戏需要用到键盘)exit()是用来结束程序的一个函数,最后再添加上display.update()进行刷新显示,整个循环的进入,运作,退出的基本结构就出来了
ps:如果在导入时用from···import···报错结果如下:
我们能看到,程序可以进入循环阶段,但在运行时会报错(这时候就连偷懒都不容易了)。将程序改回:
我们可以看到程序正常运行了。(ps:亲身试错过)
4.绘制圆
pygame.draw.circle(sn,'black',(300,300),100,10)
这里分别设置了圆的颜色,位置(注:pygame的(0,0)在左上角),半径及其宽度,sn在这段程序外就设置了,这样是为了使其在打开的窗口内显示。
5.绘制线段
pygame.draw.line(sn,'black',(100,100),(400,450),8)
sn的作用跟圆的绘制时的作用一样,紧接着就是颜色,初始位置,结束位置,线条宽度。
好啦,我们知道了这些,接下来就是对键盘的一顿敲击了
6.合并
首先让用户选择难度
print('请选择困难程度')
print('a为困难,b为中等,c为简单(点击键盘对应键键即可)')
初始化定义
import pygame,sys,math,time
pygame.init()
sn = pygame.display.set_mode((600,500))
mf = pygame.font.Font(None,60)
pygame.display.set_caption('饼块游戏')
a = b = c = d = e = f = g = h = a1 = b1 = c1 = d1 = e1 = f1 = a2 = b2 = c2 = d2 = 0
z = 1
我们创建一个while和for循环及其退出语句(按下e键退出在后文)
print('请选择困难程度')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
while 1:
for event in pygame.event.get():#一定要加上此循环语句,否则下一句将会报错(先在for循环内调用)
if event.type == pygame.QUIT:
sys.exit()
接着便是判断用户选择的难度了,额外还要附加一个判断用户退出的一个判断
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
#(语句执行详见下文)
elif event.key == pygame.K_b:
#(语句执行详见下文)
elif event.key == pygame.K_c:
#(语句执行详见下文)
elif event.key == pygame.K_e:
print('您已选择退出游戏')
time.sleep(0.7)
sys.exit()
我们先开干困难关卡的程序,我们的a~h八个字符为0就是困难关卡中的八个按键,首先我们要先绘制出一个圆,再用线段去把圆分成8份,一份一个字母(或数字),用户按照任意顺序按下其中的键,被按到的那个字母(或数字)便会消失(就是以背景色为颜色在同样位置再写一遍既可达到效果)(a中的程序)
#困难模式的绘图
sn.fill('blue')
#绘制圆
pygame.draw.circle(sn,'black',(300,250),200,10)
#绘制线段
pygame.draw.line(sn,'black',(165,110),(420,400),14)#线段基本就复制粘贴既可
pygame.draw.line(sn,'black',(300,50),(300,450),10)
pygame.draw.line(sn,'black',(435,110),(180,400),14)
pygame.draw.line(sn,'black',(102,265),(498,265),10)
#添加运行文字
tm = mf.render('h',True,'red')#也是复制粘贴一波
sn.blit(tm,(170,200))
tm = mf.render('n',True,'red')
sn.blit(tm,(250,125))
tm = mf.render('8',True,'red')
sn.blit(tm,(170,300))
tm = mf.render('1',True,'red')
sn.blit(tm,(250,365))
tm = mf.render('l',True,'red')
sn.blit(tm,(350,125))
tm = mf.render('t',True,'red')
sn.blit(tm,(350,350))
tm = mf.render('z',True,'red')
sn.blit(tm,(430,200))
tm = mf.render('4',True,'red')
sn.blit(tm,(430,300))
pygame.display.update()
z = 2
紧接着判断用户按下p键开始游戏(当a执行后z=2既可执行以下语句,均与if event.key == pygame.K_a:同级)
if z == 2:
if event.key == pygame.K_p:
z = 2.5
print('按下p键开始')
使得z为3后再执行下列循环(可能有人会问我,判断这么麻烦,咋不直接套娃一样一个一个往里套呢。啧,这是不行滴(亲身错误示范得知),会导致程序无法继续进行下一步)。在这就不得不说一个比较重要的函数——perf_counter(),这个函数开始时只要用time去调用它,结束时再调用一次就可得知“开始时间”和“结束时间”了(开始时非0,所以要相减)。最后一行必须将此上数字归零,否则就在下一次执行时由于满足直接进入此处(if a == b == c == d == e == f == g == h == 1:处)执行操作
if z == 2.5:
st = time.perf_counter()
z = 3
if z == 3:
if event.key == pygame.K_h:
a = 1
tm = mf.render('h',True,'blue')
sn.blit(tm,(170,200))
if event.key == pygame.K_n:
b = 1
tm = mf.render('n',True,'blue')
sn.blit(tm,(250,125))
if event.key == pygame.K_8:
c = 1
tm = mf.render('8',True,'blue')
sn.blit(tm,(170,300))
if event.key == pygame.K_1:
d = 1
tm = mf.render('1',True,'blue')
sn.blit(tm,(250,365))
if event.key == pygame.K_l:
e = 1
tm = mf.render('l',True,'blue')
sn.blit(tm,(350,125))
if event.key == pygame.K_t:
f = 1
tm = mf.render('t',True,'blue')
sn.blit(tm,(350,350))
if event.key == pygame.K_z:
g = 1
tm = mf.render('z',True,'blue')
sn.blit(tm,(430,200))
if event.key == pygame.K_4:
h = 1
tm = mf.render('4',True,'blue')
sn.blit(tm,(430,300))
pygame.display.update()
if a == b == c == d == e == f == g == h == 1:
ed = time.perf_counter()
if ed - st > 5:
print('挑战失败')
sys.exit()
else:
print('挑战成功')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
a = b = c = d = e = f = g = h = 0
中等难度的程序其实就简单些,大部分就是复制,还有一些修改就行了,和a部分差不多,就是少了一些字母
sn.fill('blue')
pygame.draw.circle(sn,'black',(300,250),200,10)
pygame.draw.line(sn,'black',(102,265),(498,265),10)
pygame.draw.line(sn,'black',(165,110),(420,400),14)
pygame.draw.line(sn,'black',(435,110),(180,400),14)
tm = mf.render('q',True,'red')
sn.blit(tm,(170,200))
tm = mf.render('5',True,'red')
sn.blit(tm,(290,125))
tm = mf.render('9',True,'red')
sn.blit(tm,(170,300))
tm = mf.render('m',True,'red')
sn.blit(tm,(290,365))
tm = mf.render('g',True,'red')
sn.blit(tm,(430,200))
tm = mf.render('i',True,'red')
sn.blit(tm,(430,300))
pygame.display.update()
z = 4
以及其执行语句
if z == 4:
if event.key == pygame.K_p:
z = 4.5
print('按下p键开始')
if z == 4.5:
st1 = time.perf_counter()
z = 5
if z == 5:
if event.key == pygame.K_q:
a1 = 1
tm = mf.render('q',True,'blue')
sn.blit(tm,(170,200))
if event.key == pygame.K_5:
b1 = 1
tm = mf.render('5',True,'blue')
sn.blit(tm,(290,125))
if event.key == pygame.K_9:
c1 = 1
tm = mf.render('9',True,'blue')
sn.blit(tm,(170,300))
if event.key == pygame.K_m:
d1 = 1
tm = mf.render('m',True,'blue')
sn.blit(tm,(290,365))
if event.key == pygame.K_g:
e1 = 1
tm = mf.render('g',True,'blue')
sn.blit(tm,(430,200))
if event.key == pygame.K_i:
f1 = 1
tm = mf.render('i',True,'blue')
sn.blit(tm,(430,300))
pygame.display.update()
if a1 == b1 == c1 == d1 == e1 == f1 == 1:
ed1 = time.perf_counter()
if ed1 - st1 > 4.5:
print('挑战失败')
sys.exit()
else:
print('挑战成功')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
a1 = b1 = c1 = d1 = e1 = f1 = 0
最后简单的那部分也没啥难的了,直接上代码吧!
sn.fill('blue')
pygame.draw.circle(sn,'black',(300,250),200,10)
pygame.draw.line(sn,'black',(300,50),(300,450),10)
pygame.draw.line(sn,'black',(102,265),(498,265),10)
tm = mf.render('d',True,'red')
sn.blit(tm,(200,150))
tm = mf.render('h',True,'red')
sn.blit(tm,(390,150))
tm = mf.render('0',True,'red')
sn.blit(tm,(200,350))
tm = mf.render('3',True,'red')
sn.blit(tm,(390,350))
pygame.display.update()
z = 6
if z == 6:
if event.key == pygame.K_p:
z = 6.5
print('按下p键开始')
if z == 6.5:
st2 = time.perf_counter()
z = 7
if z == 7:
if event.key == pygame.K_d:
a2 = 1
tm = mf.render('d',True,'blue')
sn.blit(tm,(200,150))
if event.key == pygame.K_h:
b2 = 1
tm = mf.render('h',True,'blue')
sn.blit(tm,(390,150))
if event.key == pygame.K_0:
c2 = 1
tm = mf.render('0',True,'blue')
sn.blit(tm,(200,350))
if event.key == pygame.K_3:
d2 = 1
tm = mf.render('3',True,'blue')
sn.blit(tm,(390,350))
pygame.display.update()
if a2 == b2 == c2 == d2 == 1:
ed2 = time.perf_counter()
if ed2 - st2 > 2.5:
print('挑战失败')
sys.exit()
else:
print('挑战成功')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
a2 = b2 = c2 = d2 = 0
7.源代码
最后在此献上源代码:
import pygame,sys,math,time
pygame.init()
sn = pygame.display.set_mode((600,500))
mf = pygame.font.Font(None,50)
pygame.display.set_caption('饼块游戏')
a = b = c = d = e = f = g = h = a1 = b1 = c1 = d1 = e1 = f1 = a2 = b2 = c2 = d2 = 0
print('请选择困难程度')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
print('按下e退出')
z = 1
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
sn.fill('blue')
pygame.draw.circle(sn,'black',(300,250),200,10)
pygame.draw.line(sn,'black',(165,110),(420,400),14)
pygame.draw.line(sn,'black',(300,50),(300,450),10)
pygame.draw.line(sn,'black',(435,110),(180,400),14)
pygame.draw.line(sn,'black',(102,265),(498,265),10)
tm = mf.render('h',True,'red')
sn.blit(tm,(170,200))
tm = mf.render('n',True,'red')
sn.blit(tm,(250,125))
tm = mf.render('8',True,'red')
sn.blit(tm,(170,300))
tm = mf.render('1',True,'red')
sn.blit(tm,(250,365))
tm = mf.render('l',True,'red')
sn.blit(tm,(350,125))
tm = mf.render('t',True,'red')
sn.blit(tm,(350,350))
tm = mf.render('z',True,'red')
sn.blit(tm,(430,200))
tm = mf.render('4',True,'red')
sn.blit(tm,(430,300))
pygame.display.update()
z = 2
elif event.key == pygame.K_b:
sn.fill('blue')
pygame.draw.circle(sn,'black',(300,250),200,10)
pygame.draw.line(sn,'black',(102,265),(498,265),10)
pygame.draw.line(sn,'black',(165,110),(420,400),14)
pygame.draw.line(sn,'black',(435,110),(180,400),14)
tm = mf.render('q',True,'red')
sn.blit(tm,(170,200))
tm = mf.render('5',True,'red')
sn.blit(tm,(290,125))
tm = mf.render('9',True,'red')
sn.blit(tm,(170,300))
tm = mf.render('m',True,'red')
sn.blit(tm,(290,365))
tm = mf.render('g',True,'red')
sn.blit(tm,(430,200))
tm = mf.render('i',True,'red')
sn.blit(tm,(430,300))
pygame.display.update()
z = 4
elif event.key == pygame.K_c:
sn.fill('blue')
pygame.draw.circle(sn,'black',(300,250),200,10)
pygame.draw.line(sn,'black',(300,50),(300,450),10)
pygame.draw.line(sn,'black',(102,265),(498,265),10)
tm = mf.render('d',True,'red')
sn.blit(tm,(200,150))
tm = mf.render('h',True,'red')
sn.blit(tm,(390,150))
tm = mf.render('0',True,'red')
sn.blit(tm,(200,350))
tm = mf.render('3',True,'red')
sn.blit(tm,(390,350))
pygame.display.update()
z = 6
elif event.key == pygame.K_e:
print('您已选择退出游戏')
time.sleep(0.7)
sys.exit()
if z == 2:
if event.key == pygame.K_p:
z = 2.5
print('按下p键开始')
if z == 2.5:
st = time.perf_counter()
z = 3
if z == 3:
if event.key == pygame.K_h:
a = 1
tm = mf.render('h',True,'blue')
sn.blit(tm,(170,200))
if event.key == pygame.K_n:
b = 1
tm = mf.render('n',True,'blue')
sn.blit(tm,(250,125))
if event.key == pygame.K_8:
c = 1
tm = mf.render('8',True,'blue')
sn.blit(tm,(170,300))
if event.key == pygame.K_1:
d = 1
tm = mf.render('1',True,'blue')
sn.blit(tm,(250,365))
if event.key == pygame.K_l:
e = 1
tm = mf.render('l',True,'blue')
sn.blit(tm,(350,125))
if event.key == pygame.K_t:
f = 1
tm = mf.render('t',True,'blue')
sn.blit(tm,(350,350))
if event.key == pygame.K_z:
g = 1
tm = mf.render('z',True,'blue')
sn.blit(tm,(430,200))
if event.key == pygame.K_4:
h = 1
tm = mf.render('4',True,'blue')
sn.blit(tm,(430,300))
pygame.display.update()
if a == b == c == d == e == f == g == h == 1:
ed = time.perf_counter()
if ed - st > 6.5:
print('挑战失败')
sys.exit()
else:
print('挑战成功')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
a = b = c = d = e = f = g = h = 0
if z == 4:
if event.key == pygame.K_p:
z = 4.5
print('按下p键开始')
if z == 4.5:
st1 = time.perf_counter()
z = 5
if z == 5:
if event.key == pygame.K_q:
a1 = 1
tm = mf.render('q',True,'blue')
sn.blit(tm,(170,200))
if event.key == pygame.K_5:
b1 = 1
tm = mf.render('5',True,'blue')
sn.blit(tm,(290,125))
if event.key == pygame.K_9:
c1 = 1
tm = mf.render('9',True,'blue')
sn.blit(tm,(170,300))
if event.key == pygame.K_m:
d1 = 1
tm = mf.render('m',True,'blue')
sn.blit(tm,(290,365))
if event.key == pygame.K_g:
e1 = 1
tm = mf.render('g',True,'blue')
sn.blit(tm,(430,200))
if event.key == pygame.K_i:
f1 = 1
tm = mf.render('i',True,'blue')
sn.blit(tm,(430,300))
pygame.display.update()
if a1 == b1 == c1 == d1 == e1 == f1 == 1:
ed1 = time.perf_counter()
if ed1 - st1 > 4.5:
print('挑战失败')
sys.exit()
else:
print('挑战成功')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
a1 = b1 = c1 = d1 = e1 = f1 = 0
if z == 6:
if event.key == pygame.K_p:
z = 6.5
print('按下p键开始')
if z == 6.5:
st2 = time.perf_counter()
z = 7
if z == 7:
if event.key == pygame.K_d:
a2 = 1
tm = mf.render('d',True,'blue')
sn.blit(tm,(200,150))
if event.key == pygame.K_h:
b2 = 1
tm = mf.render('h',True,'blue')
sn.blit(tm,(390,150))
if event.key == pygame.K_0:
c2 = 1
tm = mf.render('0',True,'blue')
sn.blit(tm,(200,350))
if event.key == pygame.K_3:
d2 = 1
tm = mf.render('3',True,'blue')
sn.blit(tm,(390,350))
pygame.display.update()
if a2 == b2 == c2 == d2 == 1:
ed2 = time.perf_counter()
if ed2 - st2 > 2.5:
print('挑战失败')
sys.exit()
else:
print('挑战成功')
print('a为困难,b为中等,c为简单(单击键盘此键即可)')
a2 = b2 = c2 = d2 = 0
ps:有错请各位指出,还请各位多多包涵噢
标签:饼块,mf,游戏,render,True,tm,sn,制作,pygame 来源: https://blog.csdn.net/LEVEL_SSR/article/details/122772875