Python自制 笨鸟先飞

2023-11-01 06:30
文章标签 python 自制 笨鸟先飞

本文主要是介绍Python自制 笨鸟先飞,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最初灵感 

        回想起寒假之前也做过一个游戏,但是还没有做好,出现了很多问题,于是想要再做一个游戏来填补自己以前的遗憾,于是着手尝试从头开始了解Pygame,于是我打开了Pygame的官网,并发现了一些有趣的事情,下面代码是官网给出的Pygame的示例代码

# Example file showing a circle moving on screen
import pygame# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)while running:# poll for events# pygame.QUIT event means the user clicked X to close your windowfor event in pygame.event.get():if event.type == pygame.QUIT:running = False# fill the screen with a color to wipe away anything from last framescreen.fill("purple")pygame.draw.circle(screen, "red", player_pos, 40)keys = pygame.key.get_pressed()if keys[pygame.K_w]:player_pos.y -= 300 * dtif keys[pygame.K_s]:player_pos.y += 300 * dtif keys[pygame.K_a]:player_pos.x -= 300 * dtif keys[pygame.K_d]:player_pos.x += 300 * dt# flip() the display to put your work on screenpygame.display.flip()# limits FPS to 60# dt is delta time in seconds since last frame, used for framerate-# independent physics.dt = clock.tick(60) / 1000pygame.quit()

这段代码的效果就是在屏幕中间整个球,然后用W,S,A,D控制这个球移动,效果如下

 研究了一下发现他的逻辑很简单,就是直接上下移动多少,然后画个球更新位置,于是我就知道了应该如何操作了。

代码

        首先先把代码放上来,就是下面这一大片,虽然其实很大一片都是用来在上面写字的。。。

