M Making Pygame User Represented Rocket Avoid Crashing Obstacles Meteoroid Falling Top Scr Q43837850
I’m making a pygame where the user(represented by a rocket) hasto avoid crashing into obstacles(meteoroid) falling from the top ofthe screen by moving sideways. This is the code I have right now, Ihave a start screen where the user presses any keys, the game willstart and there are obstacles randomly falling from the top. How doI make my rocket move sideways (left and right) using keys and toend the game when it gets hit by one of the obstacles.PLEASE ANSWER WITH A CODE THAT WORKS ON PYTHON VER.2.7 ANDPLEASE DONT USE SPRITES!! Thank you!!
My code:
import pygame # call on the libraryimport random # to generate mobs randomly later# define global variablessize = (width, height) = (600, 700)black = (0, 0, 0)white = (255, 255, 255)green = (0, 200, 50)brown= (139,115,85)red= (255,64,64)# initialize pygame#pygame.init()pygame.font.init() # will import the downloaded text from the same folder# draw the screenscreen = pygame.display.set_mode([width, height])pygame.display.set_caption(“SAVE OUR PLANET!”) # set the window titleclock = pygame.time.Clock() # adjust game speed (Frames Per Second)font_name = pygame.font.match_font(‘8bit_wonder’) # call the downloaded fontmyfont = pygame.font.Font(‘8-BIT WONDER.TTF’, 15) # adjust the fontmyfonttwo = pygame.font.Font(‘8-BIT WONDER.TTF’, 13)mob_list= []for i in range (9): x= random.randrange(0,600) y= random.randrange(0,700) mob_list.append([x,y])# call on the imagesbackground = pygame.image.load(“bg.png”).convert()earthSmall = pygame.image.load(“earthSmall.png”).convert()player = pygame.image.load(“player.png”).convert()title = pygame.image.load(“title.png”).convert()”””end= pygame.image.load(“end.png”).convert()”””#blip = pygame.mixer.Sound(“blip.wav”)#bang = pygame.mixer.Sound(“bang.wav”)# start dialogue function def startScreen(): screen.blit(earthSmall, (160, 100)) # earth graphic call screen.blit(title, (38, 400)) # title call pygame.display.flip() # update the screen # to terminate in case user closes the window def drawRoket(screen,x): pygame.draw.rect(screen, white, [250+x, 500, 50, 100]) pygame.draw.ellipse(screen,black,(260+x,510,30,30)) pygame.draw.polygon(screen, white,[[250+x,500],[275+x,450],[299+x,500]]) pygame.draw.polygon(screen, white,[[255+x,610],[275+x,560],[295+x,610]]) def drawMob(): for i in range (len(mob_list)): pygame.draw.circle(screen, brown, mob_list[i], 10) mob_list[i][1] += 1 if mob_list[i][1]>700: y=random.randrange(-50, -10) mob_list[i][1] = y x= random.randrange(0,600) mob_list[i][0] = x done=False start=False# ——– Main Program Loop ———–while not done: # — Main event loop for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if event.type == pygame.KEYUP: # if the user presses any key start=True # the game starts if start==False: startScreen() else: screen.blit(background,[0,0]) drawRoket(screen, x_coordinate) drawMob() pygame.display.flip() # — Game logic should go here # i.e calculations for positions, variable updates # First, clear the screen to white. Don’t put other drawing commands # above this, or they will be erased with this command. # — Drawing code should go here # — Go ahead and update the screen with what we’ve drawn. pygame.display.flip() # — Limit to 60 frames per second clock.tick(60) # Close the window and quit.# If you forget this line, the program will ‘hang’# on exit if running from IDLE.pygame.quit()
Expert Answer
Answer to I’m making a pygame where the user(represented by a rocket) has to avoid crashing into obstacles(meteoroid) falling from…
OR