广告
返回顶部
首页 > 资讯 > 后端开发 > Python >如何使用Python实现愤怒小鸟游戏
  • 595
分享到

如何使用Python实现愤怒小鸟游戏

2023-06-15 08:06:37 595人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

这篇文章给大家分享的是有关如何使用python实现愤怒小鸟游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。开发工具Python版本:3.6.4相关模块:pygame模块;以及一些python自带的模块。环境搭建

这篇文章给大家分享的是有关如何使用python实现愤怒小鸟游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

开发工具

Python版本:3.6.4
相关模块:
pygame模块;
以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

原理介绍

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

如何使用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'):            self.screen.blit(self.text, self.text_pos)

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

如果玩家点击退出键(QUIT),则退出游戏:

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

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

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

游戏界面大概长这样:

如何使用Python实现愤怒小鸟游戏

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

'''游戏状态'''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.pi)        # 屏幕大小        self.screen_size = screen.get_rect().size        self.screen_size = (self.screen_size[0], self.screen_size[1] - 50)        # 导入图像        self.image = pygame.image.load(imagepaths[0])    '''画到屏幕上'''    def draw(self):        if not self.is_loaded:            for point in self.fly_path:                pygame.draw.ellipse(self.screen, self.color, (point[0], point[1], 3, 3), 1)        position = self.loc_info[0] - self.loc_info[2], self.loc_info[1] - self.loc_info[2]        self.screen.blit(self.image, position)    '''判断有没有被鼠标选中'''    def selected(self):        pos = pygame.mouse.get_pos()        dx, dy = pos[0] - self.loc_info[0], pos[1] - self.loc_info[1]        dist = math.hypot(dy, dx)        if dist < self.loc_info[2]:            return True        return False    '''加载到弹弓上'''    def load(self, slingshot):        self.loc_info[0], self.loc_info[1] = slingshot.x, slingshot.y        self.is_loaded = True    '''重新设置位置'''    def reposition(self, slingshot):        pos = pygame.mouse.get_pos()        if self.selected:            self.loc_info[0], self.loc_info[1] = pos[0], pos[1]            dx, dy = slingshot.x - self.loc_info[0], slingshot.y - self.loc_info[1]            self.velocity.magnitude = min(int(math.hypot(dx, dy) / 2), 80)            self.velocity.angle = math.pi / 2 + math.atan2(dy, dx)    '''显示发射小鸟的路径'''    def projectpath(self):        if self.is_loaded:            path = []            bird = Bird(self.screen, self.imagepaths, self.loc_info, velocity=self.velocity)            for i in range(30):                bird.move()                if i % 5 == 0: path.append((bird.loc_info[0], bird.loc_info[1]))            for point in path:                pygame.draw.ellipse(self.screen, self.color, (point[0], point[1], 2, 2))    '''移动小鸟'''    def move(self):        # 根据重力改变小鸟的速度向量        self.velocity = VectorAddition(self.velocity, self.gravity)        self.loc_info[0] += self.velocity.magnitude * math.sin(self.velocity.angle)        self.loc_info[1] -= self.velocity.magnitude * math.cos(self.velocity.angle)        self.velocity.magnitude *= self.inverse_friction        # 宽度超出屏幕        if self.loc_info[0] > self.screen_size[0] - self.loc_info[2]:            self.loc_info[0] = 2 * (self.screen_size[0] - self.loc_info[2]) - self.loc_info[0]            self.velocity.angle *= -1            self.velocity.magnitude *= self.elasticity        elif self.loc_info[0] < self.loc_info[2]:            self.loc_info[0] = 2 * self.loc_info[2] - self.loc_info[0]            self.velocity.angle *= -1            self.velocity.magnitude *= self.elasticity        # 高度超出屏幕        if self.loc_info[1] > self.screen_size[1] - self.loc_info[2]:            self.loc_info[1] = 2 * (self.screen_size[1] - self.loc_info[2]) - self.loc_info[1]            self.velocity.angle = math.pi - self.velocity.angle            self.velocity.magnitude *= self.elasticity        elif self.loc_info[1] < self.loc_info[2]:            self.loc_info[1] = 2 * self.loc_info[2] - self.loc_info[1]            self.velocity.angle = math.pi - self.velocity.angle            self.velocity.magnitude *= self.elasticity

