0% found this document useful (0 votes)
14 views25 pages

CG Report

Uploaded by

kalaala335
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)
14 views25 pages

CG Report

Uploaded by

kalaala335
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/ 25

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“Jnana Sangama”, Belagavi – 590 018, Karnataka

COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT


REPORT (21CSL66) ON

“ROCK-PAPER-SCISSORS GAME”
Submitted in partial fulfilment of the requirements for the award of the Degree
of

Bachelor of Engineering
In
Computer Science and

Engineering By

Uday B (1TJ22CS411)
Rupam Bhunia (1TJ22CS408)

Under the guidance of:

Ms. Sonia Das

Assistant

professor
Department of CSE, TJIT

Academic Year: 2023 -2024

Department of Computer Science and

Engineering, T John Institute of Technology,


BENGALURU – 560083.
T JOHN INSTITUTE OF TECHNOLOGY
Affiliated to Visvesvaraya Technological University)
Approved by AICTE, Govt. of India, New Delhi.
No. 86/1, Gottigere, Bannerghatta Road, Bengaluru-560083

CERTIFICATE

This is to certify that the mini project work entitled “ROCK-PAPER-


SCISSORS GAME” carried out by Uday B(1TJ22CS411) and Rupam
Bhunia (1TJ22CS408), in partial fulfilments of the requirements of Computer
Graphics Laboratory with Mini Project (21CSL66) prescribed by the
Visvesvaraya Technological University, Belagavi for the VI Semester
B.E(Computer Science and Engineering) Degree course during the academic
year of 2023-2024.

Ms. Sonia Das Dr. Suma R


Assistant professor Associate professor & Head
Dept. of Computer Science, TJIT Dept. of Computer Science,TJIT

Internal Examiner External Examiner


DECLARATION
We Uday b and Rupam Bhunia students of Bachelor of Engineering in Computer Science &
Engineering, T John Institute of Technology, hereby declare that, under the supervision of
our guide Ms. Sonia Das have independently carried out the project titled “ROCK-PAPER-
SCISSORS GAME” and submitted it in partial fulfillment of the requirements of Computer
Graphics Laboratory with Mini Project prescribed by the Visvesvaraya Technological
University, Belgaum, for the VI Semester B.E (Computer Science & Engineering) during the
academic year 2023-2024.

Uday B (1TJ22CS411)

RupamBhunia(1TJ22CS408)
ACKNOWLEDGMENT

The satisfaction and euphoria that accompanies the successful completion of any task
wouldbe incomplete without mentioning the people who made it possible, whose
constant guidance and encouragement crowned my efforts with success.

We have a great pleasure in expressing our deep sense of gratitude to founder


Chairman, Dr. Thomas P John for having provided us with a great infrastructure
and well-furnished laboratories. we thank our beloved Principal, Dr. P Suresh
Venugopal for giving me the opportunity to study in this prestigious institute. his
constant support and encouragement.

We are also grateful to the HOD of Computer Science Department, Dr. Suma R for
supporting and guiding me in our academics. We would like to thank Ms.Sonia Das,
mini project Coordinator for her suggestions and advice given to complete the work.

We indebted to thank the entire computer science department for their help and the
co- operation. It is a great pleasure for us to acknowledge the valuable suggestions,
help and ideas of all our friends. Most importantly, we thank the almighty and the
family for their blessings and continuous support.
ABSTRACT

This Python script implements an interactive rock-paper-scissors game using computer vision for
hand gesture recognition. Leveraging the OpenCV library for video capture and image processing,
the game captures live video from the user's webcam. The cvzone library, particularly the
HandDetector module, is used for real-time hand tracking and gesture detection.

Players can start the game by pressing the 's' key, initiating a countdown timer displayed on the
screen. During this period, the player's hand gestures are analyzed to identify the moves—rock,
paper, or scissors—based on the number of fingers shown. Simultaneously, the computer makes a
random choice among the three options. The player's and computer's choices are visually displayed
using overlay images.

