Question
I'm making a pygame where the user(represented by a rocket) has to avoid crashing into obstacles(meteoroid) falling from the top of the screen by moving
I'm making a pygame where the user(represented by a rocket) has to avoid crashing into obstacles(meteoroid) falling from the top of the screen by moving sideways. This is the code I have right now, I have a start screen where the user presses any keys, the game will start and there are obstacles randomly falling from the top. How do I make my rocket move sideways (left and right) using keys and to end the game(present the end screen(I have a picture)) when it gets hit by one of the obstacles? PLEASE ANSWER WITH A CODE THAT WORKS ON PYTHON VER.2.7 AND NO SPRITES! Thank you.
My code:
import pygame # call on the library import random # to generate mobs randomly later # define global variables size = (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 screen screen = pygame.display.set_mode([width, height]) pygame.display.set_caption("SAVE OUR PLANET!") # set the window title clock = pygame.time.Clock() # adjust game speed (Frames Per Second) font_name = pygame.font.match_font('8bit_wonder') # call the downloaded font myfont = pygame.font.Font('8-BIT WONDER.TTF', 15) # adjust the font myfonttwo = 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 images background = 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()import pygame # call on the library import random # to generate mobs randomly later # define global variables (width, height) (0, 0, 0) size (600, 700) black = white (255, 255, 255) (0, 200, 50) brown= (139,115,85) red= (255, 64,64) green = # initialize pygame #pygame.init() pygame.font.init () # will import the downloaded text from the same folder # draw the screen screen pygame.display.set_caption ("SAVE OUR PLANET!") # set the window title pygame.display.set_mode ( [width, height]) clock = pygame.time.clock () # adjust game speed (Frames Per Second) font name = pygame.font.match font ('8bit wonder') # call the downloaded font myfont myfonttwo mob list= [] = pygame.font. Font ('8-BIT WONDER.TTF', 15) # adjust the font = pygame.font. Font ('8-BIT WONDER.TTF', 13) for i in range (9): x= random.randrange (0, 600) y= random.randrange (0,700) mob_list.append ( [x, y]) # call on the images background = pygame. image.load("bg.png").convert() earth = pygame. image.load("earth.png").convert() earthSmall = pygame.image.load("earthSmall.png").convert() player = pygame. image.load ("player.png").convert() rock = pygame. image.load("rock.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 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 # --- 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() import pygame # call on the library import random # to generate mobs randomly later # define global variables (width, height) (0, 0, 0) size (600, 700) black = white (255, 255, 255) (0, 200, 50) brown= (139,115,85) red= (255, 64,64) green = # initialize pygame #pygame.init() pygame.font.init () # will import the downloaded text from the same folder # draw the screen screen pygame.display.set_caption ("SAVE OUR PLANET!") # set the window title pygame.display.set_mode ( [width, height]) clock = pygame.time.clock () # adjust game speed (Frames Per Second) font name = pygame.font.match font ('8bit wonder') # call the downloaded font myfont myfonttwo mob list= [] = pygame.font. Font ('8-BIT WONDER.TTF', 15) # adjust the font = pygame.font. Font ('8-BIT WONDER.TTF', 13) for i in range (9): x= random.randrange (0, 600) y= random.randrange (0,700) mob_list.append ( [x, y]) # call on the images background = pygame. image.load("bg.png").convert() earth = pygame. image.load("earth.png").convert() earthSmall = pygame.image.load("earthSmall.png").convert() player = pygame. image.load ("player.png").convert() rock = pygame. image.load("rock.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 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 # --- 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()
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