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, anything thing else you think will look good. Try your best to create the best graphics you can. The game logic is all good. Take your time drawing it, it should look realistic, change the background to blue for water. I want the tire to actually look like a tire and not just a red blob. A submarine is hard to draw but use a draw a yellow sideways oval and a gray pipe sticking out the top. Google a submarine to imagine what it looks like. Draw a fish so you actually know its a fish not just a gray block. Im really emphasizing how important it is to take your time, this will take time and if you try to rush it, it will come out bad. As realistic as possible using pygame.draw and using different shapes if you can.
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!