The game keeps track of the scores, updating them on the screen after each round. The first to reach
five points wins, triggering a win or lose message displayed with corresponding images. The game
can be exited at any time by pressing the 'Esc' key. This interactive game combines computer vision
techniques with engaging gameplay, providing an entertaining experience for the user.
TABLE OF CONTENTS

Titles Page No.

List of Figures Ⅰ

CHAPTER 1 INTRODUCTION 01
1.1 About Computer Graphics 01
1.2 About Python 01
1.2.1 Python Architecture 01
1.3 Python Package Organization 02
1.4 Python in the Project 03
1.5 Python Functions 03
1.5.1 Built-in function 03
1.5.2 Python Library 04

CHAPTER 2 SYSTEM REQUIREMENTS 05


2.1 Software Requirements 05
2.2 Hardware Requirements 05

CHAPTER 3 DESIGN 06

CHAPTER 4 IMPLEMENTATION 07
4.1 Rock-Paper-Scissors Game 07
4.2 User defined Functions 08
4.3 Imports and Process 09
4.4 Main Function and Program 09

CHAPTER 5 RESULTS 13

CONCLUSION 15

REFERENCES 16
LIST OF FIGURES

Fig. No. Description Page No

5.1 Game Start 13


5.2 Won the game 13
5.3 Game Lost 14
Rock-Paper-Scissors Game

CHAPTER 1

INTRODUCTION

1.1 About Computer Graphics

Computer graphics is an art of drawing pictures on computer screens with the help of
programming. It involves computations, creation, and manipulation of data. In other words,
we can say that computer graphics is a rendering tool for the generation and manipulation of
images. In today’s world, It is one of the most widely used powerful and interesting features
of the computer which gives us the power to handle the graphical data very efficiently and
also process them rapidly and effectively. Computer graphics started with a display of data on
hard copy plotters and cathode ray tube screens soon after the introduction of computers
themselves. The development of computer graphics has been driven both by the needs of the
user community and by advances in hardware and software. Computer graphics today is
largely interactive. The user controls the contents, structures and appearances of the object
and of their displayed images by using input devices, such as keyboard, mouse or touch-
screen.

1.2 About python

Python is a high level entrepreneur and object oriented programming language known for the
reliability and simplicity. It supports dynamics semantics and offers building data structures
making it versatile for various application. Python is widely used in the web development,
scientific computing, data analysis, artificial intelligence and automation. Its design
philosophy prioritizes code readability achieved through significant indentation.

1.2.1 Python Architecture

Python has been widely used to develop large-scale software systems such as distributed
systems, cloud computing, artificial intelligence, and Web platforms due to its flexibility
and versatility. As a kind of complex software, Python interpreter could also suffer from
software bugs and thus fundamentally threaten the quality of all Python program

Department of CSE, TJIT, 2023-24 Page 1


Bangalore
Rock-Paper-Scissors Game

Fig:1.2.1 Python Architecture

1.3 Python Package Organization


Python package creates a hierarchical directory structure with numerous modules and sub-
packages to give an application development environment. They are simply a collection of
modules and sub-packages.

When working on a large or complex project, we frequently wind up with multiple modules.
Using, maintaining, and organizing so many modules is challenging.
Fortunately. Python delivers us a simple solution in the form of packages. Using packages, we
can easily group, organize, and use multiple modules.
Packages also allow us to access all functions from all modules in a package with just one import
statement

Department of CSE, TJIT, 2023-2 Page 2


Bangalore
Rock-Paper-Scissors Game

Python packages Organization

Fig:1.3 Python packages

1.4 Python in the Project

The hand Space War Game system using image processing looks for patterns using
algorithms trained on data from depth and leap motion sensors: The system distinguishes a
hand from the background using color and depth data. The hand sample is further divided
into the arm, wrist, palm, and fingers.

1.5 Python Functions

1.5.1 BUILT-IN FUNCTION

Python Functions is a block of statements that return the specific task. The idea is to put
some commonly or repeatedly done tasks together and make a function so that instead of
writing the same code again and again for different inputs, we can do the function calls.

Department of CSE, TJIT, 2023-2 Page 3


Bangalore
Rock-Paper-Scissors Game

Some Benefits of Using Functions

