Created
March 21, 2023 04:50
-
-
Save R3DHULK/3c1cfe269e3f7ea4f327f682239249ea to your computer and use it in GitHub Desktop.
GUI Based Frogger Game In Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pygame | |
| import random | |
| # Initialize Pygame | |
| pygame.init() | |
| # Set the window dimensions and create the window | |
| window_width = 600 | |
| window_height = 600 | |
| screen = pygame.display.set_mode((window_width, window_height)) | |
| # Set the game title | |
| pygame.display.set_caption('Frogger') | |
| # Define the colors | |
| frog_color = (0, 255, 0) | |
| log_color = (138, 51, 36) | |
| car_color = (0, 0, 255) | |
| # Define the font | |
| font = pygame.font.SysFont(None, 30) | |
| # Set the game speed | |
| clock = pygame.time.Clock() | |
| # Define the game variables | |
| frog_x = window_width // 2 - 25 | |
| frog_y = window_height - 50 | |
| frog_size = 50 | |
| frog_speed = 50 | |
| car_speed = 5 | |
| car_spawn_rate = 100 | |
| cars = [] | |
| log_speed = 3 | |
| log_spawn_rate = 50 | |
| logs = [] | |
| score = 0 | |
| game_over = False | |
| # Define the game loop | |
| while not game_over: | |
| # Handle events | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| game_over = True | |
| elif event.type == pygame.KEYDOWN: | |
| if event.key == pygame.K_UP: | |
| frog_y -= frog_speed | |
| elif event.key == pygame.K_DOWN: | |
| frog_y += frog_speed | |
| elif event.key == pygame.K_LEFT: | |
| frog_x -= frog_speed | |
| elif event.key == pygame.K_RIGHT: | |
| frog_x += frog_speed | |
| # Spawn new cars randomly | |
| if random.randint(0, car_spawn_rate) == 0: | |
| car_width = random.randint(50, 100) | |
| car_x = random.randint(-car_width, window_width) | |
| car_y = random.randint(200, 350) | |
| cars.append({'x': car_x, 'y': car_y, 'width': car_width, 'height': 50}) | |
| # Spawn new logs randomly | |
| if random.randint(0, log_spawn_rate) == 0: | |
| log_width = random.randint(50, 100) | |
| log_x = random.randint(-log_width, window_width) | |
| log_y = random.randint(100, 150) | |
| log_direction = random.choice(['left', 'right']) | |
| logs.append({'x': log_x, 'y': log_y, 'width': log_width, | |
| 'height': 50, 'direction': log_direction}) | |
| # Move the cars | |
| for car in cars: | |
| car['x'] += car_speed | |
| pygame.draw.rect(screen, car_color, | |
| (car['x'], car['y'], car['width'], car['height'])) | |
| # Check for collision with the frog | |
| if frog_y < car['y'] + car['height'] and frog_y + frog_size > car['y']: | |
| if frog_x + frog_size > car['x'] and frog_x < car['x'] + car['width']: | |
| # Frog collided with car, game over | |
| game_over = True | |
| # Draw the logs | |
| for log in logs: | |
| if log['direction'] == 'left': | |
| log['x'] -= log_speed | |
| if log['x'] + log['width'] < 0: | |
| log['x'] = window_width | |
| elif log['direction'] == 'right': | |
| log['x'] += log_speed | |
| if log['x'] > window_width: | |
| log['x'] = -log['width'] | |
| pygame.draw.rect(screen, log_color, | |
| (log['x'], log['y'], log['width'], log['height'])) | |
| # Check if the frog is on the log | |
| if frog_y < log['y'] + log['height'] and frog_y + frog_size > log['y']: | |
| if log['direction'] == 'left': | |
| frog_x -= log_speed | |
| elif log['direction'] == 'right': | |
| frog_x += log_speed | |
| # Check for collision with the water | |
| if frog_y < 350: | |
| # Frog fell in water, game over | |
| game_over = True | |
| # Draw the frog | |
| pygame.draw.rect(screen, frog_color, | |
| (frog_x, frog_y, frog_size, frog_size)) | |
| # Draw the score | |
| score_text = font.render('Score: {}'.format(score), True, (255, 255, 255)) | |
| screen.blit(score_text, (10, 10)) | |
| # Update the screen | |
| pygame.display.update() | |
| # Check if the frog made it to the other side | |
| if frog_y < 100: | |
| # Frog made it to the other side, increase the score and reset the frog position | |
| score += 10 | |
| frog_x = window_width // 2 - frog_size // 2 | |
| frog_y = window_height - frog_size | |
| # Set the game speed | |
| clock.tick(30) | |
| # Game over, show the final score | |
| final_score_text = font.render( | |
| 'Final Score: {}'.format(score), True, (255, 255, 255)) | |
| screen.blit(final_score_text, (window_width // | |
| 2 - 100, window_height // 2 - 15)) | |
| pygame.display.update() | |
| # Wait for 3 seconds before closing the window | |
| pygame.time.wait(3000) | |
| # Quit Pygame | |
| pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment