Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button press Sound and some colors #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pygame
import pygame.mixer

# Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 300, 300
CELL_SIZE = 40
PADDING = 20
SCREEN_WIDTH, SCREEN_HEIGHT = 500, 500
CELL_SIZE = 60
PADDING = 25
ROWS = COLS = (SCREEN_WIDTH - 4 * PADDING) // CELL_SIZE

# Colors
Expand All @@ -20,7 +21,7 @@

win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
font = pygame.font.SysFont('cursive', 25)

button_sound = pygame.mixer.Sound('pick-92276.mp3')
# Define the Cell class to represent each cell in the grid
class Cell:
def __init__(self, row, col):
Expand Down Expand Up @@ -80,10 +81,23 @@ def reset_player():
fill_count, p1_score, p2_score = reset_score()
turn, players, current_player, next_turn = reset_player()

#Background color animation variables
color1 = [30, 30, 30]
color_increment = [1, 1, 1]
color_change_speed = 2

def animate_background():
global color1, color_increment
for i in range(3):
color1[i] += color_increment[i] * color_change_speed
if color1[i] >= 255 or color1[i] <= 0:
color_increment[i] *= -1


# Main game loop
running = True
while running:

animate_background()
win.fill(DARK_GRAY)

for event in pygame.event.get():
Expand All @@ -105,12 +119,16 @@ def reset_player():
elif not game_over:
if event.key == pygame.K_UP:
up = True
button_sound.play() # Play button press sound
elif event.key == pygame.K_RIGHT:
right = True
button_sound.play() # Play button press sound
elif event.key == pygame.K_DOWN:
bottom = True
button_sound.play() # Play button press sound
elif event.key == pygame.K_LEFT:
left = True
button_sound.play() # Play button press sound
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
up = False
Expand Down Expand Up @@ -177,12 +195,12 @@ def reset_player():
next_turn = False

# Display scores and current player
p1_img = font.render(f'{p1_score}', True, BLUE)
p2_img = font.render(f'{p2_score}', True, BLUE)
p1_img = font.render(f'{p1_score}', True, RED)
p2_img = font.render(f'{p2_score}', True, RED)

# Render player texts with appropriate positions
p1_text = font.render('Player 1:', True, BLUE)
p2_text = font.render('Player 2:', True, BLUE)
p1_text = font.render('Player 1:', True, (0, 255, 0))
p2_text = font.render('Player 2:', True, (0, 255, 0))

# Calculate positions for player texts and scores
p1_text_pos = (2 * PADDING, 15)
Expand All @@ -209,16 +227,16 @@ def reset_player():
overlay.set_alpha(200)
overlay.fill(BLACK)
win.blit(overlay, (0, 0))
over_img = font.render('Game Over', True,WHITE )
over_img = font.render('Game Over', True,GREEN )
winner_img = font.render(f'Player {1 if p1_score > p2_score else 2} Won', True, GREEN)
msg_img = font.render('Press R to restart, Q or ESC to quit', True, RED)
win.blit(over_img, ((SCREEN_WIDTH - over_img.get_width()) / 2, 100))
win.blit(winner_img, ((SCREEN_WIDTH - winner_img.get_width()) / 2, 150))
win.blit(msg_img, ((SCREEN_WIDTH - msg_img.get_width()) / 2, 200))

# Draw border
pygame.draw.rect(win, LIGHT_GRAY, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 2, border_radius=10)
pygame.draw.rect(win, RED, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 2, border_radius=10)

pygame.display.update()

pygame.quit()
pygame.quit()
Binary file added pick-92276.mp3
Binary file not shown.