Increase Code Readability


Increase Code Reusability
Built-in Functions.
 User-defined Functions.
 Anonymous Functions (Lambda Functions)
 Recursive Functions.
 Higher-Order Functions.
 Generator Functions.
 Fundamental User-defined Feature.
 Function with Parameters.

1.5.2 Python Library

1.5.1 cv2 (OpenCV): A powerful library for real-time computer vision and image processing.
It provides tools for capturing video, processing images, and performing tasks such as
object detection and recognition.

1.5.2 cvzone: A library built on top of OpenCV, simplifying common computer vision tasks
like hand detection, face detection, and object tracking. It provides easy-to-use functions
for various applications in computer vision.

1.5.3 random: A built-in Python library that generates pseudo-random numbers for various
distributions. It is commonly used for random number generation, shuffling data, and
random selections.

Department of CSE, TJIT, 2023-24 Page 4


Bangalore
Rock-Paper-Scissors Game

CHAPTER 5

SYSTEM REQUIREMENT

The package is designed such that users with a computer having minimum configuration can
also use it, which does not require complex graphics packages.
The package requires simple in-built functions found in the header file along with a few
users defined functions.

2.1 Software Requirements

 Operating System - Ubuntu 14.04 / Windows

 Language Tool - Python Compiler 3

 Libraries - python3

 Documentation Tool - MS-Word

2.2 Hardware Requirements

 Processor - Intel Pentium onwards Compatible Hardware

 RAM - 256Mb RAM (minimum)

 Hard Disk - 4 GB (minimum)

 Monitor – VGA Compatible

 Keyboard - Standard 101 keys Keyboard

Department of CSE, TJIT, 2023-24 Page 5


Bangalore
Rock-Paper-Scissors Game

CHAPTER 3

DESIGN

The rock-paper-scissors game is developed using OpenCV for video capture and image
processing, along with cvzone's HandDetector for real-time hand gesture recognition. The
game captures live video from the user's webcam and processes it to detect hand gestures,
determining the player's move (rock, paper, or scissors) based on the number of fingers
shown. The game is initiated by pressing the 's' key, starting a 3-second countdown for the
player to display their gesture, while the computer simultaneously makes a random choice.
The player's and computer's choices are visually displayed using overlay images, and scores
are updated and shown on the screen after each round. The game continues until either the
player or the computer wins five rounds, at which point a win or lose message is displayed.
The game can be exited at any time by pressing the 'Esc' key. This interactive design
combines computer vision techniques with engaging gameplay, providing an entertaining user
experience.

Department of CSE, TJIT, 2023-24 Page 6


Bangalore
Rock-Paper-Scissors Game

CHAPTER 4

IMPLEMENTATION

4.1 Rock-Paper-Scissors Game

This rock-paper-scissors game is implemented using Python and several libraries to enhance
the gameplay experience with real-time hand gesture recognition and interactive elements.
 Hand Gesture Recognition: Utilizes cvzone's HandDetector class to detect and
analyze hand gestures. The hand gestures (rock, paper, or scissors) are determined
based on the number of fingers shown, enhancing the interactive nature of the game.
 Video Capture and Processing: Employs cv2.VideoCapture() to capture live video
from the webcam. The captured video frames are processed and resized using
cv2.resize() to fit the game display and ensure smooth gameplay.
 Game Timer: Implements a timer using Python's time module. The elapsed time is
tracked and displayed to create a countdown before the player's move is registered,
adding a dynamic element to the gameplay.
 Computer's Move: Uses Python’s random.randint() to generate a random move for
the computer. This move is then displayed on the screen, and game logic determines
the winner of each round based on the player's and computer's moves.
 Score Tracking: Manages game scores using a simple list to keep track of both the
player’s and computer’s scores. The scores are updated and displayed on the screen,
and the game checks for win conditions based on score thresholds.
 Overlay Images: Incorporates cvzone.overlayPNG() to overlay images (such as the
computer’s move and win/lose notifications) onto the background image. This visual
feedback enhances the user experience by providing clear and engaging game states.
 Game Control: Handles user inputs via keyboard events, such as starting the game