import pygame
import random
import time# pygame setup
pygame.init()
screen = pygame.display.set_mode((1920, 1080))
clock = pygame.time.Clock()
running = True
dt = 0score_history = []#基本参数
screen_width = screen.get_width()
screen_height = screen.get_height()#字体参数
menu_font = pygame.font.Font('STZHONGS.TTF', 48)
game_name_font = pygame.font.Font('SIMYOU.TTF', 96)
special_font = pygame.font.Font('SIMYOU.TTF', 24)#读取图片
bird = pygame.image.load("bird.jpg")#点参数
point_size = 35
score = 0#速度参数
path_y = 20
add_speed = 5 
down_y = 5#时间参数
pause_speed = 0
press_time = 0
not_press = 0
start_time = pygame.time.get_ticks() def random_height():return random.randint(270, 1080 - 10*point_size)#柱子参数
bar_width = 200
bar_position = 1920 - bar_width
pass_locate = random_height()
bar_speed = 7#正式开始游戏
start = False
game_over = False
win = Falseplayer_pos = pygame.Vector2(screen.get_width()/2, screen.get_height() / 2 )pygame.mixer.music.load("START.wav")
pygame.mixer.music.play(-1)while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falsescreen.fill("white")if start == False:mouse_position = pygame.mouse.get_pos()mouse = pygame.draw.circle(screen, "black", mouse_position, 10)game_name = game_name_font.render("笨鸟先飞", True, "red")easy_rect = pygame.Rect(screen.get_width()/2 - 400, screen.get_height() / 2 - 100, 200, 100)e_text = menu_font.render("简单难度", True, "black")hard_rect = pygame.Rect(screen.get_width()/2 - 100, screen.get_height() / 2 - 100, 200, 100)h_text = menu_font.render("困难难度", True, "black")explain_rect = pygame.Rect(screen.get_width()/2 - 100, screen.get_height() / 2 + 100, 200, 100)ex_text = menu_font.render("游戏说明", True, "black")very_hard_rect = pygame.Rect(screen.get_width()/2 + 200, screen.get_height() / 2 - 100, 200, 100)v_text = menu_font.render("地狱难度", True, "black")special_text = special_font.render("Made by: hjl 2023.8.2 In Home", True, "black")pygame.draw.rect(screen, "green", easy_rect,5)pygame.draw.rect(screen, "yellow", hard_rect,5)pygame.draw.rect(screen, "red", very_hard_rect,5)pygame.draw.rect(screen, "blue", explain_rect, 5)screen.blit(e_text, (screen.get_width()/2 - 395, screen.get_height() / 2 - 90))screen.blit(h_text, (screen.get_width()/2 - 95, screen.get_height() / 2 - 90))screen.blit(v_text, (screen.get_width()/2 + 205, screen.get_height() / 2 - 90))screen.blit(ex_text, (screen.get_width()/2 - 95, screen.get_height() / 2 + 110))screen.blit(game_name, (screen.get_width()/2 - 180, screen.get_height() / 2-300))screen.blit(special_text, (screen.get_width()/2 - 180, screen.get_height() / 2+400))easy_text = menu_font.render("30", True, "green")hard_text = menu_font.render("20", True, "yellow")very_hard_text = menu_font.render("15", True, "red")explain_rect_text = menu_font.render("游戏说明", True, "blue")if mouse.colliderect(easy_rect):screen.blit(easy_text, mouse_position)pygame.draw.rect(screen, "black", easy_rect, 5)mouse = pygame.draw.circle(screen, "green", mouse_position, 10)if pygame.mouse.get_pressed()[0]:start = Truewin_score = 30if mouse.colliderect(hard_rect):screen.blit(hard_text, mouse_position)pygame.draw.rect(screen, "black", hard_rect, 5)mouse = pygame.draw.circle(screen, "yellow", mouse_position, 10)if pygame.mouse.get_pressed()[0]:start = Truebar_speed = 10down_y = 6path_y = 17win_score = 20if mouse.colliderect(very_hard_rect):screen.blit(very_hard_text, mouse_position)pygame.draw.rect(screen, "black", very_hard_rect, 5)mouse = pygame.draw.circle(screen, "red", mouse_position, 10)if pygame.mouse.get_pressed()[0]:start = Truebar_speed = 12down_y = 8path_y = 20win_score = 15if mouse.colliderect(explain_rect):screen.blit(explain_rect_text, mouse_position)pygame.draw.rect(screen, "black", explain_rect, 5)mouse = pygame.draw.circle(screen, "blue", mouse_position, 10)if pygame.mouse.get_pressed()[0]:screen.fill("white")explain = game_name_font.render("游戏说明", True, "black")screen.blit(explain, ((screen.get_width()/2) - 200 , screen.get_height() / 2 - 400))explain_text = menu_font.render("1.按下空格键或鼠标右键使小鸟上升", True, "black")screen.blit(explain_text, ((screen.get_width()/2) - 350 , screen.get_height() / 2 - 200))explain_text = menu_font.render("2.松开空格键或鼠标右键使小鸟下降", True, "black")screen.blit(explain_text, ((screen.get_width()/2) - 350 , screen.get_height() / 2 - 100))explain_text = menu_font.render("3.碰到柱子或者地面游戏结束", True, "black")screen.blit(explain_text, ((screen.get_width()/2) - 350 , screen.get_height() / 2 - 0))explain_text = menu_font.render("4.按下ESC键退出游戏", True, "black")screen.blit(explain_text, ((screen.get_width()/2) - 350 , screen.get_height() / 2 + 100))keys = pygame.key.get_pressed()if keys[pygame.K_ESCAPE]:running = Falseif start == True :now_time = pygame.time.get_ticks() - start_timeif not_press == 0:not_press = pygame.time.get_ticks()/300if press_time == 0:press_time = pygame.time.get_ticks()/300cir = pygame.draw.circle(screen, "white", player_pos, point_size)bird = pygame.transform.scale(bird, (point_size*2, point_size*2))screen.blit(bird, (player_pos.x - point_size, player_pos.y - point_size))keys = pygame.key.get_pressed()if keys[pygame.K_SPACE] or pygame.mouse.get_pressed()[2]:between_press = pygame.time.get_ticks()/300 - not_presspath = (path_y)*(between_press) + 0.5 * add_speed * (between_press) * (between_press)player_pos.y -= pathpress_time = pygame.time.get_ticks()/300elif keys[pygame.K_ESCAPE]:running = False else:not_press = pygame.time.get_ticks()/300press_button = Falsedelta_time = pygame.time.get_ticks()/300  - press_timeif bar_position > 0:bar_position -= bar_speedelse:bar_position = 1920 - bar_widthpass_locate = random_height()score += 1bar_speed += 0.5score_text = menu_font.render("SCORE:" + str(score), True, "black")screen.blit(score_text, (screen.get_width() - 1730,100))if score == win_score:win = Truepygame.mixer.music.stop()pygame.mixer.music.load("win.wav")pygame.mixer.music.play(-1)player_pos = pygame.mouse.get_pos()pygame.draw.circle(screen, "gold", player_pos, point_size)bar_width = 0bar_position = 1920 - bar_widthscreen.fill("white")color_egg = pygame.image.load("color_egg.jpg")screen.blit(color_egg, (0, 0))egg_text1 = menu_font.render("          恭喜你发现了隐藏彩蛋", True, "black")egg_text2 = menu_font.render("          左边是我最好的朋友,吉仔", True, "black")egg_text3 = menu_font.render("          你赢了!", True, "black")screen.blit(egg_text1, (screen.get_width()/2 ,400))screen.blit(egg_text2, (screen.get_width()/2 ,500))screen.blit(egg_text3, (screen.get_width()/2 ,600))pygame.display.flip()time.sleep(4)screen.fill("gold")win_text = game_name_font.render("YOU WIN", True, "RED")win_text2  = menu_font.render("将在三秒后退出游戏...", True, "black")screen.blit(win_text, ((screen.get_width()/2) - 200 , screen.get_height() / 2 - 100))screen.blit(win_text2, ((screen.get_width()/2) - 250 , screen.get_height() / 2 + 100))pygame.display.flip()time.sleep(2)breakrect1 = pygame.Rect(bar_position, 0, bar_width, pass_locate)rect2 = pygame.Rect(bar_position, pass_locate + 6*point_size, bar_width, 1080)pygame.draw.rect(screen, "green", rect1)pygame.draw.rect(screen, "green", rect2) player_pos.y += down_y * (delta_time) +   add_speed * (delta_time) * (delta_time)if cir.colliderect(rect2) or cir.colliderect(rect1) or player_pos.y > screen_height - 3 * point_size or player_pos.y < 0 + 4  * point_size:      start = Falsebar_width = 200bar_position = 1920 - bar_widthpass_locate = random_height()player_pos = pygame.Vector2(screen.get_width()/2, screen.get_height() / 2 )screen.fill("white")over = game_name_font.render("GAME OVER", True, "black")text_score = game_name_font.render(" SCORE:" + str(score), True, "red")screen.blit(over, ((screen.get_width()/2) - 200 , screen.get_height() / 2 - 100))screen.blit(text_score, ((screen.get_width()/2) - 200 , screen.get_height() / 2 + 100))bar_speed = 7score_history.append(score)best_score_text = menu_font.render("BEST SCORE:" + str(max(score_history)), True, "gold")screen.blit(best_score_text, (screen.get_width() - 1730,200))pygame.display.flip()press_time = 0score = 0#播放ENDpygame.mixer.music.stop()pygame.mixer.music.load("END.wav")pygame.mixer.music.play()keys = pygame.key.get_pressed()if keys[pygame.K_ESCAPE]:running = Falsepygame.time.wait(3000)pygame.mixer.music.load("START.wav")pygame.mixer.music.play(-1)dt = clock.tick(60) / 1000 pygame.display.flip() pygame.quit()

