13.7-全栈Java笔记:打飞机游戏实战项目|Rectangle|intersects|Plane

本文深入探讨了游戏开发中的碰撞检测技术,特别是矩形碰撞检测原理。通过Java的Rectangle类和intersects()方法实现物体间的碰撞检测。以打飞机游戏为例,解释了如何检测飞机与炮弹的碰撞,以及处理碰撞后的逻辑,帮助读者理解全栈Java工程师所需的技能。

碰撞类检测技术
游戏中,碰撞是遇到最频繁的技术。当然,很多游戏引擎内部已经做了碰撞检测处理,我们只需调用即可。本节课是从碰撞的原理进行讲解,大家自己去实现基本的碰撞检测。

矩形检测原理
游戏中,多个元素是否碰到一起,实际上,通常是用“矩形检测”原理实现的。 我们在前面提到,游戏中所有的物体都可以抽象成“矩形”,我们只需判断两个矩形是否相交即可。对于一些复杂的多边形、不规则物体,实际上是将他分解成多个矩形,继续进行矩形检测。
Java的API中,为我们提供了Rectangle类来表示矩形相关信息,并且提供了intersects()方法,直接判断矩形是否相交。
我们在前面设计GameObject这个基类的时候,增加过一个方法:

           /**
            * 返回物体对应矩形区域,便于后续在碰撞检测中使用
            * @return
            */
           public Rectangle getRect(){
                     return    new Rectangle((int)x,(int) y, width, height);
           }      

也就是说,本游戏中所有物体都能拿到他自己的矩形对象。
炮弹和飞机碰撞检测
我们

### 计算矩形周长和面积 ```c #include <stdio.h> // 定义Point结构体 typedef struct { double x; double y; } Point; // 定义Rectangle结构体 typedef struct { Point topLeft; Point bottomRight; } Rectangle; // 计算矩形面积 double rectangleArea(Rectangle rect) { double length = rect.bottomRight.x - rect.topLeft.x; double width = rect.topLeft.y - rect.bottomRight.y; return length * width; } // 计算矩形周长 double rectanglePerimeter(Rectangle rect) { double length = rect.bottomRight.x - rect.topLeft.x; double width = rect.topLeft.y - rect.bottomRight.y; return 2 * (length + width); } int main() { Rectangle rect; rect.topLeft.x = 0; rect.topLeft.y = 10; rect.bottomRight.x = 5; rect.bottomRight.y = 0; double area = rectangleArea(rect); double perimeter = rectanglePerimeter(rect); printf("矩形面积: %.2f\n", area); printf("矩形周长: %.2f\n", perimeter); return 0; } ``` ### 学生档案管理 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义学生结构体 typedef struct Student { char name[50]; int age; struct Student *next; } Student; // 创建新节点 Student* createNode(char *name, int age) { Student *newNode = (Student*)malloc(sizeof(Student)); strcpy(newNode->name, name); newNode->age = age; newNode->next = NULL; return newNode; } // 插入节点 void insertNode(Student **head, char *name, int age) { Student *newNode = createNode(name, age); if (*head == NULL) { *head = newNode; } else { Student *temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } // 删除节点 void deleteNode(Student **head, char *name) { Student *temp = *head, *prev; if (temp != NULL && strcmp(temp->name, name) == 0) { *head = temp->next; free(temp); return; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); } // 更改节点数据 void updateNode(Student *head, char *name, int newAge) { Student *temp = head; while (temp != NULL) { if (strcmp(temp->name, name) == 0) { temp->age = newAge; break; } temp = temp->next; } } // 搜索指定姓名学生 Student* searchNode(Student *head, char *name) { Student *temp = head; while (temp != NULL) { if (strcmp(temp->name, name) == 0) { return temp; } temp = temp->next; } return NULL; } // 遍历所有学生信息 void traverseList(Student *head) { Student *temp = head; while (temp != NULL) { printf("姓名: %s, 年龄: %d\n", temp->name, temp->age); temp = temp->next; } } int main() { Student *head = NULL; insertNode(&head, "张三", 20); insertNode(&head, "李四", 21); insertNode(&head, "王五", 22); printf("所有学生信息:\n"); traverseList(head); deleteNode(&head, "李四"); printf("\n删除李四后所有学生信息:\n"); traverseList(head); updateNode(head, "张三", 23); printf("\n更新张三年龄后所有学生信息:\n"); traverseList(head); Student *found = searchNode(head, "王五"); if (found != NULL) { printf("\n找到王五,年龄: %d\n", found->age); } else { printf("\n未找到王五\n"); } return 0; } ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值