GP Journal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

SHAIKH MARIYA NASRULLAH

TCS2324071
TYBSC-CS
SEMESTER-5
GAME PROGAMMING
PRACTICAL JOURNAL
S.I.E.S College of Arts, Science and Commerce (Autonomous) Sion(W),
Mumbai-400 022.

CERTIFICATE

This is to certify that Mr. / Miss. SHAIKH MARIYA NASRULLAH


Roll No. TCS2324071 has successfully completed the necessary course of
experiments in the subject of GAME PROGRAMMING during the academic
year 2023 – 2024 complying with the requirements for the course of T.Y. BSc.
Computer Science [Semester-V].

Prof. In-Charge Examination Date:


Prof. Soni Yadav
(Game Programming)

HOD’s Signature & Date:


Dr. Manoj Singh

College Seal
& Date
INDEX
Sr Aim Date Sign
No
1 Write a python program to perform translation operation 25-07-2023
on rectangle by taking initial coordinates from user.
2 Write a python program to perform scaling operation on 25-07-2023
triangle by taking initial coordinates from user.
3 Write a python program to perform reflection operation 01-08-2023
on polygon by taking initial coordinates from user.
4 Write a python program to rotate right angle triangle by 01-08-2023
45 degree by taking initial coordinates from user.
5 Write a python program to perform shearing on rectangle 22-08-2023
in positive direction of x-axis by taking initial coordinates
from user.
6 Write a python program to create below shape and perform 22-08-2023
reflection about parallel to y-axis, followed by translation
and scaling operation on it.
7 Implement space invader game in python using pygame 08-08-2023
module.
8 Implement Snake game in python using pygame module. 03-09-2023

9 Implement 2D UFO game using unity. 25-08-2023

10 Implement 3D roll ball game using unity. 29-08-2023


Practical-1
Aim: Write a python program to perform translation operation on
rectangle by taking initial coordinates from user.

#Translation (change position)

from tkinter import*


root=Tk()
root.title("Mariya Shaikh_71")
c=Canvas(root,bg="aqua",height="700",width="700")
x0=int(input("Enter x0 : "))
y0=int(input("Enter y0 : "))
x1=int(input("Enter x1 : "))
y1=int(input("Enter y1 : "))
x2=int(input("Enter x2 : "))
y2=int(input("Enter y2 : "))
x3=int(input("Enter x3 : "))
y3=int(input("Enter y3 : "))
rectangle=c.create_polygon(x0,y0,x1,y1,x2,y2,x3,y3,fill="red")
tx=100
ty=100
rectangle=c.create_polygon(x0+tx,y0+ty,x1+tx,y1+ty,x2+tx,y2+ty,x3+t
x,y3+ty,fill="blue")
c.pack()
mainloop()
Practical-2
Aim: Write a python program to perform scaling operation on
triangle by taking initial coordinates from user.

#Scaling (change Size)

from tkinter import *


root=Tk()
root.title("Mariya Shaikh_71")
C=Canvas(root,bg="aqua",height=400,width=400)
C.create_text(110,30,text="Triangle before Scaling")
x0=int(input("Enter value : "))
y0=int(input("Enter value : "))
x1=int(input("Enter value : "))
y1=int(input("Enter value : "))
x2=int(input("Enter value : "))
y2=int(input("Enter value : "))
poly=C.create_polygon(x0,y0,x1,y1,x2,y2,fill="red")
sx=2
sy=2
C.create_text(110,200,text="Triangle after Scaling")
poly=C.create_polygon(100*sx,100*sy,100*sx,150*sy,150*sx,150*sy,
fill="blue")
C.pack()
mainloop()
Practical-3
Aim: Write a python program to perform reflection operation on
polygon by taking initial coordinates from user.

