这个游戏是一个加法出题器,你需要单击数字弹球,让它们加到10的倍数,那么就会得到一颗心。得到了一定数量的心,那么游戏就成功结束了。以下是大部分源代码,相信你一定可以自行完善它。
"""
神算子一休哥.py
这个游戏是一个心算小游戏,需要你单击散开的小球。
累加10分的倍数就会增加一颗心,当到达了22颗心,游戏成功结束!
"""
import time
from turtle import Turtle,Shape,Screen
from winsound import PlaySound,SND_ASYNC,SND_LOOP
from tkinter.messagebox import showinfo
def addcostume(image,screen):
"""增加图形造型到屏幕"""
pass
class Ball(Turtle):
hearts = 0 # 心的计数器
score = 0 # 初始得分
leijia = 0
clicks = 0 # 单击计数
w = Turtle(shape='blank') # 在屏幕上写字的海龟
w.penup() # 抬笔
w.sety(100) # 设置y坐标
_gameover = False # 游戏是否结束
last_score = score # 上次得分
heart = Turtle(shape='blank')
heart.penup()
heart.speed(0)
heart.goto(-200,150)
begin = time.time() # 起始时间
def __init__(self,heading,image,number):
Turtle.__init__(self,shape='blank')
self.penup()
self.speed(0)
self.shape(image) # 造型
self.number = number # 表示的数字
self.setheading(heading) # 初始方向
self.onclick(self.addscore)
self.sw = self.screen.window_width()
self.sh = self.screen.window_height()
self.move()
self.htcounter = 0 # 隐藏秒数计数
def checkht(self):
"""如果是隐藏的,并且没有开始计数,那么1秒后再次运行
由于计数器增加1,所以下次运行就会显示角色
"""
pass
def move(self):
if Ball._gameover :
self.ht()
return
if self.isvisible():self.fd(1)
if self.bounce_edge():self.ht()
self.screen.ontimer(self.move,50)
def bounce_edge(self):
if self.xcor() + 25 > self.sw//2 or self.xcor() -25 < -self.sw//2:
self.setheading(180 - self.heading())
if self.ycor() + 25 > self.sh//2 or self.ycor() -25 < -self.sh//2:
self.setheading( - self.heading())
def addscore(self,x,y):
if Ball._gameover: return
pass
@staticmethod
def gameover():
sj = round(time.time() - Ball.begin)
t = Turtle(shape='blank')
t.penup()
t.sety(25)
info = "你获得了22颗心,游戏成功结束"
t.write(info,align='center',font=('',14,'normal'))
t.sety(-35)
info = "所用时间:" + str(sj) + "秒"
t.write(info,align='center',font=('',20,'normal'))
Ball._gameover = True
def main():
PlaySound('聪明的一休.wav',SND_LOOP|SND_ASYNC)
costumes = [f"res/{i}.png" for i in range(1,21)] # 1,2,3,...20
screen = Screen()
screen.setup(480,360)
screen.delay(0)
screen.bgpic('封面背景.png')
screen.title('神算子一休哥海龟画图版by李兴球')
showinfo('游戏规则','单击数字弹球,累加到10的倍数就得到一颗心!')
screen.addshape('heart.gif')
[addcostume(im,screen) for im in costumes]
for index in range(len(costumes)):
Ball(index*18,costumes[index],index+1)
screen.mainloop()
if __name__== "__main__":
main()
需要完整源代码与素材请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

