python/pygame 挑战魂斗罗 笔记(二)

news/2025/6/19 17:33:30

一、建立地面碰撞体:

现在主角Bill能够站立在游戏地图的地面,是因为我们初始化的时候把Bill的位置固定了self.rect.y = 250。而不是真正的站在地图的地面上。

背景地图是一个完整的地图,没有地面、台阶的概念,就无法通过碰撞检测来实现玩家角色在各台阶地面上的移动跳跃,可以考虑在PS中把地面、台阶给提取出来,让角色可以通过碰撞检测来实现,但这需要重新PS中修改地图,并在代码中加载好多个图片。

这里采用的是在所有地面、台阶的位置画一条线,暂时就叫地面碰撞体吧。然后实现主角Bill和这些地面碰撞体发生碰撞,从而让主角Bill能够站在这个碰撞体上面。

1、先写出地面碰撞体的类:

在ContraMap.py中增加CollideGround类:

class CollideGround(pygame.sprite.Sprite):def __init__(self, length, x, y):pygame.sprite.Sprite.__init__(self)self.image = pygame.Surface((length * Constant.MAP_SCALE, 3))self.image.fill((255, 0, 0))self.rect = self.image.get_rect()self.rect.x = x * Constant.MAP_SCALE - Constant.WIDTH * 2self.rect.y = y * Constant.MAP_SCALE

这个类很简单,就是一个3像素高的红色矩形。因为地图放大了3倍,同时我们在主角出现,也就是地图走到- Constant.WIDTH * 2的时候再把碰撞体画出来,把这些因素考虑进去,可以使在PS中测量碰撞体长度、坐标位置更方便一点。

2、测量并定义地面碰撞体:

在Config.py中增加一些存放碰撞体的组,测量可以在PS或其它画图软件中进行:

    collider = pygame.sprite.Group()collider83 = pygame.sprite.Group()collider115 = pygame.sprite.Group()collider146 = pygame.sprite.Group()collider163 = pygame.sprite.Group()collider178 = pygame.sprite.Group()collider211 = pygame.sprite.Group()collider231 = pygame.sprite.Group()

这里先测量并定义出前面的部分,其实有collider一个组就行,这里按照y坐标的位置分了很多组只是为了测量、加载的时候更清晰一点,不容易乱,最后统一加入collider组。

Variable.collider115.add(CollideGround(736, 832, 115)
)Variable.collider146.add(CollideGround(97, 959, 146),CollideGround(66, 1215, 146)
)Variable.collider163.add(CollideGround(95, 1440, 163)
)Variable.collider178.add(CollideGround(32, 1055, 178),CollideGround(32, 1151, 178)
)Variable.collider211.add(CollideGround(63, 1088, 211),CollideGround(63, 1407, 211)
)Variable.collider.add(Variable.collider83, Variable.collider115, Variable.collider146, Variable.collider163,Variable.collider178, Variable.collider211, Variable.collider231)
3、修改StateMap类的update方法,在Varibale.step==2时,加入all_sprites组进行绘制。
    def update(self):if self.order == 2:print('纵向地图')else:if Variable.step == 0 and self.rect.x >= -Constant.WIDTH:self.rect.x -= 10if Variable.step == 1 and self.rect.x > -Constant.WIDTH * 2:self.rect.x -= 10if self.rect.x == -Constant.WIDTH * 2:Variable.step = 2Variable.all_sprites.add(Variable.collider)

这样就得到了这张带红色地面线的图片。 

二、主角Bill移动+跳跃:

1、给主角Bill定义移动、跳跃、下落三种状态。

初始状态为下落,也就是主角从画面外降落,碰撞到地面碰撞体后把碰撞体的top值赋值给Bill的bottom,实现停止下落。

            self.falling = Trueself.jumping = Falseself.moving = False
2、常量中增加x、y两个方向的移动速度,以及模拟重力。
    SPEED_X = 3SPEED_Y = -10GRAVITY = 0.5
 3、先定义移动、跳跃、下落、动作图片序列四个方法:

