Python Project
Python Project
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 :
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.
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))
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)
pg.display.update()
# 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}")
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()
Output :
On pressing the exit button, the program ends and the window gets closed.
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.