"""這是一個多人小遊戲,通過按方向箭頭操作小老鼠闖過街道,作者:李興球,2018/10/1日."""
from turtle import *
from random import randint,choice
class Bus(Turtle):
"""小汽車類,參數說明:
images:圖形列表,每輛小汽車實例的造型是隨機選擇的一張圖片
ycor:初始y座標,它的x座標是隨機的一個值
mouses:老鼠列表,每輛小汽車會檢測有沒有碰到每隻老鼠
"""
def __init__(self,images,ycor,mouses):
Turtle.__init__(self,visible=False)
self.up() # 擡筆
self.images = images # 造型列表
self.shape(choice(images)) # 隨機選擇一個,做爲初始造型
self.sety(ycor) # 初始y座標
self.setx(-self.screen.window_width()//2 - randint(100,300))
self.mouses= mouses
self.st() # 顯示
self.move() # 不斷地移動
def move(self):
"""本方法讓小汽車不斷地從左到右移動"""
self.setx(self.xcor() + 5)
x = self.xcor()
y = self.ycor()
self.left = x - 35 # 小汽車的寬度和高度約爲70 60
self.right = x + 35
self.top = y + 30
self.bottom = y - 30
if self.left > self.screen.window_width()//2:
self.ht() # 隱藏
self.shape(choice(self.images)) # 換個造型,好像另一輛車一樣
x = -self.screen.window_width()//2 - randint(100,300)
self.setx(x)
self.st() # 顯示
self.pressmouse() # 是否壓到了老鼠
self.screen.ontimer(self.move,10)
def pressmouse(self):
"""壓到老鼠了判斷,判斷汽車和老鼠的兩個矩形是否重疊,使用的是逆向思維法"""
pass
class Mouse(Turtle):
"""老鼠類,參數說明:
images: 造型列表
keys:按鍵列表,上鍵和下鍵
laugh:笑聲
bell:鐘聲
"""
def __init__(self,images,keys,laugh=None,bell=None):
Turtle.__init__(self,shape='blank',visible=False)
self.up() # 擡筆
self.dead = False # 描述老鼠是否死亡的邏輯變量
self.index= 0 # 造型索引號
self.images = images # 造型列表
self.upkey = keys[0] # 向上按鍵
self.downkey = keys[1] # 向下按鍵
self.laugh = laugh # 大笑聲
self.bell =bell # 過關後的聲
self.alt_image() # 換造型
self.register_keys() # 註冊按鍵
self.st() # 顯示出來
self.wait_success() # 等待過關
def register_keys(self):
"""註冊按鍵"""
pass
def cancle_keys(self):
"""取消註冊的按鍵"""
pass
def alt_image(self):
""切換造型"""
pass
def wait_success(self):
"""每隔0.1秒判斷是否成功穿越"""
if self.ycor() > self.screen.window_height()//2-80 and self.isvisible():
try:self.bell.play() #播放鐘聲,表示成功過街
except:pass
self.stamp() #蓋圖章
self.cancle_keys() #取消按鍵綁定
self.ht() #隱藏
else:
self.screen.ontimer(self.wait_success,100)
def init_screen(width,height,title,bgimage):
"""初始化屏幕"""
screen = Screen()
screen.setup(width,height )
screen.title(title)
screen.bgpic(bgimage)
screen.delay(0)
return screen
def register_gif():
"""註冊汽車與老鼠gif圖案到形狀列表"""
busimages = ["小汽車" + str(i) + ".gif" for i in range(4)]
[screen.addshape(image) for image in busimages] #註冊所有小汽車gif圖到形狀列表
mouseimages = ["mouse0.gif","mouse1.gif","mouse2.gif"]
[screen.addshape(image) for image in mouseimages] #註冊所有老鼠gif圖到形狀列表
return busimages,mouseimages
def init_audio():
"""播放背景音樂,新建音頻實例對象"""
pygame_normal = False #描述pygame是否正常的邏輯變量
try:
import pygame
pygame.mixer.init()
pygame_normal = True
except:
pass
if pygame_normal:
pygame.mixer.music.load("歡快女唱電音歌曲超嗨.wav")
pygame.mixer.music.play(-1,0)
haha = pygame.mixer.Sound("Laugh-male1.wav") #哈哈聲
bell = pygame.mixer.Sound("BellToll.wav") #鐘聲
cricket = pygame.mixer.Sound("Cricket.wav") #吱聲
else:
haha = None ; bell=None ; cricket=None
return haha,bell,cricket
def make_mouses():
"""實例化老鼠"""
mouse_a = Mouse(mouseimages,("w","s"),haha,bell) #實例化左邊老鼠,用w,s鍵移動老鼠
mouse_a.setx(-50)
mouse_a.sety(-height//2 + 50)
mouse_b = Mouse(mouseimages,("Up","Down"),haha,bell) #實例化右邊老鼠,用上,下方向箭頭移動老鼠
mouse_b.setx(+50)
mouse_b.sety(-height//2 + 50)
ms = [mouse_a,mouse_b]
return ms
if __name__=="__main__":
width,height = 800,600 #屏幕寬高定義
screen = init_screen(width,height,"老鼠過街_作者:李興球","街道800x600.gif")
busimages,mouseimages = register_gif() #註冊汽車與老鼠gif圖形
haha,bell,cricket = init_audio() #初始化音頻
ms = make_mouses() #生成兩隻老鼠
[ Bus(busimages,ycor,ms) for ycor in (170,70,-80,-170)] #生成4輛在不同y座標的小車
cricket.play() #播放下老鼠的吱吱聲
screen.listen() #監聽按鍵
screen.mainloop() #主循環