小游戏"/>
python老鼠过街小游戏
"""这是一个多人小游戏,通过按方向箭头操作小老鼠闯过街道,作者:李兴球,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):
"""压到老鼠了判断,判断汽车和老鼠的两个矩形是否重叠,使用的是逆向思维法"""
for mouse in self.mouses:
if mouse.dead: continue #老鼠死了,继续(下一个老鼠)
x = mouse.xcor() #老鼠的宽度和高度约为20x40
y = mouse.ycor()
xLeft = x - 10 #老鼠左边的x坐标
xRight = x + 10 #老鼠右边的x坐标
yTop = y + 20 #老鼠上边的y坐标
yBottom = y - 20 #老鼠下边的y坐标
notoverlap = xRight <self.left or xLeft>self.right or yBottom > self.top or yTop<self.bottom
if not notoverlap :
mouse.dead = True
mouse.cancle_keys()
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):
"""注册按键"""
self.screen.onkeypress(lambda :self.sety(self.ycor() + 10),self.upkey) #注册上按键
self.screen.onkeypress(lambda :self.sety(self.ycor() - 10),self.downkey) #注册下按键
def cancle_keys(self):
"""取消注册的按键"""
self.screen.onkeypress(None,self.upkey) #取消注册上按键
self.screen.onkeypress(None,self.downkey) #取消注册下按键
def alt_image(self):
"""在0到1之间切换造型"""
self.shape(self.images[self.index])
self.index = 1 - self.index
if not self.dead :
self.screen.ontimer(self.alt_image,300)
else:
self.shape(self.images[2]) #切换到死亡的造型(带'血'的)
try:self.laugh.play()
except: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() #主循环
"""需要本作品的素材请联系李兴球先生"""
更多推荐
python老鼠过街小游戏
发布评论