欢迎加入我们卧虎藏龙的python讨论qq群:729683466
●导 语 ●
这次带大家做一个新的游戏
小猫抓鱼
其实,很久之前公众号发过一个游戏
接水果
当然,这个游戏颜值更高。
也更好玩
代码及相关资源获取
1:关注“python趣味爱好者”公众号,代码文件的百度网盘链接
链接:https://pan.baidu.com/s/1kDFZIk4Oc0nt6_CTAbOjyw
提取码:1y02
--来自百度网盘超级会员V3的分享
效果演示
ss
往期精选
🛠
开发工具
python3.6.4
第三方库:
pygame,random
讲解部分
一:游戏原理
游戏原理很简单,和接水果的逻辑差不多,以前我们做接水果游戏的时候,是在游戏界面下方设置一个挡板,然后水果不断从上方掉下来。在这个游戏中,小猫顶着一个盆子在界面下方移动,然后鱼从上方不断下落,当然,这个游戏和之前的还是有一点不同,那就是天上除了会掉下来鱼以外,还会掉下来炸弹,小猫不但要学会抓鱼,还要学会躲炸弹。小猫所在的位置是随着鼠标的移动的。
二:游戏逻辑
要用pygame开发游戏,首先就需要对pygame进行初始化,初始化的代码如下:
pygame.init()
然后设置游戏界面的尺寸以及游戏界面的标题
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("嗷大喵爱吃鱼!")
还需要载入一些重要的图片元素,比如说“猫”的图片和“鱼”的图片。
cat=pygame.image.load("aodamiao_2.png")
width,height=cat.get_size()
pic=pygame.transform.scale(cat,(width,height))
fish=pygame.image.load("fish.png")
用pygame.image.load函数就可以读取相应的图片,然后用get_size获取图片的尺寸。在游戏运行的时候,就可以直接将图片粘贴到界面上。
接下来,就是对一些参数进行初始化,比如说分数,鱼的数量,炸弹的数量等等。
在所有的游戏中最重要的是一个while true循环。必须要有一个无限循环,程序才能持续运行。
然后就是需要获取鼠标的位置,这就需要我们用pygame里面的方法获鼠标的位置,用到的代码如下:
elif event.type == MOUSEMOTION:
mouse_x,mouse_y = event.pos
move_x,move_y = event.rel
其中当获取是事件的type是MOUSEMOTTON时,代表鼠标移动了,这时候就需要更新鼠标的位置。
在最开始的时候,游戏就导入了random模块,这是一个产生随机数的第三方库,在游戏中,鱼或者是炸弹是从天上随机掉下来的,这就需要用random.randint产生一些随机整数,用来表示鱼或者炸弹从哪里掉下来。
bomb_x = random.randint(0, 500)
这一句表示的是产生0--500之间的随机数。
还有一个重要的概念就是碰撞检测,无论是猫接到鱼还是猫被炸弹炸了,都需要进行碰撞检测判断。显然,游戏界面里所有的元素都是有一个x,y坐标系的,可以通过x,y坐标系来表示元素的 位置,当然也就可以这样来判断元素之间是否发生了碰撞。用if进行条件判断。
if bomb_x > pos_x-10 and bomb_x < pos_x + 70:
score += 10
bomb_x = random.randint(0, 500)
bomb_y = -50
最终游戏界面上还要显示一些文本,用于提示玩家游戏进行到哪一步了。
为此,我们专门设置了一个函数。
def print_text(font, x, y, text, color=(255,255,255)):
imgText = font.render(text, True, color)
screen.blit(imgText, (x,y))
调用这个函数,就可以在特定的位置打印出特定的文字。
三:重点代码实现
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
#sys.exit()
pygame.quit()
exit()
elif event.type == MOUSEMOTION:
mouse_x,mouse_y = event.pos
move_x,move_y = event.rel
elif event.type == MOUSEBUTTONUP:
if game_over:
game_over = False
lives = 10
score = 0
Round =1
vel_y=0.4
mine=0
flag=0
pic=cat
bomb_y = -50
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0,0,100))
if game_over:
screen.blit(init,(60, 60))
print_text(font3, 200, 400,"Clicked To Play!")
print_text(font2, 310, 480,"Copyright@2015 developed by xiaoxiami")
else:
#Round setting
if score >300 and score <600:
Round=2
elif score >600 and score <900:
Round =3
elif score >900 and score <1200:
Round=4
elif score >1200 and score <1500:
Round =5
elif score >=1500:
Round =6
#draw the Round
print_text(font1, 280, 0, "Round: " + str(Round))
#speed setting
if Round ==1:
vel_y=0.4
elif Round ==2:
vel_y=0.6
elif Round ==3:
vel_y=0.8
elif Round ==4:
vel_y=1.0
elif Round ==5:
vel_y=1.2
#mine number setting
#mine=random.randint(1,9)
#move the fish
bomb_y += vel_y
mine_y+=vel_yy
#has the player missed the fish?
if bomb_y > 500:
bomb_x = random.randint(0, 500)
bomb_y = -50
lives -= 1
if lives == 0:
game_over = True
#see if player has caught the fish
elif bomb_y > pos_y:
if bomb_x > pos_x-10 and bomb_x < pos_x + 70:
score += 10
bomb_x = random.randint(0, 500)
bomb_y = -50
if Round >2:
#has the player missed the mine?
if mine_y > 500:
mine_x = random.randint(0, 500)
mine_y = -50
#see if player has caught the mine
elif mine_y > pos_y:
if mine_x > pos_x and mine_x < pos_x + 40:
mine_x = random.randint(0, 500)
mine_y = -50
lives-=1
pic=cat2
if lives == 0:
game_over = True
#draw the fish
screen.blit(fish,(bomb_x,int(bomb_y)))
#draw the mine
if Round >2:
screen.blit(mine_png,(mine_x,int(mine_y)))
#set cat position
pos_x = mouse_x
if pos_x < 0:
pos_x = 0
elif pos_x > 510:
pos_x = 500
#draw cat
if lives<5:
pic=cat2
screen.blit(pic,(pos_x,pos_y))
#print # of lives
print_text(font1, 0, 0, "LIVES: " + str(lives))
#print score
print_text(font1, 500, 0, "SCORE: " + str(score))
pygame.display.update()
(xxx有话说可不写)
作者|
编辑|
感谢大家观看
有钱的老板可打赏一下小编哦
扫描二维码
关注我们
QQ群:729683466
◰
参考来源
代码来源:GitHub