GP Practical
GP Practical
CERTIFICATE
Place :
Date :
PRACTICAL 1
In this practical we are just learning the window framework and initializing a Direct3D device.
Step 1:
I. Create new project, and select “Windows Forms Application”, select .NET
Framework as 2.0 in Visuals C#.
II. Right Click on properties Click on open click on build Select Platform Target
and Select x86.
Step 2:
Click on View Code of Form 1.
Step 3:
Go to Solution Explorer, right click on project name, and select Add Reference. Click
on Browse and select the given .dll files which are “Microsoft.DirectX”,
“Microsoft.DirectX.Direct3D”, and “Microsoft.DirectX.DirectX3DX”.
Step 4:
Go to Properties Section of Form, select Paint in the Event List and enter as
Form1_Paint.
Step 5:
Edit the Form’s C# code file. Namespace must be as same as your project name.
Program:
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
[USCSP5042]
1
Game ProgrammingTYBSc. CS.
usingMicrosoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace GP_P1
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
public Form1()
{
InitializeComponent();
InitDevice();
}
Step 6: Click on Start. And here is the output. We have initialized 3D Device.
Output:
[USCSP5042]
2
Game ProgrammingTYBSc. CS.
Step 1:
.Create new project, and select “Windows Forms Application”, select .NET
i. Right Click on properties Click on open click on build Select Platform Target and
Select x86.
Step 2:
Step 3:
Go to Solution Explorer, right click on project name, and select Add Reference. Click
on Browse and select the given .dll files which are “Microsoft.DirectX”,
Step 4:
Go to Properties Section of Form, select Paint in the Event List and enter as
Form1_Paint.
[USCSP5042]
3
Game ProgrammingTYBSc. CS.
Step 5:
Edit the Form’s C# code file. Namespace must be as same as your project name.
Program:
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace p1_gp
{
public partial class Form3 : Form
{
CustomVertex.PositionColored[] vertexes = new
CustomVertex.PositionColored[6];
Microsoft.DirectX.Direct3D.Device device;
public Form3()
{
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);
}
[USCSP5042]
4
Game ProgrammingTYBSc. CS.
}
private void Form3_Paint(object sender, PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.WhiteSmoke, 1.0f, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList,
vertexes.Length / 3, vertexes);
device.EndScene();
device.Present();
}
}
[USCSP5042]
5
Game ProgrammingTYBSc. CS.
Output:
In this practical we are just learning to Draw a Rectangle and initializing a Direct3D device.
Step 1:
. Create new project, and select “Windows Forms Application”, select .NET
Right Click on properties Click on open click on build Select Platform Target and
Select x86.
Step 2:
[USCSP5042]
6
Game ProgrammingTYBSc. CS.
Step 3:
Go to Solution Explorer, right click on project name, and select Add Reference. Click
on Browse and select the given .dll files which are “Microsoft.DirectX”,
Step 4:
Go to Properties Section of Form, select Paint in the Event List and enter as
Form1_Paint.
Step 5:
Edit the Form’s C# code file. Namespace must be as same as your project name.
Program:
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 Practical2b
{
public partial class Form1 : Form
{
Microsoft.DirectX.Direct3D.Device device;
CustomVertex.PositionColored[] vertexes = new
CustomVertex.PositionColored[6];
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
[USCSP5042]
7
Game ProgrammingTYBSc. CS.
{
device.Clear(ClearFlags.Target, Color.PaleVioletRed, 1.0f,
0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList,
vertexes.Length / 3,
vertexes);
device.EndScene();
device.Present();
}
}
}
}
[USCSP5042]
8
Game ProgrammingTYBSc. CS.
Output:
[USCSP5042]
9
Game ProgrammingTYBSc. CS.
on Browse and select the given .dll files which are “Microsoft.DirectX”,
“Microsoft.DirectX.Direct3D”, and “Microsoft.DirectX.DirectX3DX”.
Step 4:
Go to Properties Section of Form, select Paint in the Event List and enter as
Form1_Paint.
Step 5:
Edit the Form’s C# code file. Namespace must be as same as your project name.
Program:
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace p1_gp
{
public partial class Form4 : Form
{
private Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionTextured[] vertex = new
CustomVertex.PositionTextured[3];
private Texture texture;
public Form4()
{
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);
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,
20), new Vector3(), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
vertex[0] = new CustomVertex.PositionTextured(new Vector3(-5,
-6, 0), 0, 0);
vertex[1] = new CustomVertex.PositionTextured(new Vector3(5,
-6, 0), 0, 1);
[USCSP5042]
10
Game ProgrammingTYBSc. CS.
}
}
Output:
[USCSP5042]
11
Game ProgrammingTYBSc. CS.
In this practical we are just learning the window framework and initializing a Direct3D
device.
Step 1:
Create new project, and select “Windows Forms Application”, select .NET
Right Click on properties Click on open click on build Select Platform Target and
Select x86.
Step 3:
Go to Solution Explorer, right click on project name, and select Add Reference. Click
on Browse and select the given .dll files which are “Microsoft.DirectX”,
Step 4:
Go to Properties Section of Form, select Paint in the Event List and enter as
Form1_Paint.
Step 5:
[USCSP5042]
12
Game ProgrammingTYBSc. CS.
Edit the Form’s C# code file. Namespace must be as same as your project name.
Program:
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 WindowsFormsApp10
{
public partial class Form1 : Form
{
private Device device;
private CustomVertex.PositionColored[] vertex = new
CustomVertex.PositionColored[6];
private Texture texture;
public Form1()
{
InitializeComponent();
}
[USCSP5042]
13
Game ProgrammingTYBSc. CS.
Output:-
[USCSP5042]
14
Game ProgrammingTYBSc. CS.
PRACTICAL 2
To create a window using Python and Pygame, you need to follow these steps:
Install Pygame: If you haven't installed Pygame yet, you can do it using pip:
Import Pygame: At the beginning of your Python script, import the Pygame module.
[USCSP5042]
15
Game ProgrammingTYBSc. CS.
import pygame
pygame.init()
Set up the window: Define the width and height of the window, and create it using the
pygame.display.set_mode() function.
Main loop: To keep the window open and responsive, create a main loop where you can
handle events and update the display.
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
Quit Pygame: After exiting the main loop, remember to quit Pygame properly.
pygame.quit()
Source code
[USCSP5042]
16
Game ProgrammingTYBSc. CS.
import pygame
pygame.init()
width, height = 800, 600
window = pygame.display.set_mode((width, height))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Output:
[USCSP5042]
17
Game ProgrammingTYBSc. CS.
# Screen settings
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Basic Movements with Pygame")
2. create a basic game loop that will handle user input and update the screen:
# Character settings
character_width, character_height = 50, 50
character_x, character_y = (WIDTH - character_width) // 2, (HEIGHT -
character_height) // 2
character_speed = 5
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
[USCSP5042]
18
Game ProgrammingTYBSc. CS.
if keys[pygame.K_LEFT]:
character_x -= character_speed
if keys[pygame.K_RIGHT]:
character_x += character_speed
if keys[pygame.K_UP]:
character_y -= character_speed
if keys[pygame.K_DOWN]:
character_y += character_speed
clock.tick(60)
3. Quit Pygame: After exiting the main loop, remember to quit Pygame properly.
pygame.quit()
import pygame
import sys
pygame.init()
# Screen settings
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Basic Movements with Pygame")
# Character settings
[USCSP5042]
19
Game ProgrammingTYBSc. CS.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
character_x -= character_speed
if keys[pygame.K_RIGHT]:
character_x += character_speed
if keys[pygame.K_UP]:
character_y -= character_speed
if keys[pygame.K_DOWN]:
character_y += character_speed
clock.tick(60)
pygame.quit()
sys.exit()
Output:
[USCSP5042]
20
Game ProgrammingTYBSc. CS.
To display text using pygame, you'll first need to install pygame and then use its functions to
create and render text on the screen.
Follow the given steps to display text on screen.
[USCSP5042]
21
Game ProgrammingTYBSc. CS.
import pygame
import sys
# Initialize Pygame
pygame.init()
Output:
[USCSP5042]
22
Game ProgrammingTYBSc. CS.
The code will create a Pygame window and display the text "Hello, Pygame!" at coordinates
(300, 250) with a white color on a black background. You can customize the text content,
font, size, and position to suit your needs
[USCSP5042]
23
Game ProgrammingTYBSc. CS.
import pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Image Display")
image_path = "c:\\" # Replace with the actual image path
image = pygame.image.load(image_path)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Quit Pygame
pygame.quit()
[USCSP5042]
24
Game ProgrammingTYBSc. CS.
Output:
[USCSP5042]
25
Game ProgrammingTYBSc. CS.
To use the mixer and music modules in Pygame, you need to import them like
this:
import pygame
from pygame import mixer
Step 2:
To initialize Pygame and the mixer module, call the following before using any
audio functionality:
pygame.init()
mixer.init()
Step 3:
mixer.Sound("soundeffect.wav").play()
Step 4:
pygame.mixer.music.load("backgroundmusic.mp3")
Step 5:
handle events and the main game loop to keep the audio playing and
responsive
Step 6:
pygame.mixer.music.stop()
[USCSP5042]
26
Game ProgrammingTYBSc. CS.
Note:
import pygame
pygame.init()
mixer.init()
pygame.mixer.music.load("DireDireDocks.mp3")
[USCSP5042]
27
Game ProgrammingTYBSc. CS.
sound_effect = mixer.Sound("marioEffect.wav")
running = True
while running:
if event.type == pygame.QUIT:
running = False
if event.key == pygame.K_SPACE:
sound_effect.play()
pygame.display.flip()
pygame.mixer.music.stop()
pygame.quit()
Output:
[USCSP5042]
28
Game ProgrammingTYBSc. CS.
F) Animation
import pygame
pygame.init()
width = 800
height = 600
pygame.display.set_caption("My Animation")
clock = pygame.time.Clock()
running = True
ball_radius = 20
ball_color = (255, 0, 0)
ball_x = width // 2
ball_y = height // 2
running = True
while running:
if event.type == pygame.QUIT:
running = False
ball_x += ball_velocity[0]
[USCSP5042]
29
Game ProgrammingTYBSc. CS.
ball_y += ball_velocity[1]
ball_velocity[0] = -ball_velocity[0]
# the radius and greater thn the screen width means ball has hit left
ball_velocity[1] = -ball_velocity[1]
#the radius and greater thn the screen height means ball has hit top
screen.fill((0, 0, 0))
pygame.display.flip()
pygame.time.delay(30)
pygame.quit()
[USCSP5042]
30
Game ProgrammingTYBSc. CS.
Output:
[USCSP5042]
31
Game ProgrammingTYBSc. CS.
PRACTICAL 3
Program:
import pygame
import time
snake_speed = 10 #snake moves with this speed for making the game
difficult increase this and play
[USCSP5042]
32
Game ProgrammingTYBSc. CS.
rect = Font.get_rect()
Font = font.render(
rect = Font.get_rect()
screen.blit(Font, rect)
pygame.display.flip()
pygame.quit()
quit()
[USCSP5042]
33
Game ProgrammingTYBSc. CS.
while True: #the game loop covers actual movement handling, the game over
conditions, the figure and score functions
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
next_dir = 'UP'
if event.key == pygame.K_DOWN:
next_dir = 'DOWN'
if event.key == pygame.K_LEFT:
next_dir = 'LEFT'
if event.key == pygame.K_RIGHT:
next_dir = 'RIGHT'
dir = 'UP'
dir = 'DOWN'
dir = 'LEFT'
dir = 'RIGHT'
if dir == 'UP':
snake_pos[1] -= 10
if dir == 'DOWN':
[USCSP5042]
34
Game ProgrammingTYBSc. CS.
snake_pos[1] += 10
if dir == 'LEFT':
snake_pos[0] -= 10
if dir == 'RIGHT':
snake_pos[0] += 10
score += 10
food = False
else:#if snake doesnot meet the food then pop the coordinates
snake_body.pop()
food = True
screen.fill('black')
[USCSP5042]
35
Game ProgrammingTYBSc. CS.
game_over()
game_over()
game_over()
pygame.display.update()
clock.tick(snake_speed)
Output:
[USCSP5042]
36
Game ProgrammingTYBSc. CS.
PRACTICAL 4
Program:
import pygame
import random
# Screen
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Shooting Game")
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
# Character
character_size = 50
character_speed = 5
character = pygame.Rect(width // 2 - character_size // 2, height -
character_size, character_size, character_size)
[USCSP5042]
37
Game ProgrammingTYBSc. CS.
[USCSP5042]
38
Game ProgrammingTYBSc. CS.
Output:
[USCSP5042]
39
Game ProgrammingTYBSc. CS.
PRACTICAL 5
Program:
import pygame
[USCSP5042]
40
Game ProgrammingTYBSc. CS.
import math
pygame.init()
clock = pygame.time.Clock()
FPS = 60
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 600
#create game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Endless Scroll")
#load image
bg = pygame.image.load("greenHill.jpg").convert()
bg_width = bg.get_width()
bg_rect = bg.get_rect()
#define game variables
scroll = 0
tiles = math.ceil(SCREEN_WIDTH / bg_width) + 1
#game loop
run = True
while run:
clock.tick(FPS)
#draw scrolling background
for i in range(0, tiles):
screen.blit(bg, (i * bg_width + scroll, 0))
bg_rect.x = i * bg_width + scroll
pygame.draw.rect(screen, (255, 0, 0), bg_rect, 1)
#scroll background
scroll -= 5
#reset scroll
if abs(scroll) > bg_width:
scroll = 0
#event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
Output:
[USCSP5042]
41
Game ProgrammingTYBSc. CS.
PRACTICAL 6
[USCSP5042]
42
Game ProgrammingTYBSc. CS.
Following are the steps to create a Camera Shake Effect in Unity: STEP 1 :
Start Unity
[USCSP5042]
43
Game ProgrammingTYBSc. CS.
[USCSP5042]
44
Game ProgrammingTYBSc. CS.
STEP 6 : Drag the assets to the Hierarchy > Click and hold asset and drag under
MainCamera SImilarly, Drag and add the UFO under the background
[USCSP5042]
45
Game ProgrammingTYBSc. CS.
STEP 8 : Select the UFO from Hierarchy. To make the UFO layer appear above the
Background, Add a sorting layer, and select the layer. Update the following settings
for UFO. Add a circle collider and adjust the radius. Adjust gravity Scale
[USCSP5042]
46
Game ProgrammingTYBSc. CS.
STEP 9 : Select the Background from Hierarchy. Add a box collider from Add
component > Box Collider 2D. Do this step 4 times for each border of the box
collider.
Also, set the Shake Frequency which set the intensity of the Shake Effect
STEP 10 : To implement the camera shake effect, add a new script as follows:
[USCSP5042]
47
Game ProgrammingTYBSc. CS.
On clicking on the new script, VS code Editor will open. Add the following code :
void Start()
{
_orignalPosOfCam = cameraTransform.position;
}
void Update()
{
if (Input.GetKey(KeyCode.S))
{
CameraShake();
}
else if (Input.GetKeyUp(KeyCode.S))
{
StopShake();
}
}
[USCSP5042]
48
Game ProgrammingTYBSc. CS.
The final output will result in the Camera Shake Effect on pressing the key ‘S’:
[USCSP5042]
49
Game ProgrammingTYBSc. CS.
PRACTICAL 7
CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animate : MonoBehaviour
{
public float speed;
Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
[USCSP5042]
50
Game ProgrammingTYBSc. CS.
STEP 3 : Adjust the following : Set animate Speed = 1500 and make the
Rigidbody2D > Gravity Scale = 0.
Run the code and the UFO should move with the Arrow Keys.
[USCSP5042]
51
Game ProgrammingTYBSc. CS.
PRACTICAL 8
STEP 2 : To add Particle effect, right-click on Effects under Hierarchy and Select >
Particle System
[USCSP5042]
52
Game ProgrammingTYBSc. CS.
STEP 3 : Scroll down in Inspector to the Shape and Change it to BOX from Cone
[USCSP5042]
53
Game ProgrammingTYBSc. CS.
STEP 4 : Adjust the ‘X” Scale under Shape to adjust the width of the effect.
[USCSP5042]
54
Game ProgrammingTYBSc. CS.
STEP 5 : Adjust ‘Y’ scale under Transform to move the particle upwards
STEP 6 : Now, rotate the particle using the rotate tool present on the scene
screen (third from top)
The final outcome would be like this :
[USCSP5042]
55