python3玩一玩pyGame系列一:写第一个小游戏-杀死新冠病毒

1 篇文章 0 订阅
1 篇文章 0 订阅

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的字样 

-----------------------------------

第一期就这样,是不是很简单呀~~~下次我们来尝试使得病毒去扩散~

转载标链接~~谢谢

好的,我为您编一个小游戏,它是一个简单的打地鼠游戏,适合小学生玩。 首先,需要安装pygame库。可以使用以下命令安装: ``` pip install pygame ``` 安装完成后,可以开始编代码。 ```python import pygame import random # 初始化pygamepygame.init() # 设置游戏窗口大小和标题 WIDTH = 640 HEIGHT = 480 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("打地鼠游戏") # 加载图片和音效 hole_img = pygame.image.load("hole.png") mouse_img = pygame.image.load("mouse.png") whack_sound = pygame.mixer.Sound("whack.wav") miss_sound = pygame.mixer.Sound("miss.wav") # 设置字体 font = pygame.font.Font(None, 36) # 定义游戏变量 score = 0 timer = 30 * 60 # 游戏时间为30秒 mouse_timer = 0 mouse_x = 0 mouse_y = 0 mouse_visible = False # 游戏循环 while timer > 0: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.MOUSEBUTTONDOWN: if mouse_visible: # 如果打中了地鼠,增加分数,并播放音效 if mouse_rect.collidepoint(event.pos): score += 1 whack_sound.play() else: miss_sound.play() # 更新游戏变量 timer -= 1 if mouse_timer == 0: # 随机生成地鼠的位置 mouse_x = random.randint(0, WIDTH - mouse_img.get_width()) mouse_y = random.randint(0, HEIGHT - mouse_img.get_height()) mouse_timer = 30 # 地鼠停留时间为1秒 mouse_visible = True else: mouse_timer -= 1 if mouse_timer == 0: mouse_visible = False # 绘制游戏画面 screen.fill((255, 255, 255)) # 绘制打洞 hole_rect = hole_img.get_rect() hole_rect.centerx = mouse_x + mouse_img.get_width() / 2 hole_rect.bottom = mouse_y + mouse_img.get_height() screen.blit(hole_img, hole_rect) # 绘制地鼠 if mouse_visible: mouse_rect = mouse_img.get_rect() mouse_rect.centerx = mouse_x + mouse_img.get_width() / 2 mouse_rect.bottom = mouse_y screen.blit(mouse_img, mouse_rect) # 绘制分数和时间 score_text = font.render("Score: " + str(score), True, (0, 0, 0)) screen.blit(score_text, (10, 10)) time_text = font.render("Time: " + str(timer // 60), True, (0, 0, 0)) screen.blit(time_text, (WIDTH - 100, 10)) # 更新屏幕 pygame.display.update() # 游戏结束,显示得分 gameover_text = font.render("Game Over! Your Score is " + str(score), True, (255, 0, 0)) screen.blit(gameover_text, (WIDTH / 2 - gameover_text.get_width() / 2, HEIGHT / 2 - gameover_text.get_height() / 2)) pygame.display.update() # 等待用户关闭窗口 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() ``` 在上面的代码中,我们使用pygame库编了一个简单的打地鼠游戏游戏循环中,我们处理鼠标点击事件,更新游戏变量,绘制游戏画面,并在游戏结束时显示得分。 您需要将hole.png、mouse.png、whack.wav和miss.wav这四个文件放在与脚本文件相同的目录下,并确保它们的文件名与代码中的文件名相同。 希望这个小游戏可以帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值