with the 's' key and exiting with the 'Esc' key. This control mechanism allows for
easy interaction and navigation within the game.
 Endgame Conditions: Implements endgame logic by checking if either the player or
computer reaches a score of 5. The game displays a win or lose message accordingly
and exits after a brief pause, ensuring a satisfying conclusion to the gameplay

Department of CSE, TJIT, 2023-24 Page 7


Bangalore
Rock-Paper-Scissors Game

4.2 User Defined Functions used in the project


In the rock-paper-scissors game, user-defined functions include:

1. detectHandGesture(fingers): Identifies the player's move based on finger positions.

2. updateScores(playerMove, randomNumber): Updates the game scores based on

the round's outcome.

3. showGameResult(imgBG, result): Displays win or lose messages and overlays

the appropriate image.

4. updateTimer(startTime): Calculates and displays the elapsed time since the game started.

5. showScores(imgBG): Updates and displays the current scores on the screen.

4.3 Important imports

1. Libraries and Modules Import:


o import random: Generates random numbers to determine the computer's move in the
game.
o import cv2: Handles video capture and image processing for real-time hand gesture
recognition.
o import cvzone: Provides utilities for overlaying images and hand detection through the
HandDetector class.
o from cvzone.HandTrackingModule import HandDetector: Detects and tracks hand
gestures to determine player moves.
o import time: Manages timing functions, such as tracking the game countdown and
elapsed time.

Department of CSE, TJIT, 2023-24 page 8


Bangalore
Rock-Paper-Scissors Game

4.3.1 Process
1. User Input Handling: The game captures hand gestures to determine the player's move but
does not handle edge cases like multiple hand detections or varying hand sizes, which might
affect gesture recognition accuracy.

2. Scaling and Resolution Management: The game uses fixed video frame resizing and
background image cropping without adapting to different screen sizes or resolutions,
limiting flexibility.

3. Optimized Resource Management: The code loads images and initializes resources at the
start but does not include efficient unloading or memory management, potentially leading to
resource inefficiencies during extended gameplay.

4. Game Logic and State Management: The game logic, including gesture detection and
score updates, is managed within a single loop without advanced state management or
handling for different game phases, keeping the implementation simple but less flexible.

4.4 Main Function

 Event Handling: The game loop processes user inputs, such as detecting hand gestures
to determine the player’s move and handling key presses to start or exit the game.
 Game State Updates: The loop updates the game state by tracking the elapsed time,
determining the player's gesture, generating the computer’s move, and updating scores
based on the round results.
 Rendering: The loop renders the background, player’s hand gesture, computer’s move,
and scores on the screen, including overlaying images for win/lose conditions and
updating the display accordingly.

Department of CSE, TJIT, 2023-24 Page 9


Bangalore
Rock-Paper-Scissors Game

4.4.1 Program

import random
import cv2
import cvzone
from cvzone.HandTrackingModule import HandDetector
import time
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
detector = HandDetector(maxHands=1, detectionCon=0.8)

timer = 0
stateResult = False
startGame = False
scores = [0, 0]

# Load winning and losing images


winImage = cv2.imread("Resources/win.png", cv2.IMREAD_UNCHANGED)
loseImage = cv2.imread("Resources/lose.png", cv2.IMREAD_UNCHANGED)

while True:
imgBG = cv2.imread("Resources/BG.png")
success, img = cap.read()

imgScaled = cv2.resize(img, (0, 0), None, 0.875, 0.875)


imgScaled = imgScaled[:, 80:480]

hands, img = detector.findHands(imgScaled)


if startGame:
if stateResult is False:
timer = time.time() - initialTime
cv2.putText(imgBG, str(int(timer)), (605, 435), cv2.FONT_HERSHEY_PLAIN, 6,
(255, 0, 255), 4)

Department of CSE, TJIT, 2023-24 page 10


Bangalore
Rock-Paper-Scissors Game

if timer > 3:
stateResult = True
timer = 0