标题效果

        先来看看这个标题的效果

 

     虽然看着很酷炫(个人觉得),但是实际上并不是很复杂的原理,偶然之间发现了可以使用pygame获取鼠标的位置,然后我就异想天开,如果我在鼠标位置绘制个球会发生什么事情呢?然后就发现有个球一直追着我的鼠标跑,而且很流畅,噢!接着又尝试着向里面加入字体,就会发现,字体就会刚好出现在球的附近,那就完事了

        接下来进行一些小操作就行,把鼠标的位置画个球,然后用pygame自带的触碰检测函数就可以判断鼠标是不是点到我搞的框上面了,接着,如果接触到框,就改变颜色就行,至于怎么变色就是让它的框变成黑色,而宽度和原来的宽度一样就可以造出一个接触后变色的效果,具体代码都在上面自己看吧

        利用这点就可以控制游戏难度与搞一些花活

        鸟就是那个圆圈,思考了一下,发现其实本质上应该不会左右动才是,只是经过一些小操作让它看上去在左右移动,所以操作很简单,就只要控制鸟的上下移动就行

        但是,在看过那个原版游戏之后发现这个球啊,是会上下加速的,就比如你不按空格了,它就下降,而且下降的速度会逐渐增加,那么应该怎么操作呢?很简单的高中物理,给那个球搞个加速度,我称之为"伪重力",公式就是0.5*g*t^2,剩下就是怎么记录时间来加速,但是实际上这个操作很简单,就只要设置几个结点,比如你按空格或者没按空格的时候记录一下起始时间然后再减一下就行,但是要控制一下时间的重置和一些别的比较琐碎的事情,不然容易出现各种bug,总之处理完成后就会得到如下的效果:

柱子

        柱子的话本质上就是两个矩形,第一个柱子的高度就是直接随机,然后第二柱子的起始点就是第一个柱子的高度再增加几个球的大小的值,测试了一下发现没有问题,然后就是如何使柱子动起来,这点也很简单,和球的原理是一样的,只要一直增加或减去某个方向的值就行,你如果想的话还可以倒着来,类似于UnderTale的操作(doge),然后再用pygame自带的触碰检测函数检测球有没有和柱子相交就完事了。效果就和上面显示的一样

重开与结束

        结束有几个情况,第一种是按ESC键就可以退出游戏,另外一种就是赢了就退出游戏,很简单的事情,没必要说

        重开比较复杂,首先要重置所有点然后再搞个战败画面,然后再返回到最开是的那个界面,反正实际上就是调参,没什么好说的,效果如下:

        

总体流程

 

 备注

        上面写的代码是包含了一些图片和音频的,那是我自己搞的彩蛋和自己用吉他弹的音乐,需要使用的话可以把包含这些操作的代码删掉就可以了,如果需要原本的内容的话可以发邮箱给我,我的邮箱地址是okura_machi@126.com,或者也可以去github上搜ifhjl,然后这个游戏就是在work项目里面的小游戏的文件夹里面,需要可以自取。

这篇关于Python自制 笨鸟先飞的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/321132

相关文章

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很