#REFLECTION
from tkinter import*
import math
root=Tk()
root.title("Mariya Shaikh_71")
c=Canvas(root,bg="aqua",height="2000",width="2000")
x0=int(input("Enter value for x0 : "))
y0=int(input("Enter value for y0 : "))
x1=int(input("Enter value for x1 : "))
y1=int(input("Enter value for y1 : "))
x2=int(input("Enter value for x2 : "))
y2=int(input("Enter value for y2 : "))
x3=int(input("Enter value for x3 : "))
y3=int(input("Enter value for y3 : "))
x4=int(input("Enter value for x4 : "))
y4=int(input("Enter value for y4 : "))
a=int(input("Enter value : "))
poly=c.create_polygon(x0,y0,x1,y1,x2,y2,x3,y3,x4,y4,fill="red")
#y=a
#x'=x
#y'=-y+2a
poly=c.create_polygon(x0,-y0+2*a,x1,-y1+2*a,x2,-y2+2*a,x3,-y3+2*a,x4,-
y4+2*a,fill="blue")
c.pack()
mainloop()
OR
Taking axis from user
#Reflection
#taking axis from user
from tkinter import *
root = Tk()
root.title("Mariya Shaikh_71")
canvas = Canvas(root, bg="#FFE4E1", height = 500, width = 500)
axis = input("At what axis should the reflection be performed (x/y):")
x0 = int(input("Enter x0:"))
y0 = int(input("Enter y0:"))
x1 = int(input("Enter x1:"))
y1 = int(input("Enter y1:"))
x2 = int(input("Enter x2:"))
y2 = int(input("Enter y2:"))
x3 = int(input("Enter x3:"))
y3 = int(input("Enter y3:"))
x4 = int(input("Enter x4:"))
y4 = int(input("Enter y4:"))
a = int(input("Enter value of a:"))
polygon = canvas.create_polygon(x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, fill="red")
if(axis == "x"):
canvas.create_text(120, 20, text = "Polygon before Reflection", font =
('BOLD'))
canvas.create_text(120, (-y0+2*a)+20, text = "Polygon after Reflection", font
= ('BOLD'))
#parallel to x - axis
new_polygon = canvas.create_polygon(x0, -y0+2*a, x1, -y1+2*a, x2, -y2+2*a,
x3, -y3+2*a, x4, -y4+2*a, fill = "lime")
elif (axis == 'y'):
canvas.create_text(360, 20, text = "Polygon before Reflection", font =
('BOLD'))
canvas.create_text((-x0+2*a)-20, 200, text = "Polygon after Reflection", font
= ('BOLD'))
#parallel to y-axis
new_polygon = canvas.create_polygon(-x0+2*a, y0, -x1+2*a, y1, -x2+2*a, y2,
-x3+2*a, y3, -x4+2*a, y4, fill = "lime")
canvas.pack()
mainloop()
Practical-4
Aim: Write a python program to rotate right angle triangle by 45
degree by taking initial coordinates from user.

#Practical 4
#Rotation

from tkinter import *


import math
root=Tk()
root.title("Mariya Shaikh_71")
C=Canvas(root,bg="aqua",height=700,width=700)
#C.create_text(110,30,text="Triangle before Rotation")
x0=int(input("Enter x0 : "))
y0=int(input("Enter y0 : "))
x1=int(input("Enter x1 : "))
y1=int(input("Enter y1 : "))
x2=int(input("Enter x2 : "))
y2=int(input("Enter y2 : "))

b=int(input("Enter angle of rotation : "))

poly=C.create_polygon(x0,y0,x1,y1,x2,y2,fill="red")
#C.create_text(110,200,text="Triangle after Rotation")
x11=abs(x0*math.cos(math.radians(b))-
y0*math.sin(math.radians(b)))
y11=abs(x0*math.sin(math.radians(b))+y0*math.cos(math.radians(b)
))
x12=abs(x1*math.cos(math.radians(b))-
y1*math.sin(math.radians(b)))
y12=abs(x1*math.sin(math.radians(b))+y1*math.cos(math.radians(b)
))
x13=abs(x2*math.cos(math.radians(b))-
y2*math.sin(math.radians(b)))
y13=abs(x2*math.sin(math.radians(b))+y2*math.cos(math.radians(b)
))
poly=C.create_polygon(x11,y11,x12,y12,x13,y13,fill="blue")
C.pack()
mainloop()
Practical-5
Aim: Write a python program to perform shearing on rectangle in
positive direction of x-axis by taking initial coordinates from user.

#Practical 5
#Shearing
from tkinter import *
import math
root=Tk()
root.title("Mariya Shaikh_71")
C=Canvas(root,bg="aqua",height=400,width=400)
C.create_text(110,30,text="Shearing")

