大家好, 我是秃头ca,(团队开发的游戏都是小学生QQ724785408)大家叫我小鞋森,today我们一起来做潜水艇撞破泡泡的game,让我们一起开始吧
一、创建窗口
我们先用Tk()创建窗口
from tkinter import * HEIGHT = 500 WIDTH = 800 window = Tk() window.title('潜水艇game') c = Canvas(window,width=WIDTH,height=HEIGHT,bg='darkblue') c.pack()
二、创造潜艇
我们现在要draw a潜水艇
ship_id = c.create_polygon(5,5,5,25,30,15fill='red') ship_id2 = c.create_oval(0,0,30,30,outline='red') SHIP_R = 15 MID_X = WIDTH / 2 MID_Y = HEIGHT / 2 c.move(ship_id,MID_X,MID_Y) c.move(ship_id2,MID_X,MID_Y)
三、让潜艇动起来
SHIP_SPD = 10 def move_ship(event): if event.keysym == 'Up': c.move(ship_id,0,-SHIP_SPD) c.move(ship_id2,0,-SHIP_SPD) elif event.keysym == 'Down': c.move(ship_id,0,-SHIP_SPD) c.move(ship_id2,0,SHIP_SPD) elif event.keysym == 'Left': c.move(ship_id,-SHIP_SPD,0) c.move(ship_id2,-SHIP_SPD,0) elif event.keysym =='Right': c.move(ship_id,SHIP_SPD,0) c.move(ship_id2,SHIP_SPD,0)
(不好意思??忘记了一行代码 c.bind_all('<Key>',move_ship) 对齐def)
准备好泡泡
好了,接下来我们要泡泡了。
from random import randint bub_id = list() bub_r = list() bub_speed = list() MIN_BUB_R = 10 MAX_BUB_R = 30 MAX_BUB_SPD = 10 GAP = 100 def create_bubble(): x = WIDTH GAP y = randint(0,HEIGHT) r = randint(MIN_BUB_R,MAX_BUB_R) id1 = c.create_oval(x - r,y - r,x r,y r,outline='white') bub_id.append(id1) bub_r.append(r) bub_speed.append(randint(1,MAX_BUB_SPD))
五、让泡泡起来
泡泡不是死物。我们应该让它移动
def move_bubbles(): for i in range(len(bub_id)): c.move(bub_id[i],-bub_speed[i],0)
六、创建定位泡泡的函数
def get_coords(id_num): pos = c.coords(id_num) x = (pos[0] pos[2])/2 y = (pos[1] pos[3])/2 return x,y
创建一个删除气泡的函数
del del_bubble(i): del bub_r[i] del bub_speed[i] c.delete(bub_id[i]) del bub_id[i]
八、清除窗外飘出的泡泡
记得清楚地标记窗户上的泡泡
def clean_up_bubs(): for i in range(len(bub_id)-1,-1,-1): x,y = get_coords(bub_id[i]) if x < -GAP: del_bubble(i)
九计算两点之间的距离
我们现在要计算潜艇和泡泡之间的距离
from math import sqrt def distance(id1,id2): x1,y1 = get_coords(id1) x2,y2 = get_coords(id2) return sqrt((x2 - x1)**2 (y2 - y1)**2)
十、打破泡泡,更新分数
def collision(): points = 0 for bub in range(len(bub_id)-1,-1,-1): if distance(ship_id2,bub_id[bub]) < (SHIP_R bub_r[bub]): points = (bub_r[bub] bub_speed[bub]) del_bubble(bub) return points
显示玩家得分和游戏剩余时间
c.create_text(50,30,text='TIME',fill='white') c.create_text(150,30,text='SCORE',fill='white') time_text = c.create_text(50,50,fill='white') score_text = c.create_text(150,50,fill='white') def show_score(score): c.itemconfig(score_text,text=str(score)) def show_time(time_left): c.itemconfig(time_text,text=str(time_left))
十二、设定时间和获奖时间所需的分数,并计算游戏的剩余时间
这是我们最后的润色
from time import sleep,time BUB_CHANCE = 10 TIME_LIMIT = 30 BONUS_SCORE = 1000 score = 0 bonus = 0 end = time() TIME_LIMIT
接下来,让我们更新游戏的主循环
接下来,我们将写游戏的主循环,并添加分数和时间
while time() < end: if randint(1,BUB_CHANCE) == 1: create_bubble() move_bubbles() clean_up_bubs() score = collision() if(int(score / BONUS_SCORE)) > bonus: bonus = 1 end = TIME_LIMIT show_score(score) show_time(int(end - time())) window.update() sleep(0.01)
添加14的最后一步 GAME OVER
终于到了最后一步
c.create_text(MID_X,MID_Y,\ text='Game over',fill='white',font='('Helvetica',30)) c.create_text(MID_X,MIN_Y 30, \ text='Score: ' str(score),fill='white') c.create_text(MID_X,MID_Y 45, \ text='Bonus time: ' str(bonus*TIME_LIMIT),fill='white')
嗯,今天的潜艇泡泡游戏就到这里。谢谢你的支持。你也可以改进它。例如,如果潜艇图像更逼真,泡泡中会出现炸弹,难度会增加等