当前位置: 首页 > news >正文

从零开始学Python游戏编程40-碰撞处理2

4 绘制防御塔

从图2中可以看出,防御塔实际上是图4中第2行第1列的图像与第7行第1列图像的组合,绘制防御塔的代码只需要将图5中绘制完整坦克代码中相应的行列坐标改为防御塔所需的行列坐标即可,在render()方法中。代码如图7所示。

图7 绘制防御塔的代码

其中,第82行和90行代码中的gameState.tower1Pos以及gameState.tower2Pos指定了防御塔的位置;第83行、第86行、第91行和第94行指定了绘制防御塔所需的图片。

5 碰撞处理

从图1中可以看出,玩家控制的坦克无法放在防御塔所在的方框中,以上功能通过GameState类的update()方法实现。

《从零开始学Python游戏编程38-精灵5》中提到的GameState类的update()方法中,根据控制指令修改玩家坦克的位置,还要对玩家坦克的位置超出游戏屏幕范围的情况进行处理。而加入碰撞处理的update()方法代码如图8所示。

图8 加入碰撞处理的update()方法代码

第22行代码创建newTankPos变量,其值为之前坦克的位置加上控制指令。newTankPos可以看作是坦克“理论上”的新位置,“理论上”的新位置指的是只有某些条件满足时,坦克才会出现在新位置上,第24-26行就指定了这些条件,包括坦克的位置必须在游戏窗口范围内,并且新位置不能在两个防御塔的位置上,满足以上条件,第27行才会将坦克的位置设置为新位置,否则坦克的位置不会改变。通过以上代码,完成了碰撞处理。

6 完整代码

碰撞处理的完整代码如下所示。

import os
import pygame
from pygame import Rect
from pygame.math import Vector2class GameState():def __init__(self):self.worldSize = Vector2(16,10)self.tankPos = Vector2(0,0)self.tower1Pos = Vector2(10, 3)self.tower2Pos = Vector2(10, 5)def update(self,moveTankCommand):newTankPos = self.tankPos + moveTankCommandif  newTankPos.x >= 0 and newTankPos.x < self.worldSize.x \and newTankPos.y >= 0 and newTankPos.y < self.worldSize.y \and newTankPos != self.tower1Pos and newTankPos != self.tower2Pos:self.tankPos = newTankPosclass UserInterface():def __init__(self):pygame.init()self.gameState = GameState()self.unitsTexture = pygame.image.load("units.png")self.cellSize = Vector2(64,64)windowSize = self.gameState.worldSize.elementwise() * self.cellSizeself.window = pygame.display.set_mode((int(windowSize.x),int(windowSize.y)))pygame.display.set_caption("移动坦克")self.moveTankCommand = Vector2(0,0)self.clock = pygame.time.Clock()self.running = Truedef processInput(self):self.moveTankCommand = Vector2(0,0)for event in pygame.event.get():if event.type == pygame.QUIT:self.running = Falsebreakelif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:self.running = Falsebreakelif event.key == pygame.K_RIGHT:self.moveTankCommand.x = 1elif event.key == pygame.K_LEFT:self.moveTankCommand.x = -1elif event.key == pygame.K_DOWN:self.moveTankCommand.y = 1elif event.key == pygame.K_UP:self.moveTankCommand.y = -1def update(self):self.gameState.update(self.moveTankCommand)def render(self):self.window.fill((0,0,0))spritePoint = self.gameState.tankPos.elementwise()*self.cellSizetexturePoint = Vector2(1,0).elementwise()*self.cellSizetextureRect = Rect(int(texturePoint.x), int(texturePoint.y),int(self.cellSize.x),int(self.cellSize.y))self.window.blit(self.unitsTexture,spritePoint,textureRect)texturePoint = Vector2(0,6).elementwise()*self.cellSizetextureRect = Rect(int(texturePoint.x), int(texturePoint.y),int(self.cellSize.x),int(self.cellSize.y))self.window.blit(self.unitsTexture,spritePoint,textureRect)spritePoint = self.gameState.tower1Pos.elementwise()*self.cellSizetexturePoint = Vector2(0,1).elementwise()*self.cellSizetextureRect = Rect(int(texturePoint.x), int(texturePoint.y), int(self.cellSize.x),int(self.cellSize.y))self.window.blit(self.unitsTexture,spritePoint,textureRect)texturePoint = Vector2(0,6).elementwise()*self.cellSizetextureRect = Rect(int(texturePoint.x), int(texturePoint.y), int(self.cellSize.x),int(self.cellSize.y))self.window.blit(self.unitsTexture,spritePoint,textureRect)spritePoint = self.gameState.tower2Pos.elementwise()*self.cellSizetexturePoint = Vector2(0,1).elementwise()*self.cellSizetextureRect = Rect(int(texturePoint.x), int(texturePoint.y), int(self.cellSize.x),int(self.cellSize.y))self.window.blit(self.unitsTexture,spritePoint,textureRect)texturePoint = Vector2(0,6).elementwise()*self.cellSizetextureRect = Rect(int(texturePoint.x), int(texturePoint.y), int(self.cellSize.x),int(self.cellSize.y))self.window.blit(self.unitsTexture,spritePoint,textureRect)pygame.display.update()    def run(self):while self.running:self.processInput()self.update()self.render()self.clock.tick(60)userInterface = UserInterface()
userInterface.run()pygame.quit()

相关文章:

  • fps项目总结:生成武器子弹丧尸攻击
  • pyinstaller打包paddleocr发生错误解决
  • 【5】GD32 基础通信外设:USART、I2C、SPI
  • 正则表达式三剑客之——awk命令
  • OCR(Optical Character Recognition),光学字符识别
  • 使用 Python 项目管理工具 uv 快速创建 MCP 服务(Cherry Studio、Trae 添加 MCP 服务)
  • 通道降维方式
  • 一款好的私有云产品推荐——优刻得私有云(UCloudStack Pro)产品白皮书
  • 单机无穷大系统暂态稳定性仿真Matlab模型
  • 数据库-子查询、关联查询 和 TCL 语言
  • 智慧医疗领域TMI期刊2025年3月研究热点解析
  • 嵌入式:Linux系统应用程序(APP)启动参数及其规则详解
  • 【网络入侵检测】基于源码分析Suricata的PCAP模式
  • 计算器(WEB)
  • 流动式起重机Q2证考试有哪些科目?
  • C++与Python编写二进制转十进制
  • 机器人行业研究系列报告
  • 方案精读:77页2024 集团企业IT技术架构规划方案【附全文阅读】
  • IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤
  • Video-LLaVA
  • 预热苏杯,“谁羽争锋”全国新闻界羽毛球团体邀请赛厦门开赛
  • 这个器官健康的人,不容易得抑郁症
  • 王毅会见瑞士联邦委员兼外长卡西斯
  • 上海:全面建设重复使用火箭创新高地、低成本商业卫星规模制造高地
  • 目前中美未进行任何经贸谈判,外交部、商务部再次表明中方立场
  • 龙头券商哪家强:中信去年营收领跑,中金净利下滑