使用C#在openGL中进行的基本3D小行星游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/a-basic-3d-asteroid-game-in-opengl-with-csharp-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1990 个词 阅读量 0使用C#在openGL中进行的基本3D小行星游戏(译文)
原文地址:https://www.codeproject.com/Articles/308167/A-Basic-3D-Asteroid-Game-in-openGL-with-Csharp
原文作者:Vasily Tserekh
译文由本站 robot-v1.0 翻译
前言
A Basic 3D Asteroid Game in openGL with C#
使用C#在openGL中进行的基本3D小行星游戏
In this article, you will learn the basics of what a game should have including a score, levels of difficulty and a life counter.
在本文中,您将学习游戏应具备的基本知识,包括得分,难度等级和寿命计数器.
介绍(Introduction)
本文适用于想开始3D游戏编程并且不知道从哪里开始的初学者.它是使用VS 2008和我自称的小型图形引擎在OpenGL中编程的(This article is intended for beginners who want to start with 3D game programming and don’t know where to start. It is programmed in OpenGL using VS 2008 and a small graphic engine I made myself called) Shadowengine
.它具有游戏应具备的基本知识:得分,难度等级和生命计数器.所有这些都是用少量的代码行编写的,目的是使其简单易懂.(. It has the basics of what a game should have: a Score, levels of difficulty and a life counter. All this is written in a small number of code lines, with the objective of being simple and understandable.)
我必须要解决的第一个问题是使场景看起来像外太空.对于该问题,我将背景色设置为黑色.在OpenGL中,设置方法如下:(The first problem I had to achieve is the scene to look like outer space. For that issue, I set the background color to black. In OpenGL, it is set this way:)
Gl.glClearColor(0, 0, 0, 1);//red green blue alpha
另一个问题是星星,我通过在屏幕上绘制随机的白点来解决.该算法或多或少是这种方式.我生成一个随机点并测量与飞船的距离,如果它小于预定义的数字,我将其丢弃并重复该过程,直到它产生所需的恒星.看一下代码:(The other problem was the stars and I solve it by drawing random white points on the screen. The algorithm is more or less this way. I generate a random point and measure the distance to the spaceship, and if it is less than a predefined number, I discard it and repeat the process until it creates the desired stars. Look at the code:)
public void CreateStars(int cantidad)
{
Random r = new Random();
int count = 0;
while (count != cantidad)
{
Position p = default(Position);
p.x = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
p.z = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
p.y = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
if (Math.Pow(Math.Pow(p.x, 2) + Math.Pow(p.y, 2) +
Math.Pow(p.z, 2), 1 / 3f) > 15)
{
stars.Add(p);
count++;
}
}
}
分数是一个随时间增加的数字,每当我通过一个级别,分数就会增长得更快.等级每450帧增加.(The score is a number which increases over time and it grows faster everytime I pass a level. The level increases every 450 frames.) 我必须解决的最后一个问题是小行星碰撞的问题.我为飞船设置了三个位置(一个为飞船,两个为机翼),不时地检查所有小行星与这三个位置之间的距离.如果它低于预定义的值,则执行碰撞事件.(The last issue I had to address is the problem of asteroid collision. I make for the ship three positions (one for the ship and two for the wings) and every once in a while, I check the distance between all the asteroids and those three positions. If it is under a predefined value, I execute the collision event.) 该项目有六个班级:(The project has six classes:)
- AsteroidGenerator.cs(AsteroidGenerator.cs)-它可以在随机的地方以随机的大小和速度处理小行星的创建.它还具有查询飞船和小行星之间是否发生碰撞的方法.(- It handles the creation of asteroids in random places and with random sizes and speeds. It also has the method to query whether or not there has been a collision between the spaceship and an asteroid.)
- 小行星(Asteroid.cs)-处理所有与小行星有关的问题,例如运动,纹理选择,绘画等.(- It handles all concerning an asteroid such as the movement, texture selection, drawing, among others.)
- Star.cs(Star.cs)-它具有创建随机白点并绘制它们的方法.(- It has a method to create random white points and to draw them.)
- Camera.cs(Camera.cs)-处理用户摄像机的选择并设置场景的正确透视图.(- It handles the selection of the user camera and to set the right perspective view of the scene.)
- 太空船(SpaceShip.cs)-此类包含有关宇宙飞船的所有方法和属性,例如运动,绘图等.(- This class contains all methods and attributes regarding the spaceship, such as movement, drawing, etc.)
- Controller.cs(Controller.cs)-此类管理游戏的创建,游戏逻辑,并包含运行游戏所需的所有类.这是一部分代码:(- This class manages the game creation, game logic and contains all the classes needed to run the game. Here is a portion of code:)
using System;
using System.Collections.Generic;
using System.Text;
namespace Naves
{
public class Controller
{
Camera camara = new Camera();
Star star = new Star();
SpaceShip spaceShip = new SpaceShip();
public SpaceShip Nave
{
get { return spaceShip; }set { spaceShip = value; }
}
public Camera Camara
{
get { return camara; }
}
public void BeginGame()
{
AsteroidGenerator.GenerateAsteroid(35, false);
}
public void ResetGame()
{
AsteroidGenerator.GenerateAsteroid(35, true);
spaceShip.Reiniciar();
}
public void CreateObjects()
{
star.CreateStars(450);
spaceShip.Create();
Asteroid.Crear();
}
public void DrawScene()
{
star.Draw();
AsteroidGenerator.DrawAsteroids();
spaceShip.Dibujar();
}
}
}
Main.cs(Main.cs)-这是包含游戏视觉组件的形式.它包含(- This is the form which contains this visual components of the game. It contains the) controller
类,并通过适当的Windows控件为播放器提供信息.它具有用于定期绘制场景的计时器,并具有用于纹理和对象加载的代码.(class and gives the player information through proper windows controls. It has a timer to periodically draw the scene and has the code for texture and object loading.)
如果要向项目添加声音,请取消注释注释行,然后按Ctrl + Alt + E,并在托管调试助手下,取消选中loaderlock异常.(If you want to add sound to the project, uncomment the commented lines and press Ctrl+Alt+E and under managed debugging assistants, uncheck the loaderlock exception.)
我希望收到来自此示例的反馈.如果您喜欢,请在下面投票或发表评论.我将尝试制作一个太空飞船可以在小行星上射击的版本.(I am hoping to receive feedback from this example. If you like it, please vote or leave a comment below. I’ll try to make a version where the spaceship can shoot at the asteroids.)
历史(History)
- 28(28)日(th)2011年12月:初始版本(December, 2011: Initial version)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# OpenGL VS2008 新闻 翻译