Professional Documents
Culture Documents
GP Journal
GP Journal
GP Journal
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
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
#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
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
import math
root=Tk()
root.title("Mariya Shaikh_71")
C=Canvas(root,bg="aqua",height=2000,width=2000)
arrow=C.create_polygon(500,185,600,100,600,150,720,150,720,220,600,220,600,
270,fill="red")
#Reflection
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
#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
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]
#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)
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)
red = pygame.Color(255, 0, 0)
# Initialising pygame
pygame.init()
# Initialise game window
pygame.display.set_caption('GeeksforGeeks Snakes')
fps = pygame.time.Clock()
[90, 50],
[80, 50],
[70, 50]
# fruit position
fruit_spawn = True
# right
direction = 'RIGHT'
change_to = direction
# initial score
score = 0
# score_surface
# surface object
score_rect = score_surface.get_rect()
# displaying text
game_window.blit(score_surface, score_rect)
# will be drawn
game_over_surface = my_font.render(
# surface object
game_over_rect = game_over_surface.get_rect()
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
time.sleep(2)
# deactivating pygame library
pygame.quit()
quit()
# Main Function
while True:
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'
direction = 'UP'
direction = 'DOWN'
direction = 'LEFT'
direction = 'RIGHT'
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
# 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_spawn = True
game_window.fill(black)
pygame.draw.rect(game_window, green,
game_over()
game_over()
pygame.display.update()
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;
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>();
}
Ground Inspector
Player Inspector
Wall Inspector
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;