Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help improving my code. It's a basic game where an object avoids stuff coming at it . It s a submarine avoiding a

I need help improving my code. It's a basic game where an object avoids stuff coming at it. Its a submarine avoiding a red cube. First, I need the object Im avoiding to look like a tire instead of a red cube. It should be a perfect circle drawn using pygame.draw not load image, It should look like a black donut and how tires normally look, not a full completely coloured in black circle. Secondly add an ending screen, like Good Try and then display their score and ask them to exit or retry. Third, make the game harder as time goes by, both by increasing the amound objects and the speed of them coming at you.
import sys
import random
import pygame
pygame.init()
WIDTH, HEIGHT =800,600
SUBMARINE_WIDTH, SUBMARINE_HEIGHT =50,30
OBSTACLE_WIDTH, OBSTACLE_HEIGHT =50,50
FPS =60
SUBMARINE_SPEED =5
WHITE, BLUE, RED, YELLOW, GRAY =(255,255,255),(0,0,255),(255,0,0),(255,255,0),(128,128,128)
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
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Submarine Game")
font = pygame.font.Font(None,36)
def draw_submarine():
pygame.draw.rect(screen, YELLOW, submarine_rect)
pygame.draw.rect(screen, GRAY, (submarine_rect.x + SUBMARINE_WIDTH //2-5, submarine_rect.y -10,10,10))
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))
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
obstacle_rect.x -=5
if obstacle_rect.right <0:
obstacle_rect.x = WIDTH
obstacle_rect.y = random.randint(0, HEIGHT - OBSTACLE_HEIGHT)
score +=1
if submarine_rect.colliderect(obstacle_rect):
print("Game Over!")
pygame.quit()
sys.exit()
screen.fill(BLUE)
draw_submarine()
draw_obstacle()
draw_score()
pygame.display.flip()
clock.tick(FPS)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

4.4 Summarize the components of a job description.

Answered: 1 week ago