0% found this document useful (0 votes)
95 views22 pages

Game Programming Practicals

The document contains details from four practical assignments completed by Nikhil Deepak Shivane for his Game Programming course. Practical 1 involves creating a device using DirectX to render an orange screen. Practical 2 draws a colored triangle using Direct3D 11. Practical 3 textures the triangle by loading an image file. Practical 4 implements programmable diffuse lighting. For each practical, Nikhil provides the code and outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views22 pages

Game Programming Practicals

The document contains details from four practical assignments completed by Nikhil Deepak Shivane for his Game Programming course. Practical 1 involves creating a device using DirectX to render an orange screen. Practical 2 draws a colored triangle using Direct3D 11. Practical 3 textures the triangle by loading an image file. Practical 4 implements programmable diffuse lighting. For each practical, Nikhil provides the code and outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Satish Pradhan Dnyanasadhana College,Thane[A.Y.

2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: - 07/07/22

Practical 1: - To create a Device using DirectX.

Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;

namespace Practical_1
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
public Form1()
{
InitializeComponent();
InitDevice();
}
public void InitDevice()
{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.HardwareVertexProcessing, pp);
}
public void Render()
{
1 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

device.Clear(ClearFlags.Target, Color.Orange, 0, 1);


device.Present();
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
Render();
}
}
}

O/P: -

2 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -21/07/22
Practical 2: - Draw triangle using Direct3D 11
Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Windows.Forms;

namespace Practical2
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
public Form1()
{
InitializeComponent();
InitDevice();
}

private void InitDevice()


{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware,
this,CreateFlags.HardwareVertexProcessing, pp);
}
public void Render()
{

3 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

CustomVertex.TransformedColored[] vertex = new


CustomVertex.TransformedColored[3];
vertex[0].Position = new Vector4(240, 110, 0, 1.0f);
vertex[0].Color = System.Drawing.Color.FromArgb(0, 255, 0).ToArgb();
vertex[1].Position = new Vector4(380, 420, 0, 1.0f);
vertex[1].Color = System.Drawing.Color.FromArgb(0, 0, 255).ToArgb();
vertex[2].Position = new Vector4(110, 420, 0, 1.0f);
device.Clear(ClearFlags.Target, Color.Purple, 0, 1);
device.BeginScene();
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertex);
device.EndScene();
device.Present();
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
Render();
}
}
}

O/P: -

4 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -28/07/22

Practical 3: - Texturing (Texture the triangle using Direct 3D 11)


Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Windows.Forms;

namespace Practical_3
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionTextured[] vertex = new
CustomVertex.PositionTextured[3];
private Texture texture;
public Form1()
{
InitializeComponent();
InitDevice();
}

private void InitDevice()


{
PresentParameters pp = new PresentParameters();

5 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.HardwareVertexProcessing, pp);
device.Transform.Projection = Matrix.PerspectiveFovLH(3.14f / 4,
device.Viewport.Width / device.Viewport.Height, 1f, 1000);

device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 20), new


Vector3(), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
vertex[0] = new CustomVertex.PositionTextured(new Vector3(0, 0, 0), 0,
0);
vertex[1] = new CustomVertex.PositionTextured(new Vector3(5, 0, 0), 0,
1);
vertex[2] = new CustomVertex.PositionTextured(new Vector3(0, 5, 0), -
1, 1);
texture = new Texture(device, new
Bitmap("C:\\Users\\Admin\\Downloads\\Wallpapers\\4.jpg"), 0,
Pool.Managed);
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
device.Clear(ClearFlags.Target, Color.Brown,
0, 1); device.BeginScene();
device.SetTexture(0, texture);
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3,
vertex);
device.EndScene();
device.Present();
}
}
}
O/P: -

6 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

7 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -25/08/22

