-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Labels
untriagedRequest triage from a team memberRequest triage from a team member
Description
example:
dotnet new dx2d12 -n apiexample
code:
using System;
using DirectX12;
using DirectX12.Graphics2D;
class PongGame : Game
{
Paddle leftPaddle;
Paddle rightPaddle;
Ball ball;
public override void Initialize()
{
leftPaddle = new Paddle(50, 200, 20, 100);
rightPaddle = new Paddle(730, 200, 20, 100);
ball = new Ball(400, 240, 10, 4, 4);
}
public override void Update(GameTime time)
{
ball.X += ball.DX;
ball.Y += ball.DY;
if (ball.Y < 0 || ball.Y > 480) ball.DY *= -1;
if (ball.Intersects(leftPaddle) || ball.Intersects(rightPaddle))
ball.DX *= -1;
if (Keyboard.IsKeyDown(Keys.W)) leftPaddle.Y -= 5;
if (Keyboard.IsKeyDown(Keys.S)) leftPaddle.Y += 5;
if (Keyboard.IsKeyDown(Keys.Up)) rightPaddle.Y -= 5;
if (Keyboard.IsKeyDown(Keys.Down)) rightPaddle.Y += 5;
}
public override void Draw(Graphics2D g)
{
g.Clear(Color.Black);
g.FillRectangle(Color.White, leftPaddle.Bounds);
g.FillRectangle(Color.White, rightPaddle.Bounds);
g.FillCircle(Color.White, ball.X, ball.Y, ball.Radius);
}
}
class Paddle {
public float X, Y, Width, Height;
public Paddle(float x, float y, float w, float h) { X=x; Y=y; Width=w; Height=h; }
public RectangleF Bounds => new RectangleF(X, Y, Width, Height);
}
class Ball {
public float X, Y, Radius, DX, DY;
public Ball(float x, float y, float r, float dx, float dy) { X=x; Y=y; Radius=r; DX=dx; DY=dy; }
public bool Intersects(Paddle p) =>
X - Radius < p.X + p.Width && X + Radius > p.X &&
Y > p.Y && Y < p.Y + p.Height;
}
Metadata
Metadata
Assignees
Labels
untriagedRequest triage from a team memberRequest triage from a team member