实现它主要需要考虑的是小鸟其实存在五种状态:

  • 排队状态,即在左下角等待进入弹弓时的状态,静止不动即可;

  • 就绪状态,即进入弹弓准备被发射的状态,其需要跟着鼠标不断地移动,使得玩家知道自己目前调整小鸟所到的位置被发射出去之后大概会是 什么样的角度和路径;

  • 飞行状态,即被弹弓发射出去之后的状态,需要根据重力和小鸟的初速度来计算其飞行路径并不断地移动;

  • 碰撞状态,即在飞行过程中撞击到其他物体例如小猪和木桩等时,运动状态发生了改变;

  • 静止状态,即小鸟完成飞行状态之后最终静止时的状态。

接着来实现一下小猪:

'''猪'''class Pig(pygame.sprite.Sprite):    def __init__(self, screen, imagepaths, loc_info, velocity=None, **kwargs):        pygame.sprite.Sprite.__init__(self)        assert len(loc_info) == 3        assert len(imagepaths) == 3        # 设置必要的属性常量        self.screen = screen        self.loc_info = list(loc_info)        self.imagepaths = imagepaths        self.velocity = VelocityVector() if velocity is None else velocity        self.type = 'pig'        self.is_dead = False        self.elasticity = 0.8        self.switch_freq = 20        self.animate_count = 0        self.inverse_friction = 0.99        self.gravity = VelocityVector(0.2, math.pi)        # 屏幕大小        self.screen_size = screen.get_rect().size        self.screen_size = (self.screen_size[0], self.screen_size[1] - 50)        # 导入图像        self.pig_images = []        for imagepath in imagepaths: self.pig_images.append(pygame.image.load(imagepath))        # 设置当前图像        self.image = random.choice(self.pig_images[:2])    '''画到屏幕上'''    def draw(self):        self.animate_count += 1        if (self.animate_count % self.switch_freq == 0) and (not self.is_dead):            self.animate_count = 0            self.image = random.choice(self.pig_images[:2])        position = self.loc_info[0] - self.loc_info[2], self.loc_info[1] - self.loc_info[2]        self.screen.blit(self.image, position)    '''移动猪'''    def move(self):        # 根据重力改变猪的速度向量        self.velocity = VectorAddition(self.velocity, self.gravity)        self.loc_info[0] += self.velocity.magnitude * math.sin(self.velocity.angle)        self.loc_info[1] -= self.velocity.magnitude * math.cos(self.velocity.angle)        self.velocity.magnitude *= self.inverse_friction        # 宽度超出屏幕        if self.loc_info[0] > self.screen_size[0] - self.loc_info[2]:            self.loc_info[0] = 2 * (self.screen_size[0] - self.loc_info[2]) - self.loc_info[0]            self.velocity.angle *= -1            self.velocity.magnitude *= self.elasticity        elif self.loc_info[0] < self.loc_info[2]:            self.loc_info[0] = 2 * self.loc_info[2] - self.loc_info[0]            self.velocity.angle *= -1            self.velocity.magnitude *= self.elasticity        # 高度超出屏幕        if self.loc_info[1] > self.screen_size[1] - self.loc_info[2]:            self.loc_info[1] = 2 * (self.screen_size[1] - self.loc_info[2]) - self.loc_info[1]            self.velocity.angle = math.pi - self.velocity.angle            self.velocity.magnitude *= self.elasticity        elif self.loc_info[1] < self.loc_info[2]:            self.loc_info[1] = 2 * self.loc_info[2] - self.loc_info[1]            self.velocity.angle = math.pi - self.velocity.angle            self.velocity.magnitude *= self.elasticity    '''猪死掉了'''    def setdead(self):        self.is_dead = True        self.image = self.pig_images[-1]

