由于篇幅限制,我将分别为你提供Python、JavaScript(HTML5 Canvas)和C#(Unity的简单控制台输出)三种语言的简单游戏代码示例。
由于你的问题涉及多种编程语言,我将为你提供每种语言的简要示例。
- Python示例:
import math
def calculate_area(radius):
return math.pi * (radius ** 2)
def calculate_circumference(radius):
return 2 * math.pi * radius
radius = 5
area = calculate_area(radius)
circumference = calculate_circumference(radius)
print(f"Area: {area}")
print(f"Circumference: {circumference}")
- JavaScript(HTML5 Canvas)示例:
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var radius = 25;
var centerX = 50;
var centerY = 50;
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'green';
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = '#003300';
ctx.stroke();
</script>
- C#(Unity)示例:
using UnityEngine;
public class CircleDrawer : MonoBehaviour
{
public float radius = 5f;
public Vector3 center = Vector3.zero;
void OnGUI()
{
GUILayout.Label("Area: " + (Mathf.PI * radius * radius));
GUILayout.Label("Circumference: " + (2 * Mathf.PI * radius));
}
void OnRenderObject()
{
Graphics.DrawMeshNow(GenerateCircleMesh(radius, 50), Matrix4x4.TRS(center, Quaternion.identity, Vector3.one));
}
Mesh GenerateCircleMesh(float radius, int segments)
{
Mesh mesh = new Mesh();
float angleStep = 360f / segments;
Vector3[] vertices = new Vector3[segments + 1];
int[] triangles = new int[segments * 3];
for (int i = 0; i <= segments; i++)
{
float angle = angleStep * i;
vertices[i] = new Vector3(Mathf.Cos(angle * Mathf.Deg2Rad), 0, Mathf.Sin(angle * Mathf.Deg2Rad)) * radius;
}
for (int i = 0; i < segments; i++)
{
triangles[i * 3] = i;
triangles[i * 3 + 1] = (i + 1) % (segments + 1);
triangles[i * 3 + 2] = (i + 2) % (segments + 1);
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
return mesh;
}
}
这些示例提供了圆的面积和周长的计算,并在各自的环境中绘制了一个简单的圆形。Python示例使用了math库来计算圆的面积和周长,而HTML5 Canvas和Unity示例则是通过绘制几何体来展示圆形。
评论已关闭