移动需要考虑三种情况:

第一是开始直到人物走到屏幕中间,这部分是主角Bill正常移动;

第二是Bill移动到游戏窗口中间时,Bill需要一直停留在中间,而Bill的向前移动实际是地图向后移动,这里设立了一个x = self.rect.centerx - Constant.WIDTH / 2,也就是Bill移动超过游戏窗口中间的距离,然后让all_sprites中的所有精灵,包括Bill都向后移动这个距离。反过来也一样。

第三是地图移动到边界时就不能继续移动了,这时需要Bill继续移动,并保证不能走出游戏窗口。这里为了控制地图,需要获取地图的rect属性。网上搜了很多一直没找到怎么从all_sprites精灵组中获取指定精灵的方法,搞了好久,后来终于想到给地图单独一个组Varible.map_storage,通过遍历这个组来获取地图以及地图的rect属性。

    def move(self):if self.direction == 'right' and self.rect.right <= Constant.WIDTH - 80 * Constant.MAP_SCALE:self.rect.x += Constant.SPEED_Xif self.rect.centerx >= Constant.WIDTH / 2:x = self.rect.centerx - Constant.WIDTH / 2for j in Variable.map_storage:if j.rect.right >= Constant.WIDTH:for i in Variable.all_sprites:i.rect.x -= xelif self.direction == 'left' and self.rect.left >= 40 * Constant.MAP_SCALE:self.rect.x -= Constant.SPEED_Xif self.rect.centerx <= Constant.WIDTH / 2:x = Constant.WIDTH / 2 - self.rect.centerxfor j in Variable.map_storage:if j.rect.left <= -Constant.WIDTH * 2:for i in Variable.all_sprites:i.rect.x += x

图片序列:就是正常设定时间钟,然后image_order在0-5中间循环。

跳跃:先设定SPEED_Y为-10,GRAVITY为0.5,这样向上移动SPPED_Y的值逐渐减小,直到SPPED_Y==0,调整jumping和falling状态,并将SPEED_Y恢复为-10,完成一次跳跃。

下落:简单的写了一个10倍Constant.GRAVITY 下落。

    def order_loop(self):self.now_time = pygame.time.get_ticks()if self.now_time - self.last_time >= 100:self.image_order += 1if self.image_order > 5:self.image_order = 0self.last_time = self.now_timedef jump(self):Constant.SPEED_Y += Constant.GRAVITYself.rect.y += Constant.SPEED_Yif Constant.SPEED_Y == 0:self.jumping = Falseself.falling = TrueConstant.SPEED_Y = -10def fall(self):self.rect.y += Constant.GRAVITY * 10
4、修改update方法,实现跳下功能:

前后移动以及跳跃都可以根据设定的按键来修改状态以及图片类型。

重点是向下跳跃,向下跳跃实现的思路,是将Bill的状态设定为一直处于下落状态(跳跃时除外),让他与地面碰撞体一直碰撞,并将碰撞体的top值赋值给self.floor变量,然后向下跳的时候(s和j键同时按下),将该碰撞体从collider中删除并用sprite_storage组暂时寄存,实现Bill往下落:

            if key_pressed[pygame.K_s] and key_pressed[pygame.K_j]:self.image_type = 'oblique_down'Variable.collider.remove(l[0])Variable.sprite_storage.add(l[0])

等到Bill的rect.top值大于self.floor时,也就是完全超过哪条地面线的位置,再把该碰撞体加载回来,否则就会出现跳下一半就检测到碰撞,被拉回原来地面的情况。加回碰撞体后,就可以实现跳回来的动作。

        if self.rect.top >= self.floor:for i in Variable.sprite_storage:Variable.collider.add(i)Variable.sprite_storage.remove(i)

其它的部分,就是按照按键以及按键组合,调整各自的状态参数就可以了。

