0% found this document useful (0 votes)
27 views

Python Project

The document describes creating a Rock, Paper, Scissors game using Pygame. It outlines initializing Pygame, loading images, defining buttons and functions for gameplay, tracking scores, and a main game loop to continuously run the game.

Uploaded by

srthksk8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Python Project

The document describes creating a Rock, Paper, Scissors game using Pygame. It outlines initializing Pygame, loading images, defining buttons and functions for gameplay, tracking scores, and a main game loop to continuously run the game.

Uploaded by

srthksk8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Problem Statement : Creating a user friendly game of Rock, Paper, Scissor using

Pygames as the GUI for the game. The challenging task is to create responsive buttons
and proper functionality to the game as per the rules of the game and also count the
score for the user to make the user feel more competitive towards the game. The end
goal is to generate a game with simpler mechanics and understanding in a way
everyone would be able to play the game.

Motivation : There are pre existing games when someone buys a laptop or a desktop.
The games are generally chess, solitaire and other games which require knowledge and
understanding of how the game works, what are the rules, how does one win the game.
So, for the issue , creation of a common game that everyone has once played in their
life and can be understood in all different parts of the world. The game requires barely
400kb to function on the work station and has a user friendly interface.

Methodology :

1. Initialization and Setup:


- The code begins by importing necessary libraries, including Pygame (`pygame`) for
handling game-related functionality.
- Pygame is initialized using `pg.init()`, and a game window with a resolution of
400x500 pixels is created.

2. Images and Buttons:


- The game uses images for the background, buttons, and choices (rock, paper,
scissor).
- Buttons for the main menu ("Start" and "Exit") and game buttons ("Rock," "Paper,"
and "Scissor") are defined as `Rect` objects.

3. Game Functions:
- Several functions are defined for specific purposes:
- `gameLogo()` and `gameName()`: Display the game logo and name.
- `backGround()`: Display the background image.
- `draw_buttons()`: Draw the main menu buttons.
- `draw_game_buttons()`: Draw the game buttons.
- `handle_button_click(pos)`: Determine which game button is clicked based on
mouse position.
- `display_result(result)`: Display the game result on the screen.
- `display_computer_choice(computer_choice)`: Display the computer's choice
during the game.
- `keep_scores()`: Display and update the player's score during the game.

4. Main Game Loop:


- The main game loop (`while running:`) continuously checks for events, such as
mouse clicks or quitting the game.
- Depending on the current game state ("menu" or "game"), different actions are
taken:
- In the "menu" state, the program checks for button clicks to start the game or exit.
- In the "game" state, the user's choice is determined by clicking one of the game
buttons. The computer's choice is randomly generated.
- The result of the game is displayed along with the computer's choice, and the
score is updated accordingly.

5. Display Updates:
- The screen is continuously updated to reflect changes in the game state, such as
drawing buttons, displaying results, and updating scores.

6. Exit Condition:
- The game loop continues until the user clicks the exit button or closes the game
window.

7. Cleanup:
- After the game loop exits, Pygame is quit, and the program terminates.

8. Scorekeeping:
- The game keeps track of the player's score, incrementing it when the player wins
and decrementing it when the player loses.
Raw Code :

import pygame as pg
import sys
import random

# initializes pygame
pg.init()

# creation of screen
screen = pg.display.set_mode((400, 500))
bg = pg.image.load('bg.jpg')
bg1 = 0
bg2 = 0
black = (0, 0, 0)
button_colour = (235, 231, 220)
start_button = pg.Rect(150, 250, 100, 50)
exit_button = pg.Rect(150, 350, 100, 50)

rock_image = pg.image.load('rock.jpg')
paper_image = pg.image.load('paper.jpg')
scissor_image = pg.image.load('scissor.jpg')
button_size = (80, 80)
rock_image = pg.transform.scale(rock_image, button_size)
paper_image = pg.transform.scale(paper_image, button_size)
scissor_image = pg.transform.scale(scissor_image, button_size)
rock_button = pg.Rect(10, 400, 80, 80)
paper_button = pg.Rect(165, 400, 80, 80)
scissor_button = pg.Rect(310, 400, 80, 80)

# Set up fonts
font = pg.font.Font(None, 36)