x0=int(input("Enter x0 : "))
y0=int(input("Enter y0 : "))
x1=int(input("Enter x1 : "))
y1=int(input("Enter y1 : "))
x2=int(input("Enter x2 : "))
y2=int(input("Enter y2 : "))
x3=int(input("Enter x3 : "))
y3=int(input("Enter y3 : "))
b=int(input("Enter the angle of shearing : "))
poly=C.create_polygon(x0,y0,x1,y1,x2,y2,x3,y3,fill="red")
xsh1=x0+y0*math.tan(math.radians(b))
xsh2=x3+y3*math.tan(math.radians(b))
poly=C.create_polygon(xsh1,y0,x1,y1,x2,y2,xsh2,y3,fill="blue")
C.pack()
mainloop()
Practical-6
Aim: Write a python program to create below shape and perform
reflection about parallel to y-axis, followed by translation and
scaling operation on it.

#Practical 6

from tkinter import *

import math

root=Tk()

root.title("Mariya Shaikh_71")

C=Canvas(root,bg="aqua",height=2000,width=2000)

C.create_text(650,90,text="Arrow before Reflection",fill="black",font=('Helvetica


15 bold'))

arrow=C.create_polygon(500,185,600,100,600,150,720,150,720,220,600,220,600,
270,fill="red")

#Reflection

a=int(input("Enter Value : "))

C.create_text(300,90,text="Arrow after Reflection",fill="black",font=('Helvetica


15 bold'))

x0=-500+2*a

x1=-600+2*a

x2=-600+2*a

x3=-720+2*a

x4=-720+2*a

x5=-600+2*a
x6=-600+2*a

C.create_polygon(-500+2*a,185,-600+2*a,100,-600+2*a,150,-720+2*a,150,-
720+2*a,220,-600+2*a,220,-600+2*a,270,fill="blue")

#Translation

tx=500

ty=500

C.create_text(1080,700,text="Translated Arrow",fill="black",font=('Helvetica 15
bold'))

translatedarrow=C.create_polygon(x0+tx,185+ty,x1+tx,100+ty,x2+tx,150+ty,x3+t
x,150+ty,x4+tx,220+ty,x5+tx,220+ty,x6+tx,270+ty,fill="green")

#Scaling

sx=2

sy=2

C.create_text(1060,350,text="Scaled Arrow",fill="black",font=('Helvetica 15
bold'))

scalingarrow=C.create_polygon(x0*sx,185*sy,x1*sx,100*sy,x2*sx,150*sy,x3*sx,
150*sy,x4*sx,220*sy,x5*sx,220*sy,x6*sx,270*sy,fill="pink")

C.pack()

mainloop()
Practical-7
Aim: Implement space invader game in python using pygame
module.

import pygame
import random
import math
from pygame import mixer

pygame.init() #initializing pygame module


screen=pygame.display.set_mode((800,600)) #Creating Screen, Screen size
pygame.display.set_caption("Space Invaders_71") #Chnage game name
icon=pygame.image.load("ufo.png") #Change icon for game
pygame.display.set_icon(icon)

#background Image
background=pygame.image.load("background (1).png")
#background music
mixer.music.load("background.wav")
mixer.music.play(-1)

#player
playerimg=pygame.image.load("player.png")
playerX=370
playerY=480
playerX_change=0

#enemy
enemyimg=[]
enemyX=[]
enemyY=[]
enemyX_change=[]
enemyY_change=[]
num_of_enemies=6
for i in range(num_of_enemies):
enemyimg.append(pygame.image.load("enemy.png"))
enemyX.append(random.randint(0, 735))
enemyY.append(random.randint(50, 150))
enemyX_change.append(3)
enemyY_change.append(40)

#Bullet
#Ready - you cant see bullet on screen
#Fire - bullet is fired from player and can be seen
bulletimg=pygame.image.load("bullet.png")
bulletX=0
bulletY=480
bulletX_change=0
bulletY_change=10
bullet_state="ready"

#score
score_value=0
font=pygame.font.Font("freesansbold.ttf",32)
textX=10
textY=10

#Game Over Text


over_font=pygame.font.Font("freesansbold.ttf",70)
def game_over_text():
over_text = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200,250))

def show_score(x,y):
score=font.render("Score: "+str(score_value),True,(255,255,255))
screen.blit(score,(x,y))

def player(x,y):
screen.blit(playerimg,(x,y)) #blit is for drawing

def enemy(x,y,i):
screen.blit(enemyimg[i],(x,y)) #drawing enemy on screen

def fire_bullet(x,y):
global bullet_state
bullet_state="fire"
screen.blit(bulletimg,(x+16,y+10))

def isCollision(enemyX,enemyY,bulletX,bulletY):
distance=math.sqrt((math.pow(enemyX-bulletX,2))+(math.pow(enemyY-
bulletY,2)))
if distance<27:
return True
else:
return False

#Stop Condition
running=True
while running:
screen.fill((0, 0, 0)) # Change background
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT: #left key is pressed
playerX_change=-4
if event.key==pygame.K_RIGHT: #right key is pressed
playerX_change = 4
if event.key==pygame.K_SPACE:
if bullet_state is "ready":
bullet_sound=mixer.Sound("laser.wav")
bullet_sound.play()
bulletX=playerX
fire_bullet(bulletX,bulletY)

if event.type==pygame.KEYUP:
if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
playerX_change=0

playerX+=playerX_change
#player boundary

if playerX<=0:
playerX=0
elif playerX>=736:
playerX=736

#enemy movement
for i in range(num_of_enemies):
#Game Over
if enemyY[i]>480:
for j in range(num_of_enemies):
enemyY[i]=2000
game_over_text()
break

enemyX[i] += enemyX_change[i]
if enemyX [i]<= 0:
enemyX_change[i] = 3
enemyY[i] += enemyY_change[i]
elif enemyX [i]>= 736:
enemyX_change [i]= -3
enemyY[i] += enemyY_change[i]

collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)


if collision:
explosion_sound=mixer.Sound("explosion.wav")
explosion_sound.play()
bulletY = 480
bullet_state = "ready"
score_value+= 1

enemyX[i] = random.randint(0, 735)


enemyY[i] = random.randint(50, 150)
enemy(enemyX[i],enemyY[i],i)

#Bullet movement
if bulletY<=0:
bulletY=400
bullet_state="ready"
if bullet_state is "fire":
fire_bullet(bulletX,bulletY)
bulletY-=bulletY_change

player(playerX,playerY)
show_score(textX,textY)

pygame.display.update() #call in last to update all the things we


added
Practical-8
Aim: Implement Snake game in python using pygame module.
# importing libraries

import pygame

import time

import random

snake_speed = 15

# Window size

window_x = 720

window_y = 480

# defining colors

black = pygame.Color(0, 0, 0)

white = pygame.Color(255, 255, 255)

red = pygame.Color(255, 0, 0)

green = pygame.Color(0, 255, 0)

blue = pygame.Color(0, 0, 255)

# Initialising pygame

pygame.init()
# Initialise game window

pygame.display.set_caption('GeeksforGeeks Snakes')

game_window = pygame.display.set_mode((window_x, window_y))

# FPS (frames per second) controller

fps = pygame.time.Clock()

# defining snake default position

snake_position = [100, 50]

# defining first 4 blocks of snake body

snake_body = [[100, 50],

[90, 50],

[80, 50],

[70, 50]

# fruit position

fruit_position = [random.randrange(1, (window_x//10)) * 10,

random.randrange(1, (window_y//10)) * 10]

fruit_spawn = True

# setting default snake direction towards

# right
direction = 'RIGHT'

change_to = direction

# initial score

score = 0

# displaying Score function

def show_score(choice, color, font, size):

# creating font object score_font

score_font = pygame.font.SysFont(font, size)

# create the display surface object

# score_surface

score_surface = score_font.render('Score : ' + str(score), True, color)

# create a rectangular object for the text

# surface object

score_rect = score_surface.get_rect()

# displaying text

game_window.blit(score_surface, score_rect)

# game over function


def game_over():

# creating font object my_font

my_font = pygame.font.SysFont('times new roman', 50)

# creating a text surface on which text

# will be drawn

game_over_surface = my_font.render(

'Your Score is : ' + str(score), True, red)

# create a rectangular object for the text

# surface object

game_over_rect = game_over_surface.get_rect()

# setting position of the text

game_over_rect.midtop = (window_x/2, window_y/4)

# blit will draw the text on screen

game_window.blit(game_over_surface, game_over_rect)

pygame.display.flip()

# after 2 seconds we will quit the program

time.sleep(2)
# deactivating pygame library

pygame.quit()

# quit the program

quit()

# Main Function

while True:

# handling key events

for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

change_to = 'UP'

if event.key == pygame.K_DOWN:

change_to = 'DOWN'

if event.key == pygame.K_LEFT:

change_to = 'LEFT'

if event.key == pygame.K_RIGHT:

change_to = 'RIGHT'

# If two keys pressed simultaneously

# we don't want snake to move into two


# directions simultaneously

if change_to == 'UP' and direction != 'DOWN':

direction = 'UP'

if change_to == 'DOWN' and direction != 'UP':

direction = 'DOWN'

if change_to == 'LEFT' and direction != 'RIGHT':

direction = 'LEFT'

if change_to == 'RIGHT' and direction != 'LEFT':

direction = 'RIGHT'

# Moving the snake

if direction == 'UP':

snake_position[1] -= 10

if direction == 'DOWN':

snake_position[1] += 10

if direction == 'LEFT':

snake_position[0] -= 10

if direction == 'RIGHT':

snake_position[0] += 10

# Snake body growing mechanism

# if fruits and snakes collide then scores

# will be incremented by 10

snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_position[0] and snake_position[1] ==
fruit_position[1]:

score += 10

fruit_spawn = False

else:

snake_body.pop()

if not fruit_spawn:

fruit_position = [random.randrange(1, (window_x//10)) * 10,

random.randrange(1, (window_y//10)) * 10]

fruit_spawn = True

game_window.fill(black)

for pos in snake_body:

pygame.draw.rect(game_window, green,

pygame.Rect(pos[0], pos[1], 10, 10))

pygame.draw.rect(game_window, white, pygame.Rect(

fruit_position[0], fruit_position[1], 10, 10))

# Game Over conditions

if snake_position[0] < 0 or snake_position[0] > window_x-10:

game_over()

if snake_position[1] < 0 or snake_position[1] > window_y-10:


game_over()

# Touching the snake body

for block in snake_body[1:]:

if snake_position[0] == block[0] and snake_position[1] == block[1]:

game_over()

# displaying score continuously

show_score(1, white, 'times new roman', 20)

# Refresh game screen

pygame.display.update()

# Frame Per Second /Refresh Rate

fps.tick(snake_speed)
Practical-9
Aim: Implement 2D UFO game using unity.
Main Camera Inspector
Background Inspector
Player Inspector
PickUP Inspector
Canvas Inspector
Score WinText
Cameracontoller script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraContoller : MonoBehaviour


{
public GameObject player;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}

// Update is called once per frame


void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}

PlayerController Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Text winText;
public Text countText;
public int count = 0;
private Rigidbody2D rbd;
public float speed;
// Start is called before the first frame update
void Start()
{
rbd = GetComponent<Rigidbody2D>();
}

// Update is called once per frame


void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical= Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rbd.AddForce(movement*speed);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag=="PickUp")
{
other.gameObject.SetActive(false);
count++;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count" + count.ToString();
if(count==8)
{
winText.text = "You Win!";
}
}
}
Rotator Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour


{
// Start is called before the first frame update
void Start()
{

// Update is called once per frame


void Update()
{
transform.Rotate(new Vector3(0, 0, 45) * Time.deltaTime);
}
}
Practical-10
Aim: Implement 3D roll ball game using unity.
Main Camera Inspector

Ground Inspector
Player Inspector
Wall Inspector

WestWall (same East wall with minus plus different)

NorthWall (same South wall with minus plus different)


PickUp Inspector
Canvas
Score Win
CameraController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour


{
private Vector3 offset;
public GameObject player;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}

// Update is called once per frame


void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}

PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour


{
public Text winText;
public Text countText;
public int count;
private Rigidbody rb;
public float speed;
// Start is called before the first frame update
void Start()
{
count = 0;
winText.text = "";
rb = GetComponent<Rigidbody>(); //initialize
SetCountText();
}

// Update is called once per frame


void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); //0.0f is
speed
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count>=9)
{
winText.text = "You Win!";
}
}
}
Rotator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour


{
// Start is called before the first frame update
void Start()
{

// Update is called once per frame


void Update()
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
}

You might also like