Question: I need help improving my code. It's a basic game where an object avoids stuff coming at it . I need to improve the graphics

I need help improving my code. It's a basic game where an object avoids stuff coming at it. I need to improve the graphics using the same method i have been using to draw, I can't load an image. A submarine is the main object that will be avoiding stuff coming at. The objects coming at it should be a tire, a fish/shark, reef coming out of the bottom of the screen, anything thing else you think will look good. Try your best to create the best graphics you can. For the game logic, it's all done except just make it go faster as you go on. Please use what i've been using and try your best to explain it to me and paste the final code aswell. import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT =800,600
SUBMARINE_WIDTH, SUBMARINE_HEIGHT =50,30
OBSTACLE_WIDTH, OBSTACLE_HEIGHT =50,50
FPS =60
SUBMARINE_SPEED =5
# Colors
WHITE =(255,255,255)
BLUE =(0,0,255)
RED =(255,0,0)
# Game variables
submarine_rect = pygame.Rect(WIDTH //2- SUBMARINE_WIDTH //2, HEIGHT //2- SUBMARINE_HEIGHT //2, SUBMARINE_WIDTH, SUBMARINE_HEIGHT)
obstacle_rect = pygame.Rect(WIDTH, random.randint(0, HEIGHT - OBSTACLE_HEIGHT), OBSTACLE_WIDTH, OBSTACLE_HEIGHT)
score =0
# Create the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Submarine Game")
# Fonts
font = pygame.font.Font(None,36)
# Functions
def draw_submarine():
pygame.draw.rect(screen, BLUE, submarine_rect)
def draw_obstacle():
pygame.draw.rect(screen, RED, obstacle_rect)
def draw_score():
score_text = font.render("Score: "+ str(score), True, WHITE)
screen.blit(score_text, (10,10))
# Game loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and submarine_rect.top >0:
submarine_rect.y -= SUBMARINE_SPEED
if keys[pygame.K_DOWN] and submarine_rect.bottom < HEIGHT:
submarine_rect.y += SUBMARINE_SPEED
# Update obstacle position
obstacle_rect.x -=5
if obstacle_rect.right <0:
obstacle_rect.x = WIDTH
obstacle_rect.y = random.randint(0, HEIGHT - OBSTACLE_HEIGHT)
score +=1
# Check for collisions
if submarine_rect.colliderect(obstacle_rect):
print("Game Over!")
pygame.quit()
sys.exit()
# Draw everything
screen.fill(WHITE)
draw_submarine()
draw_obstacle()
draw_score()
# Update display
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!