GP Journal
GP Journal
ROLL NO: 18
PRACTICAL: 1
AIM: - Setup DirectX 11, Window Framework and Initialize Direct3D Device,
Loading models into DirectX 11 and rendering
CODE: -
Step 1: Download the DirectX SDK
1. Visit the Microsoft DirectX SDK Download Page:
o Go to the DirectX SDK (June 2010) Download.
2. Click the "Download" button and select the DXSDK_Jun10.exe file.
1
TY BSC CS
ROLL NO: 18
o In Visual Studio, if the Solution Explorer pane is not visible, go to
View > Solution Explorer.
2. Right-click on your project:
o Right-click on the project (not the solution) in the Solution Explorer
and select Properties.
3. Set the platform target:
o In the Properties window, click on the Build tab.
o Under Platform target, set the dropdown to x86 (or x64 if you are
targeting 64-bit systems).
4. Save the changes:
o Press Ctrl + S to save the changes.
2
TY BSC CS
ROLL NO: 18
o If the Properties window is not visible, press F4 or go to View >
Properties Window in the menu.
Now, write the code to render graphics in DirectX. The following code sets up a
Direct3D device, clears the screen, and prepares the application for rendering
graphics in a Windows Forms environment:
3
TY BSC CS
ROLL NO: 18
OUTPUT: -
4
TY BSC CS
ROLL NO: 18
PRACTICAL: 2
CODE:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Move Rectangle with Arrow Keys")
# Set up rectangle properties
rect_width, rect_height = 50, 50
rect_x, rect_y = width // 2, height // 2
rect_speed = 5
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Get keys pressed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
rect_x -= rect_speed
if keys[pygame.K_RIGHT]:
rect_x += rect_speed
if keys[pygame.K_UP]:
rect_y -= rect_speed
if keys[pygame.K_DOWN]:
rect_y += rect_speed
# Ensure the rectangle stays within the window boundaries
if rect_x < 0:
rect_x = 0
if rect_x > width - rect_width:
rect_x = width - rect_width
if rect_y < 0:
rect_y = 0
if rect_y > height - rect_height:
rect_y = height - rect_height
5
TY BSC CS
ROLL NO: 18
# Fill screen with black
screen.fill(black)
# Draw the rectangle
pygame.draw.rect(screen, white, (rect_x, rect_y, rect_width, rect_height))
# Update the display
pygame.display.flip()
# Control the frame rate
pygame.time.Clock().tick(60)
OUTPUT: -
6
TY BSC CS
ROLL NO: 18
PRACTICAL: 3
CODE: -
import pygame
import random
# Initialize Pygame
pygame.init()
# Display settings
width = 600
height = 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# Clock and speed
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
# Draw the snake
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, black, [x[0], x[1], snake_block, snake_block])
def gameLoop():
game_over = False
game_close = False
# Game variables initialization
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
# Food initialization
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
screen.fill(white)
font_style = pygame.font.SysFont(None, 50)
7
TY BSC CS
ROLL NO: 18
OUTPUT: -
9
TY BSC CS
ROLL NO: 18
PRACTICAL: 4
CODE: -
import pygame
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
ENEMY_COLOR = (255, 0, 0)
PLAYER_COLOR = (0, 255, 0)
BULLET_COLOR = (0, 0, 255)
# Set up the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Invaders")
# Game variables
player_pos = [WIDTH // 2, HEIGHT - 50]
player_speed = 5
enemies = []
bullets = []
score = 0
level = 1
enemy_count = 10
game_over = False
enemy_speed = 1
# Font
font = pygame.font.SysFont(None, 35)
# Functions
def spawn_enemies(count):
for _ in range(count):
x_pos = random.randint(0, WIDTH - 50)
y_pos = random.randint(-150, -50)
enemies.append([x_pos, y_pos])
def draw_player():
pygame.draw.rect(screen, PLAYER_COLOR, (player_pos[0], player_pos[1],
50, 30))
def draw_enemies():
for enemy in enemies:
10
TY BSC CS
ROLL NO: 18
pygame.draw.rect(screen, ENEMY_COLOR, (enemy[0], enemy[1], 50,
30))
def draw_bullets():
for bullet in bullets:
pygame.draw.rect(screen, BULLET_COLOR, (bullet[0], bullet[1], 5, 10))
def move_enemies():
for enemy in enemies:
enemy[1] += enemy_speed
def move_bullets():
for bullet in bullets:
bullet[1] -= 10
def check_collisions():
global score
for bullet in bullets:
for enemy in enemies:
if bullet[0] in range(enemy[0], enemy[0] + 50) and bullet[1] in
range(enemy[1], enemy[1] + 30):
bullets.remove(bullet)
enemies.remove(enemy)
score += 1
break
def check_game_over():
for enemy in enemies:
if enemy[1] > HEIGHT - 50:
return True
return False
def display_score():
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, [10, 10])
def display_level():
level_text = font.render(f"Level: {level}", True, BLACK)
screen.blit(level_text, [10, 50])
# Main game loop
clock = pygame.time.Clock()
spawn_enemies(enemy_count)
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullets.append([player_pos[0] + 22, player_pos[1]])
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_pos[0] > 0:
player_pos[0] -= player_speed
11
TY BSC CS
ROLL NO: 18
if keys[pygame.K_RIGHT] and player_pos[0] < WIDTH - 50:
player_pos[0] += player_speed
screen.fill(WHITE)
move_enemies()
move_bullets()
check_collisions()
if check_game_over():
game_over_text = font.render("GAME OVER", True, WHITE)
screen.blit(game_over_text, [WIDTH // 2 - 100, HEIGHT // 2 - 20])
pygame.display.flip()
pygame.time.wait(2000)
game_over = True
continue
draw_player()
draw_enemies()
draw_bullets()
display_score()
display_level()
if not enemies:
level += 1
enemy_count += 5
enemy_speed += 1
spawn_enemies(enemy_count)
bullets = [bullet for bullet in bullets if bullet[1] > 0]
pygame.display.flip()
clock.tick(30)
pygame.quit()
OUTPUT: -
12
TY BSC CS
ROLL NO: 18
PRACTICAL: 5
CODE: -
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
# Load background image
background = pygame.image.load('game.jpg')
background = pygame.transform.scale(background, (WIDTH, HEIGHT)) #
Scale to fit the window
# Set up the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2D Infinite Scrolling Background")
# Variables for background scrolling
y1 = 0
y2 = -HEIGHT # Second background image positioned above the first one
# Main game loop
clock = pygame.time.Clock()
while True:
13
TY BSC CS
ROLL NO: 18
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Scroll the background
y1 += 1
y2 += 1
# Reset background positions when they move off screen
if y1 >= HEIGHT:
y1 = -HEIGHT
if y2 >= HEIGHT:
y2 = -HEIGHT
# Draw the background images
screen.blit(background, (0, y1))
screen.blit(background, (0, y2))
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)
OUTPUT: -
14
TY BSC CS
ROLL NO: 18
15
TY BSC CS
ROLL NO: 18
PRACTICAL: 6
16
TY BSC CS
ROLL NO: 18
6: Drag the assets to the Hierarchy > Click and hold asset and drag under
MainCamera
17
TY BSC CS
ROLL NO: 18
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.
18
TY BSC CS
ROLL NO: 18
Adjust gravity Scale
19
TY BSC CS
ROLL NO: 18
Rigidbodies are components that allow a GameObject to react to real-time
physics. This includes reactions to forces and gravity, mass, drag and
momentum. You can attach a Rigidbody to your GameObject by simply clicking
on Add Component and typing in Rigidbody2D in the search field.
10.To implement the camera shake effect, add a new script as follows:
20
TY BSC CS
ROLL NO: 18
The final output will result in the Camera Shake Effect on pressing the key ‘S’:
OUTPUT: -
`
21
TY BSC CS
ROLL NO: 18
PRACTICAL: 7
Here right click each image and save as picture in a particular folder given below
Gamecharacter Coin
Scene
22
TY BSC CS
ROLL NO: 18
5.Now In Project section we have Assets here we have to Create 3 Folders and name
it
23
TY BSC CS
ROLL NO: 18
1. Scripts
2. Sprites
3. Scenes
24
TY BSC CS
ROLL NO: 18
25
TY BSC CS
ROLL NO: 18
By selecting the first coin, press Ctrl + C then Ctrl + V and drag near one after other
as shown in the image
First - Double Tap – coinscript and write the following code in Visual Studio 2022
Filename: coinscript.cs
Program:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
27
TY BSC CS
ROLL NO: 18
{
scorescript.coinAmount += 1;
Destroy(gameObject);
}
}
Second - Double Tap – movescript and write the following code in Visual
Studio 2022
Filename: movescript.cs
Program:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
dirX = Input.GetAxis("Horizontal");
dirY = Input.GetAxis("Vertical");
}
void FixedUpdate()
{
rb.velocity = new Vector2(dirX * moveSpeed, dirY * moveSpeed);
28
TY BSC CS
ROLL NO: 18
}
}
Third - Double Tap – scorescript and write the following code in Visual Studio 2022
Filename: scorescript.cs
Program:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
void Start()
{
text = GetComponent<Text>();
}
void Update()
{
text.text = coinAmount.ToString();
}
}
So far our screen designing and its scripting done
10.Now we have to configure all objects from Hierarchy to its Inspector
window
Here, are our scene object, gamecharacter object and 6 coins then we will create
text file under Canvas object
By Right Click – Create Empty
GameObject will be created then rename it as Canvas
29
TY BSC CS
ROLL NO: 18
Now for Canvas – Right Click – UI – Legacy - Text
Now Coin
(Added 6 Times Coins with its Components and its Configuration needed)
Coin
Select 1st Coin from Hierarchy and from Inspector Window of Coin
Add Component – Circle Collider 2D then
31
TY BSC CS
ROLL NO: 18
Drag and Drop coinscript from Script folder to Inspector Window of the Coin
32
TY BSC CS
ROLL NO: 18
Now after adding the Coin that is first Coin we have configure its component
Sprite Renderer – Sorting Layer – New Layer 1
Circle Collider – is Trigger checked
Drag and Drop – coinscript
After adding all the components and its configurations for the coin
33
TY BSC CS
ROLL NO: 18
Now,
Gamecharacter
We have to add components to gamecharacter and configure it
Select gamecharacter then add Sorting Layer – New Layer 1
Add components – Rigidbody 2D and Box Collider 2D – Check and its properties then
drag and drop movescript
34
TY BSC CS
ROLL NO: 18
Now all set and we have completed all screen designing, scripting and
configurations to all objects.
To run the game, save and by pressing Ctrl + S and play
Output:
35
TY BSC CS
ROLL NO: 18
PRACTICAL: 8
Requirements: 2 Images
Snow land
Snow particle
Game Scene
36
TY BSC CS
ROLL NO: 18
Now in Project Window – Under Assets -> Right Click -> Import New Assets
37
TY BSC CS
ROLL NO: 18
Now fit the snowland background image into rectangle white border
Now zoom by pressing ctrl +scroll up roll from mouse and fit in screen
This will present the Particle System in the center that is trying to emit objects
38
TY BSC CS
ROLL NO: 18
To get the particle emit from sky. Go to Transform Change the Rotation value X = -
270 and Position value Z: 0. It will reverse the Particle System downwards shown
below.
From Here we have to keep Particle System selected for each change to updates
its properties in its Inspector Window.
To position the Particle System above the snowland background, select the Shape
module transform editing mode
39
TY BSC CS
ROLL NO: 18
Select on down side blue arrow, it will turn yellow arrow and then drag it upward
40
TY BSC CS
ROLL NO: 18
Start Size – Click on right most dropdown arrow and select Random Between Two
Constants and edit its values between 0.1 and 1.
41
TY BSC CS
ROLL NO: 18
4. Texture Sheet Animation – Here we have to add our snow particle image
into this texture sheet animation.
Then drag and drop our snow particle image from Assets to the texture sheet
animation
Here, this is the update and changes done in Texture Sheet Animation
42
TY BSC CS
ROLL NO: 18
Now our Screen Designing and Particle System Configurations done, Save the
Project and Play
Output:
43