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

news/2025/5/22 2:27:30/

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()


http://www.ppmy.cn/news/1622682.html

相关文章

中国250米土壤PH(H2O)值数据

土壤PH值&#xff0c;又称为土壤酸碱度、“土壤反应”&#xff0c;是土壤酸度和碱度的总称。通常用以衡量土壤酸碱反应的强弱。它是土壤溶液的酸碱反应。主要取决于土壤溶液中氢离子的浓度&#xff0c;以pH值表示。pH值等于7的溶液为中性溶液&#xff1b;pH值小于7&#xff0c;…

时间序列预测模型比较分析:SARIMAX、RNN、LSTM、Prophet 及 Transformer

时间序列预测根据过去的模式预测未来事件。我们的目标是找出最佳预测方法&#xff0c;因为不同的技术在特定条件下表现出色。本文章将探讨各种方法在不同数据集上的表现&#xff0c;为你在任何情况下选择和微调正确的预测方法提供真知灼见。 我们将探讨五种主要方法&#xff1…

大模型微调 - transformer架构

什么是Transformer Transformer 架构是由 Vaswani 等人在 2017 年提出的一种深度学习模型架构&#xff0c;首次发表于论文《Attention is All You Need》中 Transformer 的结构 Transformer 编码器&#xff08;Encoder&#xff09; 解码器&#xff08;Decoder&#xff09; …

(CAS:130100-20-8)Mag-Fura-2 AM Cell Permeant,配制方法步骤

一、试剂描述 Mag-Fura-2 AM是一种胞内镁离子指示剂&#xff0c;属于紫外激发的比率型探针&#xff0c;与镁离子结合的Kd值为1.9mM。 与Fura-2类似&#xff0c;Mag-Fura-2的激发波长历经蓝色迁移从3 69nm到330nm。Mag-Fura-2 AM具细胞膜渗透性&#xff0c;只需简单孵育&#…

中国250米土壤质地类型数据

土壤质地指土壤中砂粒、粉粒和黏粒的相对含量和组成。根据土壤质地的不同&#xff0c;可以将土壤分为砂土、壤土、黏土等类型。土壤质地对土壤的物理性质&#xff08;如渗透性、保水性&#xff09;和化学性质&#xff08;如养分含量&#xff09;有重要影响。 本数据集是以250米…

Nginx解决跨域问题

Nginx解决跨域问题详解 #mermaid-svg-KbGKpZziw2l5DJ0R {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-KbGKpZziw2l5DJ0R .error-icon{fill:#552222;}#mermaid-svg-KbGKpZziw2l5DJ0R .error-text{fill:#552222;stro…

ComfyUI 简介

目录 &#x1f19a; 与其他文生图工具对比 &#x1f4a1; ComfyUI 的优势与不足 ✅ 优势 ❌ 不足 &#x1f4b0; 部署成本分析 &#x1f9e9; 竞品分析 1. AUTOMATIC1111 WebUI 2. InvokeAI 3. DiffusionBee 4. Midjourney &#x1f4ca; 总结对比图 开源协议&#…

CS001-7-hbao

HBAO https://zhuanlan.zhihu.com/p/348467142 HBAO(屏幕空间的环境光遮蔽) - 知乎 (zhihu.com) [摸着原神学图形]HBAO实现与优化 - 知乎 (zhihu.com) https://zhuanlan.zhihu.com/p/367793439 Global Illumination_Horizon-Based Ambient Occlusion(HBAO)-CSDN博客 这个解…