GPP 5
GPP 5
GPP 5
Practical No.5
Description :
Game Elements :
Background.jpg
T110 Rugved Shetge
Image/Element Source :
Character : https://www.freepik.com/free-vector/jet-fighter-plane-flying-
illustration_11645877.htm#query=cartoon%20fighter
%20jet&position=1&from_view=keyword&track=ais_hybrid&uuid=bd90c032-
3eae-48cb-824b-da4cef97dae4#position=1&query=cartoon%20fighter%20jet
Enemy : https://www.vecteezy.com/vector-art/6369513-military-fighter-jet-flat-
icon-with-premium-download
Bullet : https://www.shutterstock.com/image-vector/cartoon-game-objects-2d-
art-object-656359261
Heart : https://www.pngwing.com/en/free-png-zrrvr/download
Background : https://www.pxfuel.com/en/desktop-wallpaper-edcjz
T110 Rugved Shetge
Code :
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen setup
width, height = 600, 750
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("T110 Rugved Shetge")
heart_size = 30
heart_img = pygame.transform.scale(heart_img, (heart_size, heart_size))
def game_over_screen(final_score):
"""Display game over screen with restart and quit buttons."""
screen.fill((0, 0, 0))
display_message("Game Over", 72, (255, 0, 0))
display_message(f"Your Score: {final_score}", 48, (255, 255, 255), 60)
display_message("Press R to Restart", 36, (255, 255, 255), 120)
display_message("Press Q to Quit", 36, (255, 255, 255), 180)
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
waiting = False
return True
if event.key == pygame.K_q:
pygame.quit()
return False
clock.tick(60)
T110 Rugved Shetge
def reset_game():
"""Reset the game state."""
global character, bullets, enemies, score, hearts
character = pygame.Rect(
width // 2 - character_size // 2,
height - character_size - 10,
character_size - 20, # Adjusted hitbox width
character_size - 20 # Adjusted hitbox height
)
bullets = []
enemies = []
score = 0
hearts = 3
# Move bullets
for bullet in bullets:
bullet.y -= 10
T110 Rugved Shetge
if bullet.top < 0:
bullets.remove(bullet)
# Spawn enemies
if random.randint(1, 100) <= 2:
enemy_x = random.randint(enemy_radius, width - enemy_radius)
enemy = pygame.Rect(enemy_x - enemy_radius, 0, enemy_radius * 2, enemy_radius *
2)
enemies.append(enemy)
# Move enemies
for enemy in enemies:
enemy.y += 5
if enemy.top > height:
enemies.remove(enemy)
# Draw everything
screen.blit(background_img, (0, 0)) # Draw background
# Draw bullets
for bullet in bullets:
screen.blit(bullet_img, bullet.topleft)
# Draw character
screen.blit(character_img, character.topleft)
# Draw enemies
for enemy in enemies:
screen.blit(enemy_img, enemy.topleft)
T110 Rugved Shetge
# Display score
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, (255, 0, 0))
screen.blit(score_text, (10, 10))
# Display hearts
for i in range(hearts):
screen.blit(heart_img, (width - (i + 1) * (heart_size + 10), 10))
# Update display
pygame.display.flip()
# Clean up
pygame.quit()
T110 Rugved Shetge
Output :