用python3.7做了一个小游戏——见缝插针
基于pgzrun的库
PySimleGUI的库可有可无,加上可使程序更加丰富一点
import pgzrun as pz
import PySimpleGUI as sg
sun = Actor('太阳.png')
sun.x = 400
sun.y = 300
#TITLE 程序标题
TITLE = '见缝插针'
WIDTH = 800
HEIGHT = 600
#创建针的列表
zhens = []
#导入针的图片,设置锚点相对坐标
startZhen = Actor('needle.jpg',anchor = (170+50,1.5))
startZhen.x = 200
startZhen.y = 300
#将新的针放入列表中
layout = [[sg.Text('选择难度'),sg.Radio("A.简单","RADIO"),sg.Radio("B.中等","RADIO"),sg.Radio("C.较难","RADIO"),sg.Radio("D.极难","RADIO"),sg.Radio("E.变态","RADIO")],
[sg.Button("提交")]
]
window = sg.Window('shi',layout, finalize=True)
my_windows_size = window.Size
button, values = window.Read()
window.close()
if values[0] ==True :
rotateSpeed = 1
if values[1] ==True :
rotateSpeed = 2
if values[2] ==True :
rotateSpeed = 3
if values[3] ==True :
rotateSpeed = 4
if values[4] ==True :
rotateSpeed = 6
#设置分数为0
fenshu = 0
def draw():
screen.fill('white')
sun.draw()
#历变zhen里面所有的zhen
startZhen.draw()
for zhen in zhens:
zhen.draw()
screen.draw.text(str(fenshu),(50,250),fontsize = 50,color = 'red')
def update():
#(当针的坐标达到400,针开始做饶圆心圆周运动)
for zhen in zhens: #对列表中每根针历遍处理
zhen.angle = zhen.angle + rotateSpeed
#当任意键被按下的方法
def on_key_down():
global rotateSpeed,fenshu
newZhen = Actor('needle.jpg',anchor = (170+50,1.5))
newZhen.x = 400
newZhen.y = 300
for zhen in zhens:
if newZhen.colliderect(zhen):
#collicede:碰撞,如果新针撞到列表里的针
rotateSpeed = 0
if fenshu <= 35 and fenshu >= 0:
sg.Popup("c")
if fenshu <= 55 and fenshu >= 36 :
sg.Popup("B")
if fenshu <= 80 and fenshu >= 56:
sg.Popup("A")
#打印 游戏失败
if rotateSpeed != 0:
fenshu = fenshu+1
zhens.append(newZhen)
pz.go()