把按键监测写到碰撞后的for循环里面,是想控制主角Bill必须是与碰撞体接触后才能进行移动及跳跃等操作,否则在刚开始下落的时候就可以移动了,也会出现半空中继续跳跃的情况。 

    def update(self):self.image = self.image_dict[self.image_type][self.image_order]if self.direction == 'left':self.image = pygame.transform.flip(self.image, True, False)if self.rect.top >= self.floor:for i in Variable.sprite_storage:Variable.collider.add(i)Variable.sprite_storage.remove(i)key_pressed = pygame.key.get_pressed()if self.falling:self.fall()if self.jumping:self.jump()if self.moving:self.move()self.order_loop()collider_ground = pygame.sprite.groupcollide(Variable.player_sprites, Variable.collider, False, False)for p, l in collider_ground.items():self.floor = l[0].rect.topp.rect.bottom = self.floorif key_pressed[pygame.K_d]:self.direction = 'right'self.moving = Trueif key_pressed[pygame.K_w]:self.image_type = 'oblique_up'elif key_pressed[pygame.K_s]:self.image_type = 'oblique_down'elif key_pressed[pygame.K_j]:self.image_type = 'jump'self.jumping = Trueself.falling = Falseelse:self.image_type = 'run'elif key_pressed[pygame.K_a]:self.direction = 'left'self.moving = Trueif key_pressed[pygame.K_w]:self.image_type = 'oblique_up'elif key_pressed[pygame.K_s]:self.image_type = 'oblique_down'elif key_pressed[pygame.K_j]:self.image_type = 'jump'self.jumping = Trueself.falling = Falseelse:self.image_type = 'run'elif key_pressed[pygame.K_w]:self.image_type = 'up'self.moving = Falseelif key_pressed[pygame.K_s]:self.image_type = 'down'self.moving = Falseelse:self.image_type = 'stand'self.moving = Falseif key_pressed[pygame.K_s] and key_pressed[pygame.K_j]:self.image_type = 'oblique_down'Variable.collider.remove(l[0])Variable.sprite_storage.add(l[0])

这一部分功能的实现费了挺长时间,前前后后改了很多次才终于完成,虽然总觉得跳的有点别扭,但总算是实现了跳下跳回的功能,就这样吧。

三、现阶段各文件完整代码:

1、Contra.py
# Contra.py
import sys
import pygameimport ContraBill
import ContraMap
from Config import Constant, Variabledef control():for event in pygame.event.get():if event.type == pygame.QUIT:Variable.game_start = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_RETURN:Variable.step = 1class Main:def __init__(self):pygame.init()self.game_window = pygame.display.set_mode((Constant.WIDTH, Constant.HEIGHT))self.clock = pygame.time.Clock()def game_loop(self):while Variable.game_start:control()if Variable.stage == 1:ContraMap.new_stage()ContraBill.new_player()if Variable.stage == 2:passif Variable.stage == 3:passVariable.all_sprites.draw(self.game_window)Variable.all_sprites.update()self.clock.tick(Constant.FPS)pygame.display.set_caption(f'魂斗罗  1.0    {self.clock.get_fps():.2f}')pygame.display.update()pygame.quit()sys.exit()if __name__ == '__main__':main = Main()main.game_loop()
2、ContraMap.py

这里把第一关的全部地面碰撞体都测量并加载出来。

