pyGame是什么:https://www.pygame.org/docs/
简单说就是用python写小游戏玩一玩,为什么要用python写小游戏玩一玩,因为平时搞研究实在太难玩了啊啊啊啊啊啊啊啊啊啊啊因为我们要拥有一颗热爱学习的积极的内心!!!!
好的,安装就是:pip install pygame
然后我们先来构思一下想要实现的第一个小游戏,不知道写什么好那就首先赶上时代杀死新冠病毒吧,可以解压不是么😁
我们先找一个小目标:来源:http://k.sina.com.cn/article_6635309284_18b7ec8e400100ljhj.html
我们找了个这样的小东西,然后放出一个成品
放上github地址:https://github.com/timcanby/learn_pygame_note/blob/master/pygame1.py
(下载的时候连着virus.jpg 和字体文件一起下)给star是好人
在这我只讲几个重点的思路就是
1:病毒移动的范围需要注意
我使用了两个随机数来算步长:(中间这个20调的越大病毒窜越快)
roads = np.random.randint(0, 20, 1)
roads2 = np.random.randint(0, 20, 1)
然后
if x > w:
x = 0
if y > h:
y = 0
来限制这个病毒不要移动出窗口。
2:鼠标点击事件的编写:
x_mouse, y_mouse = event.pos
if np.abs(x-x_mouse) <=50 and np.abs(y-y_mouse)<=50:
flag=1
x,y 是病毒的位置 x_mouse,y_mouse是鼠标点击的位置。
实现的方法其实很多,这里只是最简单的根据原图的大小随便写了一个两点距离的绝对值不超过的范围
3:这个判断有没有击中的flag很重要,当击中时,界面再次刷新就出现you win的字样
-----------------------------------
第一期就这样,是不是很简单呀~~~下次我们来尝试使得病毒去扩散~
转载标链接~~谢谢