def check_if_player_lost(self): """ Checks if player lost (hp <= 0) and if so brings lose menu up """ if config.PLAYER.creature.stat.hp <= 0: self.playing = False menu.lose_menu()
def run_game(): # function to run the game """game play interface""" screen = pygame.display.set_mode( (800, 600)) # set the height and width of the screen display.set_caption("Jet mission") # set the title of the game scores = 0 # set the score start from 0 theClock = pygame.time.Clock() # object to track time bg_image = Star_bg( "star.gif") # insert the star.gif image for the background #coordinate of moving background x = 0 # set the coordinate x start from 0 y = 0 # set the coordinate y start from 0 x1 = bg_image.width # x1 is equal to background width y1 = 0 # y1 is equal to 0 pygame.init() # initialize pygame #creating a jet : jet1 = Jet(screen) # make an object called jet1 from Jet class Jet_sprites = Group(jet1) #create asteroid object group asteroid_group = Group() #create bullets object Group bullets = Group() Fps = 40 # set fps to 40 asteroid_timer = pygame.time.get_ticks() # get time for the asteroid_timer while True: # if the condition is True loop it theClock.tick(Fps) Fps += 0.01 # game phase goes faster after every frame """background move""" x -= 5 # for moving the background backward x1 -= 5 # for moving the background backward bg_image.draw( screen, x, y) # show the background image according to x,y coordinate bg_image.draw( screen, x1, y1) # show the background image according to x1,y1 coordinate if x < -bg_image.width: # if x is less than minus background width, run the code x = 0 if x1 < 0: # if x1 is less than 0, run the code x1 = bg_image.width # create score board font = pygame.font.SysFont("Times New Romans", 36) # set the font and the font size score_board = font.render( "score:" + str(scores), True, (255, 255, 255)) # update the score , set color to white # update refered to the word's method screen.blit(score_board, (10, 550)) # draw the socre board in game Jet_sprites.draw(screen) # draw the jet in game bullets.draw(screen) # draw the bullet asteroid_group.draw(screen) # draw the asteroid display.update() # update jet and screen view event.get() # get the event """moving the jet according to key pressed""" key = pygame.key.get_pressed( ) # make a variable for pygame.key.get_pressed if key[K_LEFT] and jet1.rect.x > 0: # if key left and jet1.rect x is greater than 0, run the code jet1.moveleft() # move the jet to left if key[K_RIGHT] and jet1.rect.x <= 700: # if key right and jet1.rect.x is less than 700, run the code jet1.moveright() # move the jet to right if key[K_DOWN] and jet1.rect.y <= 500: # if key down and jet1.rect.y is less than 500, run the code jet1.movedown() # move it down if key[K_UP] and jet1.rect.y > 0: # if key up and jet1.rect.y is greater than 0, run the code jet1.moveup() # move it up if key[K_SPACE] and len(bullets) <= jet1.firerates + (scores / 4000): bullet = Bullet( screen, jet1.rect.x + 50, jet1.rect.y + 42) # to set the bullet position based on jet position bullets.add(bullet) # add bullets pygame.mixer.music.load( "LaserBlast.wav") # set the sound when you shoot the bullet pygame.mixer.music.play( ) # play the sound when you shoot the bullet if key[K_ESCAPE]: # if space is pressed, run the code menu.menu_screen(Button, run_game) # end the game if key[K_p]: # if p is pressedn, run the code menu.pause_menu(Button, run_game) # paused the game """generate asteroid randomly""" if pygame.time.get_ticks() - asteroid_timer >= 200: asteroid = Asteroid(screen, 50, 50, random.randint(1, 4) * 6, 800, (random.randint(1, 28) * 20)) # spawn the asteroid randomly asteroid_group.add(asteroid) # add asteroid asteroid_timer = pygame.time.get_ticks( ) # set time to spawn the asteroid """update the movement of asteroid""" for asteroid in asteroid_group: asteroid.movement() if asteroid.rect.right <= 0: # if asteroid.rect.right is equal or less than 0, run the code asteroid_group.remove(asteroid) # remove after screen if groupcollide(Jet_sprites, asteroid_group, dokilla=True, dokillb=True): # condition check menu.lose_menu(Button, run_game, scores) """update bullet movement on screen""" for bullet in bullets: bullet.movement() if bullet.rect.left > 800: # if bullet.rect.left is greater than 800 bullets.remove(bullet) # remove the bullet from the screen if groupcollide(bullets, asteroid_group, dokilla=True, dokillb=True): # if the bullet hit the asteroid scores += 100 # add score 100
def run_game(): # create screen resolution screen = pygame.display.set_mode((800, 600)) # create program name display.set_caption("Dank Mission") # initialize score scores = 0 # initialize all imported pygame modules pygame.init() # create jet object group jet1 = Jet(screen) Jet_sprites = Group(jet1) # create asteroid object group asteroid_group = Group() # create bullets object Group bullets = Group() # create how fast the game phase theClock = pygame.time.Clock() Fps = 40 asteroid_timer = pygame.time.get_ticks() while True: theClock.tick(Fps) Fps += 0.01 # game phase goes faster after every frame # create score board font=pygame.font.Font("Minecraft.ttf",36) # create font score_board=font.render("score:"+str(scores),True,(255,255,255)) # update referred to the word's method screen.blit(score_board,(10,550)) # load bg image for game screen.fill(pygame.Color(15, 77, 143)) # create jet Jet_sprites.draw(screen) # create bullets bullets.draw(screen) # create asteroid asteroid_group.draw(screen) # update current frame with all the above changes display.update() # get events from the queue pygame.event.get() """moving the jet according to key pressed""" key = pygame.key.get_pressed() # LEFT if key[K_LEFT] and jet1.rect.x>0: jet1.moveleft() # RIGHT if key[K_RIGHT] and jet1.rect.x<=700: jet1.moveright() # DOWN if key[K_DOWN] and jet1.rect.y<=500: jet1.movedown() # UP if key[K_UP] and jet1.rect.y>0: jet1.moveup() # SPACE if key[K_SPACE] and len(bullets) <= jet1.firerates+(scores/4000): bullet = Bullet(screen, jet1.rect.x+50, jet1.rect.y+42) bullets.add(bullet) pygame.mixer.music.load("LaserBlast.wav") pygame.mixer.music.play() # ESC if key[K_ESCAPE]: menu.pause_menu(Button,run_game) # P if key[K_p]: menu.pause_menu(Button,run_game) """generate asteroid randomly""" if pygame.time.get_ticks() - asteroid_timer >= 200: asteroid = Asteroid(screen, 50, 50, random.randint(1,4)*6, 800, (random.randint(1,28) * 20)) asteroid_group.add(asteroid) asteroid_timer = pygame.time.get_ticks() """update the movement of asteroid""" for asteroid in asteroid_group: asteroid.movement() if asteroid.rect.right <= 0: asteroid_group.remove(asteroid) #remove after screen if groupcollide(Jet_sprites,asteroid_group,dokilla=True,dokillb=True):#collition check menu.lose_menu(Button,run_game,scores) """update bullet movement on screen""" for bullet in bullets: bullet.movement() if bullet.rect.left > 800: bullets.remove(bullet) if groupcollide(bullets,asteroid_group,dokilla=True,dokillb=True): scores += 100 pygame.mixer.music.load('oof.mp3') pygame.mixer.music.play(0)
def run_game(): #define the run_game #game play interface screen = pygame.display.set_mode( (800, 600)) #this is for the screen width and height display.set_caption("SPACE COW") #set the title for the gam scores = 0 #set the current score when you start the game which is Zero theClock = pygame.time.Clock() #create an object to help track time bg_image = Star_bg("star.gif") #to call the star picture into the game x = 0 y = 0 x1 = bg_image.width y1 = 0 #make the coordinate of moving background pygame.init() #initialize all imported pygame modules cow1 = Cow(screen) Cow_sprites = Group(cow1) #creating a jet asteroid_group = Group() #create asteroid object group bullets = Group() #create bullets object Group Fps = 120 asteroid_timer = pygame.time.get_ticks( ) #get the time in milliseconds for the asteroid_timer while True: #make a while loop theClock.tick( Fps ) #sets up how fast game should run or how often while loop should update itself, run through itself. Fps += 0.01 #game phase goes faster after every frame x -= 5 x1 -= 5 bg_image.draw(screen, x, y) bg_image.draw(screen, x1, y1) if x < -bg_image.width: x = 0 if x1 < 0: #spawn the asteroid from the right x1 = bg_image.width font = pygame.font.SysFont("", 36) #input the score in 36fontsize score_board = font.render( "score:" + str(scores), True, (255, 255, 255) ) #to input the score inside the game in white color and add "score:" before it screen.blit(score_board, (10, 550)) # update refered to the word's method Cow_sprites.draw(screen) #put the space cow inside the screen game bullets.draw(screen) #put the bullets inside the screen game asteroid_group.draw(screen) #put the asteroid inside the screen game display.update() #update cow and screen view event.get() #summon and get the event from other file #moving the cow according to key pressed key = pygame.key.get_pressed( ) #make key variable for what hotkey we will press while playing the game if key[K_LEFT] and cow1.rect.x > 0: #if you press left key, and the cow is not over the left screen which is x = 0 in the screen width, the cow will move to the left cow1.moveleft() if key[K_RIGHT] and cow1.rect.x <= 550: #if you press right key, and the cow is not over the right screen which is x = 700 in the screen width because the half of the cow picture is 100 width, cow1.moveright() # then the cow will move to the right if key[K_DOWN] and cow1.rect.y <= 450: #if you press down key, and the cow is not over the bottom screen which is y=500, the cow will move down cow1.movedown() if key[K_UP] and cow1.rect.y > 0: #if you press up key, and the cow is no over the top screen which is y = 0, the cow will move up cow1.moveup() if key[K_SPACE] and len(bullets) <= cow1.firerates + ( scores / 4000 ): #if you press space and total of the bullet in game game is not bigger than the cow firerate + score/4000 bullet = Bullet( screen, cow1.rect.x + 150, cow1.rect.y + 42) #set the bullet spawn place when you press space bullets.add(bullet) #add the bullet spawn place to the bullets pygame.mixer.music.load( "LaserBlast.wav" ) #to add the sound everytime you shot the bullet pygame.mixer.music.play() #to play the sound if key[K_ESCAPE]: #if you press escape key, it will go to main menu screen and call the Button class and run_game so you can run the game again menu.menu_screen(Button, run_game) if key[K_p]: #if you press p key, it will go to pause menu screen and call the Button class and run_game so you can run the game again menu.pause_menu(Button, run_game) #generate asteroid randomly if pygame.time.get_ticks( ) - asteroid_timer >= 200: #if the millisecond time - the asteroid timer is bigger or equal to 200 asteroid = Asteroid( screen, 50, 50, random.randint(1, 4) * 6, 800, (random.randint(1, 28) * 20) ) #make variable asteroid and set the spawn place which is random by using random.randint asteroid_group.add( asteroid) #add the variable asteroid to the asteroid_group asteroid_timer = pygame.time.get_ticks( ) #get the time in milliseconds for the asteroid_timer #update the movement of asteroid for asteroid in asteroid_group: #make asteroid loop in the asteroid_group asteroid.movement() #movement for the asteroid if asteroid.rect.right <= 0: #if the asteroid goes beyond the left screen asteroid_group.remove(asteroid) #the asteroid will be removed if groupcollide(Cow_sprites, asteroid_group, dokilla=True, dokillb=True): #collition check menu.lose_menu( Button, run_game, scores ) #if the asteroid touch your space cow, the game will go to the lose menu and call Button class, run_game to play again, and scores to reveal your score #update bullet movement on screen for bullet in bullets: #make bullet loop in the bullets(variable from classes file) bullet.movement() #movement for the bullet if bullet.rect.left > 800: #if the bullet goes beyond the right screen, the bullet will be removed bullets.remove(bullet) if groupcollide(bullets, asteroid_group, dokilla=True, dokillb=True): #collition check scores += 100 #if the bullet touch the asteroid, your score will be up by 100
def rungame(): #Gameplay screen resolution and caption title screen = pygame.display.set_mode((800, 600)) display.set_caption("Jet mission") #starting scores and background image scores = 0 theClock = pygame.time.Clock() bg_image = Star_bg("star.gif") #The starting coordinate of the moving background x = 0 y = 0 x1 = bg_image.width y1 = 0 #initiate pygame pygame.init() #Creating the jet jet1 = Jet(screen) Jet_sprites = Group(jet1) #Create group for the moving asteroid asteroid_group = Group() #Create group for shooting bullets bullets = Group() #Initial Fps and that it will be faster after every frame Fps = 40 asteroid_timer = pygame.time.get_ticks() while True: theClock.tick(Fps) Fps += 0.01 #Moving background x -= 5 x1 -= 5 bg_image.draw(screen,x,y) bg_image.draw(screen,x1, y1) if x < -bg_image.width: x = 0 if x1 < 0: x1 = bg_image.width #Making The score board with Font style and size font=pygame.font.SysFont("Times New Romans",36) score_board=font.render("score:"+str(scores),True,(255,255,255)) screen.blit(score_board,(10,550)) #Draws the jet Jet_sprites.draw(screen) #Draws the bullets bullets.draw(screen) #Draws the moving asteroid asteroid_group.draw(screen) #update jet when the game is run display.update() event.get() #Controls to move the jet and fire bullets key = pygame.key.get_pressed() if key[K_LEFT] and jet1.rect.x>0: jet1.moveleft() if key[K_RIGHT] and jet1.rect.x<=700: jet1.moveright() if key[K_DOWN] and jet1.rect.y<=500: jet1.movedown() if key[K_UP] and jet1.rect.y>0: jet1.moveup() if key[K_SPACE] and len(bullets) <= jet1.firerates+(scores/4000): bullet = Bullet(screen, jet1.rect.x+50, jet1.rect.y+42) bullets.add(bullet) pygame.mixer.music.load("LaserBlast.wav") pygame.mixer.music.play() if key[K_ESCAPE]: menu.menu_screen(Button,rungame) if key[K_p]: menu.pause_menu(Button,rungame) #Creating the asteroid randomly on the screen and moves if pygame.time.get_ticks() - asteroid_timer >= 200: asteroid = Asteroid(screen, 50, 50, random.randint(1,4)*6, 800, (random.randint(1,28) * 20)) asteroid_group.add(asteroid) asteroid_timer = pygame.time.get_ticks() #Updating the movement of the asteroids for asteroid in asteroid_group: asteroid.movement() if asteroid.rect.right <= 0: asteroid_group.remove(asteroid) #remove after screen if groupcollide(Jet_sprites,asteroid_group,dokilla=True,dokillb=True):#collition check menu.lose_menu(Button,rungame,scores) #Updating the bullets on screen for bullet in bullets: bullet.movement() if bullet.rect.left > 800: bullets.remove(bullet) if groupcollide(bullets,asteroid_group,dokilla=True,dokillb=True): scores += 100
def run_game( ): #to define run_game, so the code can be call when using "run_game" code """game play interface""" screen = pygame.display.set_mode( (800, 600)) #size of the game's window display pygame.display.set_caption("Jet mission") #to set the name of the game scores = 0 #to set the score on the game theClock = pygame.time.Clock() #to track the time bg_image = Star_bg("star.gif") #to put the background of the game #coordinate of moving background x = 0 y = 0 x1 = bg_image.width y1 = 0 pygame.init() #creating a jet jet1 = Jet(screen) Jet_sprites = Group(jet1) #create asteroid object group asteroid_group = Group() #create bullets object Group bullets = Group() Fps = 40 asteroid_timer = pygame.time.get_ticks( ) #to set the time for asteroid_timer while True: #loop so it can run it agian theClock.tick(Fps) #to sets the game's speed to run Fps += 0.01 #game phase goes faster after every frame """background move""" x -= 5 x1 -= 5 bg_image.draw(screen, x, y) bg_image.draw(screen, x1, y1) if x < -bg_image.width: x = 0 if x1 < 0: #spawning the asteroid x1 = bg_image.width # create score board font = pygame.font.SysFont("Times New Romans", 36) #the size and type of the font score_board = font.render( "score:" + str(scores), True, (255, 255, 255)) #to input the color for the "score" and "score:" # update refered to the word's method screen.blit(score_board, (10, 550)) #put where the score will show # Jet_sprites.draw(screen) #to input the jet to inside the game bullets.draw(screen) #to input the bullet to inside the game asteroid_group.draw(screen) #to input the asteroid to inside the game display.update() #update jet and screen view event.get() #to summon and get the event from others files """moving the jet according to key pressed""" key = pygame.key.get_pressed() #to make the key for playing the game if key[K_LEFT] and jet1.rect.x > 0: #make the jet go to the left side by pressing the left key and will not disappear when going to the over left jet1.moveleft() if key[K_RIGHT] and jet1.rect.x <= 700: #make the jet go to the right side by pressing the right key and will not disappear when going to the over right jet1.moveright() if key[K_DOWN] and jet1.rect.y <= 500: #make the jet go to the down side by pressing the down key and will not disappear when going to the over down jet1.movedown() if key[K_UP] and jet1.rect.y > 0: #make the jet go to the up side by pressing the up key and will not disappear when going to the over up jet1.moveup() if key[K_SPACE] and len(bullets) <= jet1.firerates + ( scores / 4000 ): #press space key to call the bullet and and the total of the bullets are not bigger than the jet1 firerate bullet = Bullet(screen, jet1.rect.x + 50, jet1.rect.y + 42) bullets.add(bullet) #to add the place for bullets spawn pygame.mixer.music.load( "LaserBlast.wav") #to make a sound everytime the bullet spawn pygame.mixer.music.play() #to run and play the sound if key[K_ESCAPE]: #to make the game back to the fist show which is the menu when pressing escape key menu.menu_screen(Button, run_game) if key[K_p]: #to stop the the game for a while by pressing p key menu.pause_menu(Button, run_game) """generate asteroid randomly""" if pygame.time.get_ticks( ) - asteroid_timer >= 200: #asteroid timer is bigger than 200 or equal with 200 asteroid = Asteroid(screen, 50, 50, random.randint(1, 4) * 6, 800, (random.randint(1, 28) * 20)) asteroid_group.add( asteroid) #add the asteroid to the asteroid group asteroid_timer = pygame.time.get_ticks( ) #get the time in pygame.time.get_ticks() for asteroid_timer """update the movement of asteroid""" for asteroid in asteroid_group: #looping asteroid in the asteroid_group asteroid.movement() #thw movement of the asteroid if asteroid.rect.right <= 0: #the asteroid disappear when arrive the left screen asteroid_group.remove(asteroid) #remove after screen if groupcollide(Jet_sprites, asteroid_group, dokilla=True, dokillb=True): #collition check menu.lose_menu( Button, run_game, scores ) #when the asteroid touch the jet the game will end and show the lose menu and shoing the score that you get """update bullet movement on screen""" for bullet in bullets: #looping the bullet in bullets bullet.movement() if bullet.rect.left > 800: #the bullet will disappear if the bullet reach the right side of the screen bullets.remove(bullet) if groupcollide(bullets, asteroid_group, dokilla=True, dokillb=True): scores += 100 #when everytime the bullet touch the asteroid, the score will increase 100
def run_game(): """game play interface""" screen = pygame.display.set_mode( (800, 600)) #the widht of the display for the game display.set_caption("Jet mission") #set the title of the game scores = 0 #the score starts from 0 theClock = pygame.time.Clock() bg_image = Star_bg("star.gif") #call the star image into the game #coordinate of moving background x = 0 y = 0 x1 = bg_image.width y1 = 0 pygame.init() #initialize pygame #creating a jet jet1 = Jet(screen) Jet_sprites = Group(jet1) #create asteroid object group asteroid_group = Group() #create bullets object Group bullets = Group() Fps = 60 asteroid_timer = pygame.time.get_ticks() while True: theClock.tick(Fps) Fps += 0.01 #game phase goes faster after every frame """background move""" x -= 5 x1 -= 5 bg_image.draw(screen, x, y) bg_image.draw(screen, x1, y1) #spawn the asteroids if x < -bg_image.width: x = 0 if x1 < 0: x1 = bg_image.width # create score board font = pygame.font.SysFont("Times New Romans", 36) score_board = font.render("score:" + str(scores), True, (255, 255, 255)) # update refered to the word's method screen.blit(score_board, (10, 550)) Jet_sprites.draw(screen) #to make the jet in the screen bullets.draw(screen) #to make the bullets in the screen asteroid_group.draw(screen) #to make the asteroid in the screen display.update() #update jet and screen view event.get() """moving the jet according to key pressed""" key = pygame.key.get_pressed() if key[K_LEFT] and jet1.rect.x > 0: #moves the jet to the left, and if it is not over the screen jet1.moveleft() #moves the jet to the left if key[K_RIGHT] and jet1.rect.x <= 700: #moves the jet to the right, and if it is not over the right screen jet1.moveright() if key[K_DOWN] and jet1.rect.y <= 500: #moves the jet downwards, and if it is not over the screen jet1.movedown() if key[K_UP] and jet1.rect.y > 0: #moves the jet upwards, and if it is not over the screen jet1.moveup() if key[K_SPACE] and len(bullets) <= jet1.firerates + (scores / 4000): bullet = Bullet( screen, jet1.rect.x + 50, jet1.rect.y + 42) #set the bullets spawning place when space is pressed bullets.add(bullet) #add the bullets pygame.mixer.music.load( "LaserBlast.wav") #make a sound every time you shoot pygame.mixer.music.play() #play the sound if key[K_ESCAPE]: menu.menu_screen( Button, run_game ) #press escape to go back to the menu screen, and run the main menu to start the game again if wanted if key[K_p]: menu.pause_menu( Button, run_game) #press p to pause the game, and run the pause menu """generate asteroid randomly""" if pygame.time.get_ticks() - asteroid_timer >= 200: asteroid = Asteroid( screen, 50, 50, random.randint(1, 4) * 6, 800, (random.randint(1, 28) * 20)) #set the spawn place for the asteroid random asteroid_group.add( asteroid) #add the variable asteroid to asteroid group asteroid_timer = pygame.time.get_ticks() """update the movement of asteroid""" for asteroid in asteroid_group: asteroid.movement() #movement for asteroids if asteroid.rect.right <= 0: #if the asteroid moves to the left of the screen and didnt hit asteroid_group.remove(asteroid) #remove after screen if groupcollide(Jet_sprites, asteroid_group, dokilla=True, dokillb=True): #collition check menu.lose_menu( Button, run_game, scores ) #if the jet touches the asteroid, it goes to the lose menu and shows the score and ask if you want to play again """update bullet movement on screen""" for bullet in bullets: bullet.movement() #movements for bullets if bullet.rect.left > 800: #if the bullet goes further than the screen of the right, it is removed bullets.remove(bullet) #removes bullet if groupcollide(bullets, asteroid_group, dokilla=True, dokillb=True): scores += 100 #if the bullet touches the asteroid, the score will be up by 100
def run_game(): # function for running the game """game play interface""" screen = pygame.display.set_mode( (1000, 1000)) # the code for running the game display.set_caption("Jet mission") # setting the title for the game scores = 0 # set the original score to 0 theClock = pygame.time.Clock() # create an object to track time bg_image = Star_bg( "star.gif") # To the star picture to be the background in the game #coordinate of moving background x = 0 # coordinate start from x = 0 y = 0 # coordinate start from y = 0 x1 = bg_image.width y1 = 0 pygame.init() # Initialize the pygame modules #creating a jet jet1 = Jet(screen) #Creating the object jet1 from the class Jet Jet_sprites = Group(jet1) #creating the group #create asteroid object group asteroid_group = Group() #create bullets object Group bullets = Group() Fps = 200 # for setting the fps of the game asteroid_timer = pygame.time.get_ticks( ) # get the time for the asteroid_timer while True: # while the condition is true (Looping) theClock.tick( Fps) # set ups how often the while loop should update each self Fps += 0.01 #game phase goes faster after every frame """background move""" x -= 5 x1 -= 5 bg_image.draw(screen, x, y) bg_image.draw(screen, x1, y1) if x < -bg_image.width: x = 0 if x1 < 0: # this will make spawn the asteroids from the right x1 = bg_image.width # create score board font = pygame.font.SysFont( "Times New Romans", 36) # create an object for input the score in 36 fontsize score_board = font.render( "score:" + str(scores), True, (255, 255, 255)) # Object for the score to update itself in white color # update refered to the word's method screen.blit(score_board, (10, 550)) Jet_sprites.draw(screen) # put the jest inside the screen game bullets.draw(screen) # put the bullets inside the screen game asteroid_group.draw( screen) # put the group of asteroids in the screen game display.update() #update jet and screen view event.get() """moving the jet according to key pressed""" key = pygame.key.get_pressed( ) # Make an object for inputing the hotkey we press when we play the game if key[K_LEFT] and jet1.rect.x > 0: # if you press the left key and the jet is not in the most left screen jet1.moveleft() # then you can press left if key[K_RIGHT] and jet1.rect.x <= 700: # If you press right key before it reaches the most right screen jet1.moveright() # then you can press right if key[K_DOWN] and jet1.rect.y <= 500: # if you press down key before it reaches the most bottom of the screen jet1.movedown() # then you can press down if key[K_UP] and jet1.rect.y > 0: #if you press up before it reaches the upper screen jet1.moveup() # then you can press up if key[K_SPACE] and len(bullets) <= jet1.firerates + ( scores / 4000 ): # if you press space and the length of bullets fired will not be greater than the fire rate of the jet and score + 4000 bullet = Bullet(screen, jet1.rect.x + 50, jet1.rect.y + 42) # object bullet and set the bullet spawn place bullets.add(bullet) # add the bullet spawn place to the bullets pygame.mixer.music.load( "LaserBlast.wav") #add sound evertime you shoot the bullet pygame.mixer.music.play() # play the sound effect if key[K_ESCAPE]: # if I press the escape key menu.menu_screen( Button, run_game ) # it will go the main menu screen and call the button class and run_game function so you can play the game again. if key[K_p]: # if I press the p key menu.pause_menu(Button, run_game) #iy will pause the game """generate asteroid randomly""" #if the asteroid timer is bigger or equal to 200 if pygame.time.get_ticks() - asteroid_timer >= 200: asteroid = Asteroid( screen, 50, 50, random.randint(1, 4) * 6, 800, (random.randint(1, 28) * 20) ) # make object asteroid and set the spawn place which is random by using the mothod random.randint asteroid_group.add(asteroid) #add asteroids to the group asteroid_timer = pygame.time.get_ticks( ) # get the time in the asteroid timer """update the movement of asteroid""" for asteroid in asteroid_group: # for looping in the condition the asteroid is in the asteroid group asteroid.movement() # movement if asteroid.rect.right <= 0: # if the asteroid goes beyond the left screen asteroid_group.remove(asteroid) #remove after screen if groupcollide(Jet_sprites, asteroid_group, dokilla=True, dokillb=True): #collition check menu.lose_menu( Button, run_game, scores ) #if the asteroid touches the jet, the game will go to the lose menu and it will call the Buton class, run_game function so you can either restart or quit the game """update bullet movement on screen""" for bullet in bullets: # for looping for bullet from bullet class bullet.movement() # getting the movement for bullet if bullet.rect.left > 800: # if the bullet goes beyond the right screen, the bullet will be removed bullets.remove(bullet) if groupcollide( bullets, asteroid_group, dokilla=True, dokillb=True): # checking if the bullet hit the asteroids scores += 100 # the score will be up by 100
def run_game(): #if game is running do the this """game play interface""" screen = pygame.display.set_mode((800, 600)) #to set the scale display.set_caption("Jet mission") #to add the text Jet Mission scores = 0 #to set the scores 0 theClock = pygame.time.Clock() #to add time bg_image = Star_bg("star.gif") #to add an image #coordinate of moving background x = 0 y = 0 x1 = bg_image.width y1 = 0 pygame.init() #creating a jet jet1 = Jet(screen) Jet_sprites = Group(jet1) #create asteroid object group asteroid_group = Group() #create bullets object Group bullets = Group() Fps = 40 #to set the frame per second asteroid_timer = pygame.time.get_ticks() #to set the time ticking while True: #while condition true theClock.tick(Fps) #to set the fps of the clock Fps += 0.01 #game phase goes faster after every frame """background move""" x -= 5 #to make the background move to the left by 5 x1 -= 5 bg_image.draw(screen, x, y) #to set the background movement bg_image.draw(screen, x1, y1) if x < -bg_image.width: #to set the background x = 0 if x1 < 0: x1 = bg_image.width # create score board font = pygame.font.SysFont("Times New Romans", 36) #to set the font score_board = font.render("score:" + str(scores), True, (255, 255, 255)) #to set the scores # update refered to the word's method screen.blit(score_board, (10, 550)) #to set screen blit of the score board Jet_sprites.draw(screen) #to activate the jet bullets.draw(screen) #to acvtivate the bullets asteroid_group.draw(screen) #to activate the asteroid display.update() #update jet and screen view event.get() #to do the movement as the key pressed key = pygame.key.get_pressed() if key[K_LEFT] and jet1.rect.x > 0: jet1.moveleft() if key[K_RIGHT] and jet1.rect.x <= 700: jet1.moveright() if key[K_DOWN] and jet1.rect.y <= 500: jet1.movedown() if key[K_UP] and jet1.rect.y > 0: jet1.moveup() if key[K_SPACE] and len(bullets) <= jet1.firerates + ( scores / 4000 ): #to set space as shooting and add score if it hit the target bullet = Bullet(screen, jet1.rect.x + 50, jet1.rect.y + 42) #to set the bullet coordinate bullets.add(bullet) #to set the bullet pygame.mixer.music.load("LaserBlast.wav") #to set sound of bullet pygame.mixer.music.play() #to add soune if key[K_ESCAPE]: menu.menu_screen(Button, run_game) #to set esc as themenu screen if key[K_p]: menu.pause_menu(Button, run_game) #to set p as pause button if pygame.time.get_ticks( ) - asteroid_timer >= 200: #to add asteroid randomly as the time flows asteroid = Asteroid(screen, 50, 50, random.randint(1, 4) * 6, 800, (random.randint(1, 28) * 20)) asteroid_group.add(asteroid) asteroid_timer = pygame.time.get_ticks() for asteroid in asteroid_group: asteroid.movement() #to get the movement def if asteroid.rect.right <= 0: #to move the asteroid asteroid_group.remove(asteroid) #remove after screen if groupcollide(Jet_sprites, asteroid_group, dokilla=True, dokillb=True): #collition check menu.lose_menu(Button, run_game, scores) #if the asteroid hit then lose for bullet in bullets: bullet.movement() #to get bullet movement def if bullet.rect.left > 800: #if the bullet exceded the map than it gone bullets.remove(bullet) if groupcollide(bullets, asteroid_group, dokilla=True, dokillb=True): scores += 100 #if it hit then add 100 to point