Practical 4: - Lightning (Programmable Diffuse Lightning using


Direct3D 11)
Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using
System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Practical_4
{
public partial class Form1 : Form
{
private Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionNormalColored[] vertex = new
CustomVertex.PositionNormalColored[3];
public Form1()
{
InitializeComponent();
InitDevice();
}

private void InitDevice()


{
PresentParameters pp = new
PresentParameters(); pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware,
8 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

this, CreateFlags.HardwareVertexProcessing, pp);


device.Transform.Projection = Matrix.PerspectiveFovLH(3.14f / 4,
device.Viewport.Width / device.Viewport.Height, 1f, 1000f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 10), new
Vector3(), new Vector3(0, 1, 0));

device.RenderState.Lighting = true;

vertex[0] = new CustomVertex.PositionNormalColored(new


Vector3(0, 1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
vertex[1] = new CustomVertex.PositionNormalColored(new Vector3(-1,
- 1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
vertex[2] = new CustomVertex.PositionNormalColored(new Vector3(1, -
1, 1), new Vector3(-1, 0, 1), Color.Red.ToArgb());
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.Plum;
device.Lights[0].Direction = new Vector3(0.8f, 0, -
1); device.Lights[0].Enabled = true;
}
public void Render()
{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionNormalColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3,
vertex);
device.EndScene();

device.Present();
}

private void Form1_Paint(object sender, PaintEventArgs e)


{
Render();
}
}

9 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

O/P: -

10 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -25/08/22
Practical 5: - Specular Lightning (Programmable Spot Lightning
using Direct3D 11)
Code: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using
System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Practical_5
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
Microsoft.DirectX.Direct3D.Texture
texture; Microsoft.DirectX.Direct3D.Font
font; public Form1()
{
InitializeComponent(
); InitDevice();
InitFont();
LoadTexture();
}

private void LoadTexture()


{
texture = TextureLoader.FromFile(device, "C:\\Users\\Admin\\
Downloads\\Wallpapers\\1.jpg", 400, 400, 1, 0, Format.A8B8G8R8,
Pool.Managed, Filter.Point, Filter.Point, Color.Transparent.ToArgb());

11 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

}
private void InitFont()
{

System.Drawing.Font f = new System.Drawing.Font("Arial", 16f,


FontStyle.Regular);
font = new Microsoft.DirectX.Direct3D.Font(device, f);
}

private void InitDevice()


{
PresentParameters pp = new
PresentParameters(); pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware,
this, CreateFlags.HardwareVertexProcessing, pp);
}

private void Render()


{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 0, 1);
device.BeginScene();
using (Sprite s = new Sprite(device))
{
s.Begin(SpriteFlags.AlphaBlend);
s.Draw2D(texture, new Rectangle(0, 0, 0, 0), new Rectangle(0,
0, device.Viewport.Width, device.Viewport.Height), new Point(0, 0), 0f,
new Point(0, 0), Color.White);
font.DrawText(s, "Dnyanasadhana college", new Point(0, 0),
Color.Black);
s.End();
}
device.EndScene();
device.Present();
}

private void Form1_Paint(object sender, PaintEventArgs e)

12 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

{
Render();
}
}
}
O/P: -

13 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -08/09/22

Practical 6: - Roll a Ball game in Unity


Code: -
PlayerController.cs:
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{ Rigidbody2D rigidbody;
int coins = 0;
public Text coinText;
void Start() {
rigidbody = GetComponent<Rigidbody2D>();
}
void Update() {
if (Input.GetKey(KeyCode.UpArrow))
{ rigidbody.AddForce(Vector2.up);
}
if (Input.GetKey(KeyCode.DownArrow))
{ rigidbody.AddForce(Vector2.down);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddForce(Vector2.left);
}
if (Input.GetKey(KeyCode.RightArrow)) { rigidbody.AddForce(Vector2.right);

14 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

}
}
private void OnTriggerEnter2D(Collider2D collision) {
collision.gameObject.SetActive(false);
coins++;
coinText.text = "Coins:" + coins.ToString();
}
}

O/P: -

15 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -15/09/22
Practical 7: - Coin collator game in Unity
Code: -
CoinController.cs:
using UnityEngine;
public class CoinController : MonoBehaviour
{ private void Update() {
transform.Rotate(new Vector3(0, 20, 0));
}
private void OnTriggerEnter(Collider other)
{ gameObject.SetActive(false);
}
}

SphereController.cs:
using UnityEngine;
public class SphereController : MonoBehaviour
{ Rigidbody rigidbody;
public float speed = 150;
void Start() {
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate() {

16 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

if (Input.GetKey(KeyCode.UpArrow)) {
rigidbody.AddForce(Vector3.forward *
Time.fixedDeltaTime * speed);

}
if (Input.GetKey(KeyCode.DownArrow))
{ rigidbody.AddForce(Vector3.back *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddForce(Vector3.left *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.RightArrow)) {
rigidbody.AddForce(Vector3.right *
Time.fixedDeltaTime * speed);
}
}
}

17 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

O/P: -

18 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

Date: -22/09/22
Practical 8: - Break the wall game in Unity
Code: -
RotateCube.cs:
using UnityEngine;
public class CoinController : MonoBehaviour
{ private void Update() {
transform.Rotate(new Vector3(0, 20, 0));
}
}

SphereController.cs:
using UnityEngine;
public class SphereController : MonoBehaviour
{ Rigidbody rigidbody;
public float speed = 150;
void Start() {
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate() {
if (Input.GetKey(KeyCode.UpArrow)) {
rigidbody.AddForce(Vector3.forward *
Time.fixedDeltaTime * speed);
}

19 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

if (Input.GetKey(KeyCode.DownArrow)) {
rigidbody.AddForce(Vector3.back *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddForce(Vector3.left *
Time.fixedDeltaTime * speed);
}
if (Input.GetKey(KeyCode.RightArrow)) {
rigidbody.AddForce(Vector3.right *
Time.fixedDeltaTime * speed);
}
}
}

O/P: -

20 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

21 SIGNATURE____________________
Satish Pradhan Dnyanasadhana College,Thane[A.Y. 2022-2023]

Name: Nikhil Deepak Shivane Roll No: 50 GI NO:13380


Program: T.Y.B.Sc. CS Subject: Game Programming

22 SIGNATURE____________________

You might also like