Python制作【小游戏合集】之愤怒的小鸟

本文介绍了如何使用Python的Pygame库开发愤怒的小鸟小游戏。内容包括游戏的开始界面、游戏原理、角色状态(小鸟、猪、木块)的定义,以及游戏主循环的实现。通过终端运行程序,玩家可以体验到游戏效果。
摘要由CSDN通过智能技术生成

导语:

哈喽铁汁们!!几天不上线的小游戏合集它又来了(主要是每日写小游戏小编怕你们会觉得腻)

 😳😳但如果铁汁萌有特别想玩的小游戏也可私信我啦!上线看到就会回的啦~

想领取完整源码跟Python学习资料的宝宝们可点击这行字体

 

今天给大家带来的小游戏呢就是愤怒的小鸟啦~废话不多说,让我们愉快地开始吧~

原理简介

这里简单介绍一下游戏的实现原理呗。首先是游戏的开始界面,大概是长这样的,比较简约:

图片

主要包括两个部分,即游戏的标题和游戏的开始以及退出按钮,这两部分的代码实现如下:


'''按钮类'''
class Button(pygame.sprite.Sprite):
def __init__(self, screen, x, y, width, height, action=None, color_not_active=(189, 195, 199), color_active=(189, 195, 199)):
        pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.action = action
self.screen = screen
self.color_active = color_active
self.color_not_active = color_not_active
'''添加文字'''
def addtext(self, text, size=20, font='Times New Roman', color=(0, 0, 0)):
self.font = pygame.font.Font(font, size)
self.text = self.font.render(text, True, color)
self.text_pos = self.text.get_rect()
self.text_pos.center = (self.x + self.width / 2, self.y + self.height / 2)
'''是否被鼠标选中'''
def selected(self):
        pos = pygame.mouse.get_pos()
if (self.x < pos[0] < self.x + self.width) and (self.y < pos[1] < self.y + self.height):
return True
return False
'''画到屏幕上'''
def draw(self):
if self.selected():
            pygame.draw.rect(self.screen, self.color_active, (self.x, self.y, self.width, self.height))
else:
            pygame.draw.rect(self.screen, self.color_not_active, (self.x, self.y, self.width, self.height))
if hasattr(self, 'text'):
self.screen.blit(self.text, self.text_pos)


'''文字标签类'''
class Label(pygame.sprite.Sprite):
def __init__(self, screen, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.screen = screen
'''添加文字'''
def addtext(self, text, size=20, font='Times New Roman', color=(0, 0, 0)):
self.font = pygame.font.Font(font, size)
self.text = self.font.render(text, True, color)
self.text_pos = self.text.get_rect()
self.text_pos.center = (self.x + self.width / 2, self.y + self.height / 2)
'''画到屏幕上'''
def draw(self):
if hasattr(self, 'text'):

 实现起来其实都比较简单,按钮类就是多了一个被鼠标选中之后(也就是鼠标的位置落在按钮的区域范围内时)改变颜色以直观地告诉玩家该按钮已经被选中了的功能。
如果玩家点击退出键(QUIT),则退出游戏:

def quitgame():
    pygame.quit()
    sys.exit()

若点击开始游戏按钮,则开始游戏:

def startgame():
    game_levels = GameLevels(cfg, screen)
    game_levels.start()

游戏界面大概长这样:

图片

玩家获胜的方法就是操作有限数量的小鸟将所有入侵的猪干掉,换句话说就是利用弹弓发射小鸟,让小鸟击中场上的所有猪。若小鸟全部发射完之后场上仍然有猪没有被击中,则玩家失败。判断游戏胜负关系的代码实现起来其实蛮简单的,大概是这样的:


'''游戏状态'''
def status(self, pigs, birds):
    status_codes = {
'gaming': 0,
'failure': 1,
'victory': 2,
    }
if len(pigs) == 0: return status_codes['victory']
elif len(birds) == 0: return status_codes['failure']
else: return status_codes['gaming']

 接着,为了实现游戏,我们先定义一下所有我们需要的游戏精灵类。首先,是我们的主角,愤怒的小鸟:


'''小鸟'''
class Bird(pygame.sprite.Sprite):
def __init__(self, screen, imagepaths, loc_info, velocity=None, color=(255, 255, 255), **kwargs):
        pygame.sprite.Sprite.__init__(self)
        assert len(loc_info) == 3
        assert len(imagepaths) == 1
# 设置必要的属性常量
self.color = color
self.screen = screen
self.loc_info = list(loc_info)
self.imagepaths = imagepaths
self.velocity = VelocityVector() if velocity is None else velocity
self.type = 'bird'
self.fly_path = []
self.is_dead = False
self.elasticity = 0.8
self.is_loaded = False
self.is_selected = False
self.inverse_friction = 0.99
self.gravity = VelocityVector(0.2, math.p
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值