if hands:
playerMove = None
hand = hands[0]
fingers = detector.fingersUp(hand)
if fingers == [0, 0, 0, 0, 0]:
playerMove = 1 # Rock
elif fingers == [1, 1, 1, 1,
1]: playerMove = 2 #
Paper elif fingers == [0, 1,
1, 0, 0]:
playerMove = 3 # Scissors

randomNumber = random.randint(1, 3)
imgAI = cv2.imread(f'Resources/{randomNumber}.png',
cv2.IMREAD_UNCHANGED)
imgBG = cvzone.overlayPNG(imgBG, imgAI, (149, 310))

if (playerMove == 1 and randomNumber == 3) or \


(playerMove == 2 and randomNumber == 1) or \
(playerMove == 3 and randomNumber == 2):
scores[1] += 1

if (playerMove == 3 and randomNumber == 1) or \


(playerMove == 1 and randomNumber == 2) or \
(playerMove == 2 and randomNumber == 3):
scores[0] += 1

Department of CSE, TJIT, 2023-24 page 11


Bangalore
Rock-Paper-Scissors Game

imgBG[234:654, 795:1195] = imgScaled


if stateResult:
imgBG = cvzone.overlayPNG(imgBG, imgAI, (149, 310))
cv2.putText(imgBG, str(scores[0]), (410, 215), cv2.FONT_HERSHEY_PLAIN, 4, (255, 255,
255), 6)
cv2.putText(imgBG, str(scores[1]), (1112, 215), cv2.FONT_HERSHEY_PLAIN, 4,(255, 255,
255), 6)
if scores[0] == 5:
imgBG = cvzone.overlayPNG(imgBG, loseImage, (400, 200))
cv2.putText(imgBG, "You Lose!", (500, 180), cv2.FONT_HERSHEY_PLAIN, 6, (0, 0, 255), 4)
elif scores[1] == 5:
imgBG = cvzone.overlayPNG(imgBG, winImage, (400, 200))
cv2.putText(imgBG, "You Win!", (500, 180), cv2.FONT_HERSHEY_PLAIN, 6, (0, 255,
0), 4)
cv2.imshow("BG", imgBG)
key = cv2.waitKey(1)
if key == ord('s'):
startGame = True
initialTime = time.time()
stateResult = False
elif key == 27: # Press 'Esc' to exit
break
# Exit loop if someone wins
if scores[0] == 5 or scores[1] == 5:
time.sleep(2) # Show the win/lose image for 2 seconds
break
cap.release()
cv2.destroyAllWindow

Department of CSE, TJIT, 2023-24 Page 12


Bangalore
Rock-Paper-Scissors Game

CHAPTER 5

RESULTS
5.1 Screenshots
Game Start

Fig 5.1 Game Start

Game Won

Fig 5.2 Won the Game

Department of CSE, TJIT, Bangalore 2023-24 page 13


Rock-Paper-Scissors Game

Fig 5.3 Game Lost

Department of CSE, TJIT, 2023-24 page 14


Bangalore
Rock-Paper-Scissors Game

CONCLUSION
This project demonstrates the development of a rock-paper-scissors game using Python,
OpenCV, and the Cvzone library for real-time hand gesture recognition. Key functionalities
include detecting player gestures, generating random computer moves, updating and
displaying scores, and managing game states with visual feedback. The game loop effectively
processes user inputs, updates game states, and renders necessary visual components to create
an engaging interactive experience. While the game is fully functional, improvements could
be made in areas such as handling edge cases in gesture detection, scaling for different
resolutions, and optimizing resource management for better performance. Overall, this project
provides a solid foundation for understanding the integration of computer vision and Python
programming in creating interactive games.

Department of CSE, TJIT, 2023-24 page 15


Bangalore
Rock-Paper-Scissors Game

REFERENCES

1. https://youtu.be/k2EahPgl0ho?si=PX0hcHQOsM0zNu3t
2. https://youtu.be/0uSA3xyXlwM?si=BJAlzPBhOhOctpsX
3. https://youtu.be/WexB24-_KCM?si=GKu6IXcLMXzvzNOE

Department of CSE, TJIT, 2023-24 Page 16


Bangalore
Rock-Paper-Scissors Game

You might also like