Answered step by step
Verified Expert Solution
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
SUBMARINEWIDTH, SUBMARINEHEIGHT
OBSTACLEWIDTH, OBSTACLEHEIGHT
FPS
SUBMARINESPEED
WHITE, BLUE, RED, YELLOW, GRAY
submarinerect pygame.RectWIDTH SUBMARINEWIDTH HEIGHT SUBMARINEHEIGHT SUBMARINEWIDTH,
SUBMARINEHEIGHT
obstaclerect pygame.RectWIDTH random.randint HEIGHT OBSTACLEHEIGHT OBSTACLEWIDTH, OBSTACLEHEIGHT
score
screen pygame.display.setmodeWIDTH HEIGHT
pygame.display.setcaptionSubmarine Game"
font pygame.font.FontNone
def drawsubmarine:
pygame.draw.rectscreen YELLOW, submarinerect
pygame.draw.rectscreen GRAY, submarinerect.x SUBMARINEWIDTH submarinerect.y
def drawobstacle:
pygame.draw.rectscreen RED, obstaclerect
def drawscore:
scoretext font.renderScore: strscore True, WHITE
screen.blitscoretext,
clock pygame.time.Clock
while True:
for event in pygame.event.get:
if event.type pygame.QUIT:
pygame.quit
sysexit
keys pygame.key.getpressed
if keyspygameKUP and submarinerect.top :
submarinerect.y SUBMARINESPEED
if keyspygameKDOWN and submarinerect.bottom HEIGHT:
submarinerect.y SUBMARINESPEED
obstaclerect.x
if obstaclerect.right :
obstaclerect.x WIDTH
obstaclerect.y random.randint HEIGHT OBSTACLEHEIGHT
score
if submarinerect.colliderectobstaclerect:
printGame Over!"
pygame.quit
sysexit
screen.fillBLUE
drawsubmarine
drawobstacle
drawscore
pygame.display.flip
clock.tickFPS
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started