猪在游戏中主要包括三种状态:

  • 静止状态,即未被击中时静止在某处的状态;

  • 被击中后的运动状态,即被其他物体击中之后根据动量守恒原理也一起运行时的状态;

  • 被击中后的静止状态,即因为被击中而产生运动之后又恢复静止时的状态、

游戏中的木块实现原理与小猪类似:

''地图里的木块'''class Block(pygame.sprite.Sprite):    def __init__(self, screen, imagepaths, loc_info, velocity=None, **kwargs):        pygame.sprite.Sprite.__init__(self)        assert len(loc_info) == 3        assert len(imagepaths) == 2        # 设置必要的属性常量        self.type = 'block'        self.screen = screen        self.loc_info = list(loc_info)        self.imagepaths = imagepaths        self.velocity = VelocityVector() if velocity is None else velocity        self.elasticity = 0.7        self.is_destroyed = False        self.inverse_friction = 0.99        self.gravity = VelocityVector(0.2, math.pi)        # 导入图像        self.block_images = []        for imagepath in imagepaths: self.block_images.append(pygame.transfORM.scale(pygame.image.load(imagepath), (100, 100)))        # 屏幕大小        self.screen_size = screen.get_rect().size        self.screen_size = (self.screen_size[0], self.screen_size[1] - 50)        # 设置当前图像        self.image = self.block_images[0]        self.rect = self.image.get_rect()        self.rotate_angle = math.radians(0)    '''画到屏幕上'''    def draw(self):        pygame.transform.rotate(self.image, self.rotate_angle)        self.screen.blit(self.image, (self.loc_info[0] - self.rect.width // 2, self.loc_info[1]))    '''设置为损坏状态'''    def setdestroy(self):        self.is_destroyed = True        self.image = self.block_images[1]    '''移动木块'''    def move(self):        # 根据重力改变木块的速度向量        self.velocity = VectorAddition(self.velocity, self.gravity)        self.loc_info[0] += self.velocity.magnitude * math.sin(self.velocity.angle)        self.loc_info[1] -= self.velocity.magnitude * math.cos(self.velocity.angle)        self.velocity.magnitude *= self.inverse_friction        # 宽度超出屏幕        if self.loc_info[0] > self.screen_size[0] - self.rect.width:            self.loc_info[0] = 2 * (self.screen_size[0] - self.rect.width) - self.loc_info[0]            self.velocity.angle *= -1            self.rotate_angle = -self.velocity.angle            self.velocity.magnitude *= self.elasticity        elif self.loc_info[0] < self.rect.width:            self.loc_info[0] = 2 * self.rect.width - self.loc_info[0]            self.velocity.angle *= -1            self.rotate_angle = -self.velocity.angle            self.velocity.magnitude *= self.elasticity        # 高度超出屏幕        if self.loc_info[1] > self.screen_size[1] - self.rect.height:            self.loc_info[1] = 2 * (self.screen_size[1] - self.rect.height) - self.loc_info[1]            self.velocity.angle = math.pi - self.velocity.angle            self.rotate_angle = math.pi - self.velocity.angle            self.velocity.magnitude *= self.elasticity        elif self.loc_info[1] < self.rect.height:            self.loc_info[1] = 2 * self.rect.height - self.loc_info[1]            self.velocity.angle = math.pi - self.velocity.angle            self.rotate_angle = math.pi - self.velocity.angle            self.velocity.magnitude *= self.elasticity

最后,我们来实现一下墙和弹弓就可以啦:

'''弹弓'''class Slingshot(pygame.sprite.Sprite):    def __init__(self, screen, x, y, width, height, color=(66, 73, 73), line_color=(100, 30, 22), **kwargs):        pygame.sprite.Sprite.__init__(self)        self.x = x        self.y = y        self.color = color        self.width = width        self.height = height        self.screen = screen        self.line_color = line_color        self.type = 'slingshot'    '''画到屏幕上'''    def draw(self, bird=None):        pygame.draw.rect(self.screen, self.color, (self.x, self.y + self.height * 1 / 3, self.width, self.height * 2 / 3))        if bird is not None and bird.is_loaded:            pygame.draw.line(self.screen, self.line_color, (self.x, self.y + self.height / 6), (bird.loc_info[0], bird.loc_info[1] + bird.loc_info[2] / 2), 10)            pygame.draw.line(self.screen, self.line_color, (self.x + self.width, self.y + self.height / 6), (bird.loc_info[0] + bird.loc_info[2], bird.loc_info[1] + bird.loc_info[2] / 2), 10)        pygame.draw.rect(self.screen, self.color, (self.x - self.width / 4, self.y, self.width / 2, self.height / 3), 5)        pygame.draw.rect(self.screen, self.color, (self.x + self.width * 3 / 4, self.y, self.width / 2, self.height / 3), 5)'''墙'''class Slab(pygame.sprite.Sprite):    def __init__(self, screen, imagepaths, x, y, width, height, color=(255, 255, 255)):        pygame.sprite.Sprite.__init__(self)        self.x = x        self.y = y        self.color = color        self.width = width        self.height = height        self.screen = screen        self.imagepaths = imagepaths        if self.width > self.height:            self.image = pygame.image.load(self.imagepaths[0])        else:            self.image = pygame.image.load(self.imagepaths[1])        self.image = pygame.transform.scale(self.image, (self.width, self.height))        self.type = 'wall'    '''画到屏幕上'''    def draw(self):        self.screen.blit(self.image, (self.x, self.y))

由此,我们完成了所有游戏精灵的定义,可以开始实现游戏的主循环啦,具体的代码实现如下:

'''开始游戏'''def start(self):    # 导入所有游戏精灵    game_sprites = self.loadlevelmap()    birds, pigs, blocks, walls = game_sprites['birds'], game_sprites['pigs'], game_sprites['blocks'], game_sprites['walls']    slingshot = Slingshot(self.screen, 200, self.screen_size[1] - 200, 30, 200)    birds[0].load(slingshot)    score_label = Label(self.screen, 50, 10, 100, 50)    score_label.addtext(f'SCORE: {self.score}', 25, self.cfg.FONTPATH['Comic_Kings'], (236, 240, 241))    birds_remaining_label = Label(self.screen, 120, 50, 100, 50)    birds_remaining_label.addtext(f"BIRDS REMAINING: {len(birds)}", 25, self.cfg.FONTPATH['Comic_Kings'], (236, 240, 241))    pigs_remaining_label = Label(self.screen, 110, 90, 100, 50)    pigs_remaining_label.addtext(f"PIGS REMAINING: {len(pigs)}", 25, self.cfg.FONTPATH['Comic_Kings'], (236, 240, 241))    carles_label = Label(self.screen, self.screen_size[0] - 270, self.screen_size[1] - 20, 300, 100)    carles_label.addtext('CARLES', 60, self.cfg.FONTPATH['arfmoochikncheez'], (113, 125, 126))    # 游戏主循环    clock = pygame.time.Clock()    blocks_to_remove, pigs_to_remove = [], []    while True:        # --按键检测        for event in pygame.event.get():            if event.type == pygame.QUIT:                self.quitgame()            elif event.type == pygame.KEYDOWN:                if event.key == pygame.K_q:                    self.quitgame()                elif event.key == pygame.K_r:                    self.start()                elif event.key == pygame.K_p or event.key == pygame.K_ESCAPE:                    self.pauseinterface()            elif event.type == pygame.MOUSEBUTTONDOWN:                if birds[0].selected():                    birds[0].is_selected = True            elif event.type == pygame.MOUSEBUTTONUP:                if birds[0].is_selected:                    birds[0].is_selected = False                    birds[0].start_flying = True        # --背景颜色填充        color = self.cfg.BACKGROUND_COLOR        for i in range(3):            color = (color[0] + 5, color[1] + 5, color[2] + 5)            pygame.draw.rect(self.screen, color, (0, i * 300, self.screen_size[0], 300))        pygame.draw.rect(self.screen, (77, 86, 86), (0, self.screen_size[1], self.screen_size[0], 50))        # --判断游戏是否结束,若没有则导入新的小鸟        if (not birds[0].is_loaded) and self.still(pigs + birds + blocks):            birds.pop(0)            if self.status(pigs, birds) == 2:                self.score += len(birds) * 100                self.switchlevelinterface()            elif self.status(pigs, birds) == 1:                self.failureinterface()            birds[0].load(slingshot)            birds[0].start_flying = False        # --重置小鸟的位置        if birds[0].is_selected:            birds[0].reposition(slingshot)        if hasattr(birds[0], 'start_flying') and birds[0].start_flying:            birds[0].is_loaded = False        # --弹弓        slingshot.draw(birds[0])        # --判断猪是否撞上木桩        for i in range(len(pigs)):            for j in range(len(blocks)):                pig_magnitude_1, block_magnitude_1 = pigs[i].velocity.magnitude, blocks[j].velocity.magnitude                pigs[i], blocks[j], is_collision = self.collision(pigs[i], blocks[j])                pig_magnitude_2, block_magnitude_2 = pigs[i].velocity.magnitude, blocks[j].velocity.magnitude                if is_collision:                    if abs(pig_magnitude_2 - pig_magnitude_2) > 2:                        blocks_to_remove.append(blocks[j])                        blocks[j].setdestroy()                    if abs(block_magnitude_2 - block_magnitude_1) > 2:                        pigs_to_remove.append(pigs[i])                        pigs[i].setdead()        # --判断鸟是否撞上木桩        for i in range(len(birds)):            if not (birds[i].is_loaded or birds[i].velocity.magnitude == 0):                for j in range(len(blocks)):                    bird_magnitude_1, block_magnitude_1 = birds[i].velocity.magnitude, blocks[j].velocity.magnitude                    birds[i], blocks[j], is_collision = self.collision(birds[i], blocks[j])                    bird_magnitude_2, block_magnitude_2 = birds[i].velocity.magnitude, blocks[j].velocity.magnitude                    if is_collision:                        if abs(bird_magnitude_1 - bird_magnitude_2) > 2:                            if blocks[j] not in blocks_to_remove:                                blocks_to_remove.append(blocks[j])                                blocks[j].setdestroy()        # --判断猪是否撞上猪或者猪撞墙        for i in range(len(pigs)):            pigs[i].move()            for j in range(i+1, len(pigs)):                pig1_magnitude_1, pig2_magnitude_1 = pigs[i].velocity.magnitude, pigs[j].velocity.magnitude                pigs[i], pigs[j], is_collision = self.collision(pigs[i], pigs[j])                pig1_magnitude_2, pig2_magnitude_2 = pigs[i].velocity.magnitude, pigs[j].velocity.magnitude                if abs(pig1_magnitude_1 - pig1_magnitude_2) > 2:                    if pigs[j] not in pigs_to_remove:                        pigs_to_remove.append(pigs[j])                        pigs[j].setdead()                if abs(pig2_magnitude_1 - pig2_magnitude_2) > 2:                    if pigs[i] not in pigs_to_remove:                        pigs_to_remove.append(pigs[i])                        pigs[i].setdead()            for wall in walls: pigs[i] = self.collision(pigs[i], wall)[0]            pigs[i].draw()        # --判断鸟是否撞到猪或者鸟是否撞到墙        for i in range(len(birds)):            if (not birds[i].is_loaded) and (birds[i].velocity.magnitude):                birds[i].move()                for j in range(len(pigs)):                    bird_magnitude_1, pig_magnitude_1 = birds[i].velocity.magnitude, pigs[j].velocity.magnitude                    birds[i], pigs[j], is_collision = self.collision(birds[i], pigs[j])                    bird_magnitude_2, pig_magnitude_2 = birds[i].velocity.magnitude, pigs[j].velocity.magnitude                    if is_collision:                        if abs(bird_magnitude_2 - bird_magnitude_1) > 2:                            if pigs[j] not in pigs_to_remove:                                pigs_to_remove.append(pigs[j])                                pigs[j].setdead()            if birds[i].is_loaded: birds[i].projectpath()            for wall in walls: birds[i] = self.collision(birds[i], wall)[0]            birds[i].draw()        # --判断木桩是否撞到了木桩或者木桩撞到墙        for i in range(len(blocks)):            for j in range(i+1, len(blocks)):                block1_magnitude_1, block2_magnitude_1 = blocks[i].velocity.magnitude, blocks[j].velocity.magnitude                blocks[i], blocks[j], is_collision = self.collision(blocks[i], blocks[j])                block1_magnitude_2, block2_magnitude_2 = blocks[i].velocity.magnitude, blocks[j].velocity.magnitude                if is_collision:                    if abs(block1_magnitude_2 - block1_magnitude_1) > 2:                        if blocks[j] not in blocks_to_remove:                            blocks_to_remove.append(blocks[j])                            blocks[j].setdestroy()                    if abs(block2_magnitude_2 - block2_magnitude_1) > 2:                        if blocks[i] not in blocks_to_remove:                            blocks_to_remove.append(blocks[i])                            blocks[i].setdestroy()            blocks[i].move()            for wall in walls: blocks[i] = self.collision(blocks[i], wall)[0]            blocks[i].draw()        # --墙        for wall in walls: wall.draw()        # --显示文字        score_label.addtext(f'SCORE: {self.score}', 25, self.cfg.FONTPATH['Comic_Kings'], (236, 240, 241))        score_label.draw()        birds_remaining_label.addtext(f"BIRDS REMAINING: {len(birds)}", 25, self.cfg.FONTPATH['Comic_Kings'], (236, 240, 241))        birds_remaining_label.draw()        pigs_remaining_label.addtext(f"PIGS REMAINING: {len(pigs)}", 25, self.cfg.FONTPATH['Comic_Kings'], (236, 240, 241))        pigs_remaining_label.draw()        carles_label.draw()        # --画面刷新        pygame.display.update()        clock.tick(self.cfg.FPS)        # --删除无效的元素        if self.still(birds + pigs + blocks):            for pig in pigs_to_remove:                if pig in pigs:                    pigs.remove(pig)                    self.score += 100            for block in blocks_to_remove:                if block in blocks:                    blocks.remove(block)                    self.score += 50            pigs_to_remove = []            blocks_to_remove = []

感谢各位的阅读!关于“如何使用Python实现愤怒小鸟游戏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: 如何使用Python实现愤怒小鸟游戏

本文链接: https://www.lsjlt.com/news/279295.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何使用Python实现愤怒小鸟游戏
    这篇文章给大家分享的是有关如何使用Python实现愤怒小鸟游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。开发工具Python版本:3.6.4相关模块:pygame模块;以及一些python自带的模块。环境搭建...
    99+
    2023-06-15
  • 忆童年!用Python实现愤怒的小鸟游戏
    开发工具 Python版本:3.6.4 相关模块: pygame模块; 以及一些python自带的模块。 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可。...
    99+
    2022-11-12
  • PythonPygame实战之愤怒的小鸟游戏实现
    目录前言一、运行环境二、代码展示 三、效果展示1)第一关2)闯关成功​3)其他关卡(随机截图)4)闯关失败前言 《愤怒的小鸟》其实活得还不错,尽管我们一直在嘲笑它的IP帝国...
    99+
    2022-11-13
  • 如何在python中利用pygame实现一个愤怒的小鸟游戏
    这篇文章将为大家详细讲解有关如何在python中利用pygame实现一个愤怒的小鸟游戏,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。代码展示import pygame,syspyg...
    99+
    2023-06-06
  • 怎么用html5实现愤怒的小鸟
    这篇文章主要介绍“怎么用html5实现愤怒的小鸟”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用html5实现愤怒的小鸟”文章能帮助大家解决问题。   简单而...
    99+
    2022-10-19
  • 如何使用纯CSS实现一只红色的愤怒小鸟
    这篇文章给大家分享的是有关如何使用纯CSS实现一只红色的愤怒小鸟的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。   代码解读   定义dom,容器中包含6个元素,分别代表头、...
    99+
    2022-10-19
  • 怎么用Python实现小鸟和水管之间碰撞的游戏
    本文小编为大家详细介绍“怎么用Python实现小鸟和水管之间碰撞的游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用Python实现小鸟和水管之间碰撞的游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。开...
    99+
    2023-06-27
  • 用python实现弹球小游戏
    目录一、弹球游戏代码 二、程序结果 总结一、弹球游戏代码  下文是tkinter的应用实例,实现弹球游戏,通过<--和-->件移动平板接球。...
    99+
    2022-11-13
  • 如何用Python实现炸弹人小游戏
    本篇内容主要讲解“如何用Python实现炸弹人小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何用Python实现炸弹人小游戏”吧!效果展示开发工具Python版本: 3.6.4相关模块:...
    99+
    2023-06-22
  • 用python做简单的小游戏,如何用python写小游戏
    大家好,小编来为大家解答以下问题,初学者怎么用python写简单小游戏教程,如何用python编写一个简单的小游戏,今天让我们一起来看看吧! 1、python简单小游戏代码   1、Python猜拳小游戏代码:   2、import r...
    99+
    2023-09-08
    人工智能
  • Python如何实现过迷宫小游戏
    小编给大家分享一下Python如何实现过迷宫小游戏,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!开发工具Python版本: 3.6.4相关模块:pygame模块;以及一些Python自带的模块。环境搭建安装Python并添...
    99+
    2023-06-22
  • 如何实现贪吃蛇Python小游戏
    这篇文章主要介绍“如何实现贪吃蛇Python小游戏”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何实现贪吃蛇Python小游戏”文章能帮助大家解决问题。贪吃蛇Python小游戏(源码+注释+粘贴即...
    99+
    2023-07-05
  • 如何使用Python编写小游戏?
    大家好,我是沐尘而生,如果你是一个热爱编程的小伙伴,又想尝试游戏开发,那么这篇文章一定能满足你的好奇心。不废话,让我们马上进入Python游戏开发的精彩世界吧! Python游戏开发的魅力 编写小游...
    99+
    2023-08-31
    python pygame 开发语言
  • 如何用python实现海龟赛跑小游戏
    这篇“如何用python实现海龟赛跑小游戏”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何用python实现海龟赛跑小游戏...
    99+
    2023-06-26
  • 如何用Python实现问题回答小游戏
    这篇文章将为大家详细讲解有关如何用Python实现问题回答小游戏,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。读取问题如下所示,我们在文本中写了一个问题,然后将其读取出来。“黄河远上白云间,...
    99+
    2023-06-22
  • 用python实现打砖块小游戏
    本文实例为大家分享了python实现打砖块小游戏的具体代码,供大家参考,具体内容如下 开发益智的打砖块小游戏,你可以试一下能打几块 import pygame,sys,time,ra...
    99+
    2022-11-11
  • 怎么用Python实现2048小游戏
    这篇文章主要介绍怎么用Python实现2048小游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、开发环境Python版本:3.6.4相关模块:pygame模块;以及一些Python自带的模块。二、环境搭建安装P...
    99+
    2023-06-15
  • 只用400行Java代码就能实现的飞翔的小鸟游戏
    目录一、效果图二、设计步骤1.素材准备2.预备知识及其运用3.大体流程三、源码1.游戏类2.地面类3.小鸟类4.柱子类一、效果图 二、设计步骤 1.素材准备 相关的素材我放到了g...
    99+
    2022-11-12
  • 如何使用Java实现扫雷小游戏
    这篇文章主要介绍“如何使用Java实现扫雷小游戏”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何使用Java实现扫雷小游戏”文章能帮助大家解决问题。效果展示主类:GameWin类package&n...
    99+
    2023-06-30
  • Python如何实现我的世界小游戏
    小编给大家分享一下Python如何实现我的世界小游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!我的世界小游戏使用方法:移动前进:W,后退:S,向左:A,向右:...
    99+
    2023-06-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作