def gameLogo():
game_logo_image = pg.image.load('rps1_logo.jpg')
game_logo_rect = game_logo_image.get_rect()
game_logo_rect.center = (400 // 2, 690 // 4 - 50) # Adjusted the Y
position of the logo
screen.blit(game_logo_image, game_logo_rect)
def gameName():
info_text = font.render("ROCK! PAPER! SCISSOR!", True,
black)
info_rect = info_text.get_rect(center=(400 // 2, 890 // 4 - 50))
screen.blit(info_text, info_rect)

def backGround():
screen.blit(bg, (bg1, bg2))

def draw_buttons():
# Draw Start Button
pg.draw.rect(screen, button_colour, start_button)
pg.draw.rect(screen, black, start_button, 2)
start_text = font.render("Start", True, black)
screen.blit(start_text, (start_button.centerx - start_text.get_width()
// 2, start_button.centery - start_text.get_height() // 2))

# Draw Exit Button


pg.draw.rect(screen, button_colour, exit_button)
pg.draw.rect(screen, black, exit_button, 2)
exit_text = font.render("Exit", True, black)
screen.blit(exit_text, (exit_button.centerx - exit_text.get_width() //
2, exit_button.centery - exit_text.get_height() // 2))

def draw_game_buttons():
screen.blit(rock_image, rock_button.topleft)
screen.blit(paper_image, paper_button.topleft)
screen.blit(scissor_image, scissor_button.topleft)
info_text = font.render(" Rock Paper
Scissor", True, black)
info_rect = info_text.get_rect(center=(170,380))
screen.blit(info_text, info_rect)

def handle_button_click(pos):
if rock_button.collidepoint(pos):
return "rock" # Rock button clicked
elif paper_button.collidepoint(pos):
return "paper" # Paper button clicked
elif scissor_button.collidepoint(pos):
return "scissor" # Scissor button clicked
return 0 # No button clicked

def display_result(result):
pg.display.set_caption("Result")
result_text = font.render(result, True, black)
result_rect = result_text.get_rect(center=(200, 300))
screen.blit(result_text, result_rect)
pg.display.flip()
pg.time.delay(2000) # Display result for 2 seconds
pg.display.set_caption("ROCK! PAPER! SCISSOR!") # Reset window title

def display_computer_choice(computer_choice):
info_text = font.render("Computer chose", True, black)
info_rect = info_text.get_rect(center=(200, 100))
screen.blit(info_text, info_rect)

# Choose the appropriate image based on the computer's choice


if computer_choice == "rock":
computer_image = rock_image
elif computer_choice == "paper":
computer_image = paper_image
elif computer_choice == "scissor":
computer_image = scissor_image
else:
return # Invalid choice

# Display the computer's choice image below the text


computer_rect = computer_image.get_rect(center=(200, 180))
screen.blit(computer_image, computer_rect)

# Choose the appropriate image based on the computer's choice


if computer_choice == "rock":
computer_image = rock_image
elif computer_choice == "paper":
computer_image = paper_image
elif computer_choice == "scissor":
computer_image = scissor_image
else:
return # Invalid choice

# Display the computer's choice image


score=0
def keep_scores():
# Draw a box around the score
box_rect = pg.Rect(140, 10, 120, 50)
pg.draw.rect(screen, button_colour, box_rect)
pg.draw.rect(screen, black, box_rect, 2)

# Render and display the score text


score_text = font.render(f"Score: {score}", True, black)
score_rect = score_text.get_rect(center=(200, 35))
screen.blit(score_text, score_rect)

pg.display.update()

pg.display.set_caption("ROCK! PAPER! SCISSOR!")


icon = pg.image.load('RPS.png')
pg.display.set_icon(icon)

game_state = "menu" # Initial game state


user_choice = 0 # Variable to store the user's choice

# Game loop
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
elif event.type == pg.MOUSEBUTTONDOWN:
# Check if the mouse click is on the start button
if game_state == "menu" and
start_button.collidepoint(event.pos):
game_state = "game" # Change the game state to "game"
# Check if the mouse click is on the exit button
elif game_state == "menu" and
exit_button.collidepoint(event.pos):
running = False
# Check if a game button is clicked
elif game_state == "game":
user_choice = handle_button_click(event.pos)
print(f"User choice: {user_choice}")

# Simulate computer's choice


computer = random.choice(["rock", "paper", "scissor"])

# Display the computer's choice


display_computer_choice(computer)

# Determine the result


if (user_choice == "rock" and computer == "scissor") or
(user_choice == "paper" and computer == "rock") or (
user_choice == "scissor" and computer == "paper"):
result = "YOU WON!"
score=score+1
elif user_choice == computer:
result = "IT'S A TIE!"
else:
result = "YOU LOST!"
score=score-1

# Display the result


display_result(result)

# Clear the screen


backGround()
gameLogo()
gameName()

if game_state == "menu":
# Draw buttons
draw_buttons()
elif game_state == "game":
# Add your start game logic here
# Set the background to the game background, remove logo and
buttons
screen.blit(bg, (bg1, bg2))
# Draw game buttons
draw_game_buttons()
keep_scores()

# Update the display


pg.display.flip()

# Quit pygame and exit the program


pg.quit()
sys.exit()

Output :
On pressing the exit button, the program ends and the window gets closed.

After pressing the start button,


While playing the game,
On a tie, The score continues to remain zero.
On winning the round, the score got incremented by 1 point.

Potential Enhancements :
This is a basic user friendly game with minimal functionality as to provide its purpose of
enjoying the game as a user but there can be many advancements made in terms of
more appealing features such as highscore, leaderboard where the user can enter his
name and check himself on a leaderboard with other local users playing the game. The
preview or the looks of the game can also be altered in a way the game looks
aesthetically pleasing. A soothing music for the game could be a great add on for the
user to feel relaxed and enjoy the game more than before. These future improvements
can actually make the game more efficient and would satisfy the standards of the pre
existing games on the market.

Errors or bugs :

This game does not contain any errors or bugs since the game is in its initial stages with
minimal functionality. As the complexity increases, the chances of getting errors or bugs
shall also increase. A debugger with a great skill in python and Pygames can easily
identify the bugs and work out a solution on them as to keep the game user friendly and
increase its usability.

You might also like