# ContraMap.py
import os
import pygamefrom Config import Constant, Variableclass StageMap(pygame.sprite.Sprite):def __init__(self, order):pygame.sprite.Sprite.__init__(self)self.image_list = []self.order = orderfor i in range(1, 9):image = pygame.image.load(os.path.join('image', 'map', 'stage' + str(i) + '.png'))rect = image.get_rect()image = pygame.transform.scale(image, (rect.width * Constant.MAP_SCALE, rect.height * Constant.MAP_SCALE))self.image_list.append(image)self.image = self.image_list[self.order]self.rect = self.image.get_rect()self.rect.x = 0self.rect.y = 0self.speed = 0def update(self):if self.order == 2:print('纵向地图')else:if Variable.step == 0 and self.rect.x >= -Constant.WIDTH:self.rect.x -= 10if Variable.step == 1 and self.rect.x > -Constant.WIDTH * 2:self.rect.x -= 10if self.rect.x == -Constant.WIDTH * 2:Variable.step = 2Variable.all_sprites.add(Variable.collider)def new_stage():if Variable.map_switch:stage_map = StageMap(Variable.stage - 1)Variable.all_sprites.add(stage_map)Variable.map_storage.add(stage_map)Variable.map_switch = Falseclass CollideGround(pygame.sprite.Sprite):def __init__(self, length, x, y):pygame.sprite.Sprite.__init__(self)self.image = pygame.Surface((length * Constant.MAP_SCALE, 3))self.image.fill((255, 0, 0))self.rect = self.image.get_rect()self.rect.x = x * Constant.MAP_SCALE - Constant.WIDTH * 2self.rect.y = y * Constant.MAP_SCALEVariable.collider83.add(CollideGround(511, 2176, 83),CollideGround(161, 2847, 83),CollideGround(64, 3296, 83)
)Variable.collider115.add(CollideGround(736, 832, 115),CollideGround(128, 1568, 115),CollideGround(160, 1695, 115),CollideGround(128, 1856, 115),CollideGround(256, 1984, 115),CollideGround(224, 2656, 115),CollideGround(65, 3040, 115),CollideGround(64, 3264, 115),CollideGround(64, 3392, 115),CollideGround(128, 3808, 115)
)Variable.collider146.add(CollideGround(97, 959, 146),CollideGround(66, 1215, 146),CollideGround(225, 2400, 146),CollideGround(96, 2976, 146),CollideGround(64, 3136, 146),CollideGround(160, 3424, 146),CollideGround(64, 3744, 146),CollideGround(32, 3936, 146)
)Variable.collider163.add(CollideGround(95, 1440, 163),CollideGround(64, 2304, 163),CollideGround(32, 2912, 163),CollideGround(32, 2328, 163),CollideGround(32, 3328, 163),CollideGround(97, 3840, 163)
)Variable.collider178.add(CollideGround(32, 1055, 178),CollideGround(32, 1151, 178),CollideGround(65, 2720, 178),CollideGround(64, 2816, 178),CollideGround(96, 3168, 178),CollideGround(63, 3648, 178),CollideGround(32, 3969, 178)
)Variable.collider211.add(CollideGround(63, 1088, 211),CollideGround(63, 1407, 211),CollideGround(97, 2208, 211),CollideGround(192, 2528, 211),CollideGround(33, 3135, 211),CollideGround(33, 3295, 211),CollideGround(97, 3520, 211),CollideGround(242, 3807, 211)
)Variable.collider.add(Variable.collider83, Variable.collider115, Variable.collider146, Variable.collider163,Variable.collider178, Variable.collider211, Variable.collider231)
3、ContraBill.py
# ContraBill.py
import os
import pygamefrom Config import Constant, Variableclass Bill(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image_dict = {'be_hit': [],'down': [],'jump': [],'oblique_down': [],'oblique_up': [],'run': [],'shoot': [],'stand': [],'up': []}for i in range(6):for key in self.image_dict:image = pygame.image.load(os.path.join('image', 'bill', str(key), str(key) + str(i + 1) + '.png'))rect = image.get_rect()image_scale = pygame.transform.scale(image, (rect.width * Constant.PLAYER_SCALE, rect.height * Constant.PLAYER_SCALE))self.image_dict[key].append(image_scale)self.image_order = 0self.image_type = 'stand'self.image = self.image_dict[self.image_type][self.image_order]self.rect = self.image.get_rect()self.rect.x = 100self.rect.y = 100self.direction = 'right'self.now_time = 0self.last_time = 0self.falling = Trueself.jumping = Falseself.moving = Falseself.floor = 0def update(self):self.image = self.image_dict[self.image_type][self.image_order]if self.direction == 'left':self.image = pygame.transform.flip(self.image, True, False)if self.rect.top >= self.floor:for i in Variable.sprite_storage:Variable.collider.add(i)Variable.sprite_storage.remove(i)key_pressed = pygame.key.get_pressed()if self.falling:self.fall()if self.jumping:self.jump()if self.moving:self.move()self.order_loop()collider_ground = pygame.sprite.groupcollide(Variable.player_sprites, Variable.collider, False, False)for p, l in collider_ground.items():self.floor = l[0].rect.topp.rect.bottom = self.floorif key_pressed[pygame.K_d]:self.direction = 'right'self.moving = Trueif key_pressed[pygame.K_w]:self.image_type = 'oblique_up'elif key_pressed[pygame.K_s]:self.image_type = 'oblique_down'elif key_pressed[pygame.K_j]:self.image_type = 'jump'self.jumping = Trueself.falling = Falseelse:self.image_type = 'run'elif key_pressed[pygame.K_a]:self.direction = 'left'self.moving = Trueif key_pressed[pygame.K_w]:self.image_type = 'oblique_up'elif key_pressed[pygame.K_s]:self.image_type = 'oblique_down'elif key_pressed[pygame.K_j]:self.image_type = 'jump'self.jumping = Trueself.falling = Falseelse:self.image_type = 'run'elif key_pressed[pygame.K_w]:self.image_type = 'up'self.moving = Falseelif key_pressed[pygame.K_s]:self.image_type = 'down'self.moving = Falseelse:self.image_type = 'stand'self.moving = Falseif key_pressed[pygame.K_s] and key_pressed[pygame.K_j]:self.image_type = 'oblique_down'Variable.collider.remove(l[0])Variable.sprite_storage.add(l[0])def order_loop(self):self.now_time = pygame.time.get_ticks()if self.now_time - self.last_time >= 100:self.image_order += 1if self.image_order > 5:self.image_order = 0self.last_time = self.now_timedef move(self):if self.direction == 'right' and self.rect.right <= Constant.WIDTH - 80 * Constant.MAP_SCALE:self.rect.x += Constant.SPEED_Xif self.rect.centerx >= Constant.WIDTH / 2:x = self.rect.centerx - Constant.WIDTH / 2for j in Variable.map_storage:if j.rect.right >= Constant.WIDTH:for i in Variable.all_sprites:i.rect.x -= xelif self.direction == 'left' and self.rect.left >= 40 * Constant.MAP_SCALE:self.rect.x -= Constant.SPEED_Xif self.rect.centerx <= Constant.WIDTH / 2:x = Constant.WIDTH / 2 - self.rect.centerxfor j in Variable.map_storage:if j.rect.left <= -Constant.WIDTH * 2:for i in Variable.all_sprites:i.rect.x += xdef jump(self):Constant.SPEED_Y += Constant.GRAVITYself.rect.y += Constant.SPEED_Yif Constant.SPEED_Y == 0:self.jumping = Falseself.falling = TrueConstant.SPEED_Y = -10def fall(self):self.rect.y += Constant.GRAVITY * 10def new_player():if Variable.step == 2 and Variable.player_switch:bill = Bill()Variable.all_sprites.add(bill)Variable.player_sprites.add(bill)Variable.player_switch = False
4、Config.py
# Config.py
import pygameclass Constant:WIDTH = 1200HEIGHT = 720FPS = 60MAP_SCALE = 3PLAYER_SCALE = 2.5SPEED_X = 3SPEED_Y = -10GRAVITY = 0.5class Variable:all_sprites = pygame.sprite.Group()player_sprites = pygame.sprite.Group()collider = pygame.sprite.Group()collider83 = pygame.sprite.Group()collider115 = pygame.sprite.Group()collider146 = pygame.sprite.Group()collider163 = pygame.sprite.Group()collider178 = pygame.sprite.Group()collider211 = pygame.sprite.Group()collider231 = pygame.sprite.Group()sprite_storage = pygame.sprite.Group()map_storage = pygame.sprite.Group()game_start = Truemap_switch = Trueplayer_switch = Truestage = 1step = 0

以上代码完成,我们的主角Bill终于可以连跑带跳游览整个地图,并终于站在了第一关的关头。


https://dhexx.cn/news/show-5318037.html

相关文章

安全开发之碰撞检测与伤害计算逻辑

一、什么是碰撞检测逻辑&#xff1f; 用通俗移动的话来说&#xff0c;碰撞检测就是一门检测两部分运动轨迹是否碰到一起的逻辑&#xff0c;在游戏中一般至少包含2方面的碰撞检测逻辑&#xff1a;一、核心玩法的碰撞检测逻辑&#xff1b;二、运动碰撞检测逻辑。 关于核心玩法的…

Linux--进程间的通信-命名管道

前文&#xff1a; Linux–进程间的通信-匿名管道 Linux–进程间的通信–进程池 命名管道的概念 命名管道是一种进程间通信&#xff08;IPC&#xff09;机制&#xff0c;运行不同进程之间进行可靠的、单向或双向的数据通信。 特点和作用&#xff1a; 跨平台性&#xff1a;在W…

单页面首屏优化,打包后大小减少64M,加载速度快了13.6秒

需求背景 从第三方采购的vue2 ElementUI实现的云管平台&#xff0c;乙方说2011年左右就开始有这个项目了&#xff08;那时候有Vue了吗&#xff0c;思考.jpg&#xff09;。十几年的项目&#xff0c;我何德何能可以担此责任。里面的代码经过多人多年迭代可以用惨不忍睹来形容&a…

基于监控视频的车辆检测

目前常用的基于监控视频的车辆检测方法分为两类&#xff1a;基于运动信息的车辆检测方法和基于特征信息的车辆检测方法。基于运动信息的车辆检测方法主要包括光流法、帧差法和背景法等。基于特征的车辆检测&#xff0c;是以统计机器学习理论为基础的车辆检测方法&#xff0c;通…

韩顺平 | 零基础快速学Python(12) OOP基础

面向对象编程-基础 类与对象 类提供了把数据和功能绑定在一起的方法。创建新类时创建了新的对象类型&#xff0c;从而能够创建该类型的新实例/对象。 类时抽象的概念&#xff0c;作为数据类型代表一类事物&#xff1b;对象时具体实际的&#xff0c;作为实例代表具体事物&…

第十六届“华中杯”大学生数学建模挑战赛B题思路

B题 使用行车轨迹估计交通信号灯周期问题 某电子地图服务商希望获取城市路网中所有交通信号灯的红绿周期,以便为司机提供更好的导航服务。由于许多信号灯未接入网络,无法直接从交通管理部门获取所有信号灯的数据,也不可能在所有路口安排人工读取信号灯周期信息。所以,该公…

【Linux系列】Ctrl + R 的使用

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

【C 数据结构】静态链表

文章目录 【 1. 基本原理 】1.1 静态链表中的节点1.2 备用链表 【 2. 静态链表的创建 】2.1 实例1 - 创建静态链表&#xff0c;指定值2.2 实例2 - 创建静态链表&#xff0c;默认值 【 3. 静态链表 添加元素 】【 4. 静态链表 删除元素 】【 5. 静态链表 查找元素 】【 6. 静态链…

Linux的学习之路:5、粘滞位与vim

摘要 这里主要是把上章没说完的权限的粘滞位说一下&#xff0c;然后就是vim的一些操作。 目录 摘要 一、粘滞位 二、权限总结 三、vim的基本概念 四、vim的基本操作 五、vim正常模式命令集 1、插入模式 2、从插入模式切换为命令模式 3、移动光标 4、删除文字 5、复…

Storm详细配置

要详细配置 Apache Storm&#xff0c;你需要关注以下几个方面&#xff1a; Topology配置&#xff1a; ● 定义你的拓扑结构&#xff0c;包括哪些Spout和Bolt将被使用&#xff0c;它们之间的连接关系&#xff0c;以及拓扑如何处理数据流。 ● 设置每个组件的并行度&#xff0c…