def run_level(self): while self.running: self.game.screen.fill( (0, 0, 255)) # background outside level space self.game.screen.blit( pygame.transform.scale(background, (round(game_width * self.scale), round(game_height * self.scale))), (round(-(self.player.x * self.scale - self.player.pos()[0])), round(-(self.player.y * self.scale - self.player.pos()[1])) )) # level space backgorund sync with player position if not self.user_interaction_handler(): return if self.clicks > 0: # very rudimentary way of knowing number of rapid clicks self.clicks -= 1 if not self.paused: if self.player.size < 1: return game_over(self.game) pygame.draw.circle(self.game.screen, (0, 30, 255), self.player.pos(), self.player.scaled_size()) self.player.update() for enemy in self.enemies.copy(): pygame.draw.circle(self.game.screen, enemy.color, enemy.pos(), enemy.scaled_size()) if not enemy.update(): self.enemies.remove(enemy) for enemy in self.enemies + [self.player]: for enemy2 in self.enemies + [self.player]: if enemy2 != enemy: if collision(enemy, enemy2): attack(enemy, enemy2) pygame.draw.circle(self.game.screen, (150, 150, 150), [ pos + (self.player.scaled_size() * 1.5) * dir for pos, dir in zip(self.player.pos(), get_trig(self.player)) ], 5) if all([ self.player.size > enemy.size for enemy in self.enemies ]) and not self.won: # check if user is the biggest self.won = true self.paused = true else: if self.won: if not pause_menu(self.game, won=true): return false else: self.paused = false else: if not pause_menu(self.game): return false else: self.paused = false pygame.display.update() self.game.clock.tick(self.game.fps)
def events(self): """ Catches all game-related events """ # catch all events here for event in pg.event.get(): if event.type == pg.QUIT: self.quit_game() if event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: menu.paused = True menu.pause_menu( ) #code gets stuck in this call until a button is pressed in the pause menu self.clock = pg.time.Clock() if event.key == pg.K_h: self.draw_debug = not self.draw_debug if event.key == pg.K_o: if self.flashlight.on: #turning off flashlight self.darkness.on = True self.battery.duration -= pg.time.get_ticks( ) - self.battery.last_update self.flashlight.on = False else: #turning on flashlight self.darkness.on = False self.battery.last_update = pg.time.get_ticks() self.flashlight.on = True #darkness condition if self.transition: self.darkness_transition(self.player) self.kidnap(self.player) # win condition if pg.sprite.spritecollide(self.player, self.win, False, collide_hit_rect): menu.win_menu() #got hit condition hit = pg.sprite.spritecollide(self.player, self.threat, False, collide_hit2_rect) if hit: self.hit(self.player, hit[0]) #mirror self.portal(self.player) self.portal(self.monster)
def Play(curr_level): CHEAT_DEATH = False F.SCREEN.blit(pygame.image.load('images/background/main_screen.png'), (0, 0)) pygame.display.flip() print "current level: " + str(curr_level) levelInfo, levelList = get_level('level_' + str(curr_level) + '.txt') pos_ID = levelInfo[0] pos_initial = levelInfo[1] bg = levelInfo[2] background = pygame.image.load('images/background/' + bg).convert() col_list = levelList[0] box_list = levelList[1] carrot_list = levelList[3] updateable_list = levelList[4] sprite_list = levelList[5] stopper_list = levelList[6] moving_list = levelList[7] checkpoint_list = levelList[8] PAUSE_SCREEN = pygame.image.load( 'images/background/main_screen.png').convert() win_screen = pygame.image.load('images/menu/win_screen.png').convert() win_screen.set_colorkey(F.COLOR_KEY) win_rect = win_screen.get_rect() win_pos = (F.SCREEN_WIDTH / 2 - win_rect.w / 2, F.SCREEN_HEIGHT / 2 - win_rect.h / 2) final_win_screen = pygame.image.load( 'images/menu/final_win_screen.png').convert() final_win_screen.set_colorkey(F.COLOR_KEY) final_win_rect = final_win_screen.get_rect() final_win_pos = (F.SCREEN_WIDTH / 2 - final_win_rect.w / 2, F.SCREEN_HEIGHT / 2 - final_win_rect.h / 2) pos_x, pos_y = pos_initial player = Player(pos_x, pos_y) player.ID = pos_ID player.level = col_list player.carrots = carrot_list player.moving = moving_list player.checkpoints = checkpoint_list for box in box_list.sprites(): box.level = stopper_list for col in col_list.sprites(): box.level.add(col) box.boxes = box_list box.player = player for moving_box in moving_list.sprites(): moving_box.level = stopper_list for colx in col_list.sprites(): moving_box.level.add(colx) updateable_list.add(player) gravity = 'S' EXIT_LEVEL = False clock = pygame.time.Clock() while not EXIT_LEVEL: for event in pygame.event.get(): if event.type == pygame.QUIT: return True, False, False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_n: curr_level += 1 return False, False, False, curr_level if event.key == pygame.K_c: CHEAT_DEATH = True elif event.key == pygame.K_ESCAPE: PAUSE = menu.pause_menu(PAUSE_SCREEN) if PAUSE == 'Continue': pass elif PAUSE == False: pass elif PAUSE == 'Main': return False, False, True, curr_level elif event.key == pygame.K_q or event.key == pygame.K_a and not player.bounce: player.go_left() elif event.key == pygame.K_e or event.key == pygame.K_d and not player.bounce: player.go_right() elif event.key == pygame.K_w or event.key == pygame.K_SPACE and player.touch_S( 0): player.jump() if event.key == pygame.K_LEFT: if static_boxes(box_list): gravity = 'LE' elif event.key == pygame.K_UP: if static_boxes(box_list): gravity = 'UP' elif event.key == pygame.K_RIGHT: if static_boxes(box_list): gravity = 'RI' elif event.key == pygame.K_DOWN: if static_boxes(box_list): gravity = 'DN' elif event.type == pygame.KEYUP: if event.key == pygame.K_q or event.key == pygame.K_a and player.spd_x < 0: player.stop() elif event.key == pygame.K_e or event.key == pygame.K_d and player.spd_x > 0: player.stop() #rof #for k in moving_list.sprites(): #move = k.update_dir() moving_list.update() box_list.update(gravity) player.update() checkpoint_list.update() if player.dead: player.image = player.dead_image if player.direction == 'Left': player.image = pygame.transform.flip(player.image, True, False) F.SCREEN.blit(background, (0, 0)) carrot_list.draw(F.SCREEN) moving_list.draw(F.SCREEN) checkpoint_list.draw(F.SCREEN) F.SCREEN.blit(player.image, (player.rect.x - 8, player.rect.y - 4)) sprite_list.draw(F.SCREEN) stopper_list.draw(F.SCREEN) pygame.display.flip() PAUSE_SCREEN.blit(F.SCREEN, (0, 0)) if not CHEAT_DEATH: death_3 = pygame.image.load( 'images/menu/countdown3.png').convert() death_3.set_colorkey(F.COLOR_KEY) death_rect = death_3.get_rect() death_pos = (F.SCREEN_WIDTH / 2 - death_rect.w / 2, F.SCREEN_HEIGHT / 2 - death_rect.h / 2) F.SCREEN.blit(death_3, death_pos) pygame.display.flip() pygame.time.wait(1000) death_2 = pygame.image.load( 'images/menu/countdown2.png').convert() death_2.set_colorkey(F.COLOR_KEY) F.SCREEN.blit(death_2, death_pos) pygame.display.flip() pygame.time.wait(1000) death_1 = pygame.image.load( 'images/menu/countdown1.png').convert() death_1.set_colorkey(F.COLOR_KEY) F.SCREEN.blit(death_1, death_pos) pygame.display.flip() pygame.time.wait(1000) F.SCREEN.blit(PAUSE_SCREEN, (0, 0)) pygame.display.flip() clock.tick(F.MAX_FPS) for obj in updateable_list.sprites(): gravity = 'S' obj.reboot(gravity) elif player.win: curr_level += 1 if curr_level == 9: while True: F.SCREEN.blit(final_win_screen, final_win_pos) pygame.display.flip() pygame.time.wait(500) for event in pygame.event.get(): return False, False, True, curr_level else: F.SCREEN.blit(win_screen, win_pos) pygame.display.flip() pygame.time.wait(500) return False, False, False, curr_level else: F.SCREEN.blit(background, (0, 0)) carrot_list.draw(F.SCREEN) checkpoint_list.draw(F.SCREEN) F.SCREEN.blit(player.image, (player.rect.x - 8, player.rect.y - 4)) sprite_list.draw(F.SCREEN) stopper_list.draw(F.SCREEN) PAUSE_SCREEN.blit(F.SCREEN, (0, 0)) pygame.display.flip() clock.tick(F.MAX_FPS)
def main(): # Initialisation global in_menu, playing debug_mode = False all_input = ("a", "z", "q", "s", "w", "t", "g", "y", "h", "b", "colon", "o", "l", "p", "m", "8", "5", "9", "6", "2", "Up", "Down", "Left", "Right", "Return") selection = 0 display.init_game() display.init_menu() while True: input = utk.attente_touche_jusqua(100 - 100 * debug_mode) # Gestion du menu if lvl.menu_loop: print("input :", input) menu.main_menu(input) timer.update_timer() # Gestion en jeu elif lvl.playing_loop: ##### Parti debug ##### if input == "F1": debug_mode = not debug_mode if debug_mode: input = all_input[random.randint(0, len(all_input) - 1)] ##### Fin parti debug ##### print("input :", input) if input is not None: if lvl.discussing: lvl.discussing = False display.efface_discuss() elif lvl.player_using_vortex != -1: vortex_selection(input) elif lvl.player_using_spell != -1: spell_target_selection(input) elif menu.paused == True: menu.pause_menu(input) else: player_choose(input) check_steal(input) if input == "Escape": print("affichage du menu pause") menu.activate_pause_menu() timer.update_timer() check_steal() refresh_display() display.display_frame() utk.mise_a_jour() if check_exit() or check_timer() or (lvl.playing_loop and check_guard_catch()) \ or input == "F4" or (not lvl.menu_loop and not lvl.playing_loop): break end_game() utk.ferme_fenetre()
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 game_loop(cfg, render, players_count, map_name): texture_loader = textures.Loader() play_map = world_map.Map(map_path(map_name), render, texture_loader) play_map.build_grid() BulletExplosion.load_animation(texture_loader) FullSizeExplosion.load_animation(texture_loader) PanzerTankMovement.load_animation(texture_loader) BasicTankMovement.load_animation(texture_loader) EnemyOneMovement.load_animation(texture_loader) EnemyTwoMovement.load_animation(texture_loader) players = [] for i in range(players_count): player = Player() player.name = _('Player %d') % i players.append(player) keyboard_controllers = controllers.keyboard_controls() for i in range(pygame.joystick.get_count()): if i >= len(players): break j = pygame.joystick.Joystick(i) players[i].controller = controllers.Gamepad(j) for player in players: if player.controller is not None: continue player.controller = next(keyboard_controllers) for i, position in enumerate(play_map.player_starts): if i >= players_count: break tank = Tank(position, texture_loader) players[i].tank = tank for player in players: if player.tank is None: raise Exception(_("Not enough start points for players!")) render.clear_screen() game_world = world.World(play_map, players, texture_loader) game_world.init() clock = pygame.time.Clock() end_message = None while 42: deltat = clock.tick(FRAMES) events = eventer.get_events() if eventer.game_stopped(): pygame.mixer.pause() selected = menu.pause_menu(cfg, render) if selected == menu.PauseMenu.PAUSE_MENU_QUIT: end_message = _("You Gave Up! Why?") break else: render.clear_screen() render.draw_background() pygame.mixer.unpause() clock.tick() continue game_state = game_world.tick(deltat, events) if game_state == GAME_OVER: end_message = _("GAME OVER. You've lost!") break if game_state == GAME_WON: end_message = _("Yey! You've won!") break render.update_fps(clock) render.draw(game_world.get_drawables()) for player in players: if player.tank is not None: player.tank.stop() while game_world.active_animations_count() > 0: deltat = clock.tick(FRAMES) events = eventer.get_events() game_world.tick_only_animations(deltat, events) render.update_fps(clock) render.draw(game_world.get_drawables()) stats = game_world.get_end_game_stats() render.draw_end_game_screen(end_message, stats) time.sleep(2)
def render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, game_state): # Define colors of walls and floors colors = { 'dark_wall': from_dungeon_level([[tc.darkest_grey,1], [tc.desaturated_orange,4], [tc.darker_azure,7], [tc.darkest_fuchsia,10], [tc.darkest_flame,13]], game_map.dungeon_level), 'dark_ground': from_dungeon_level([[tc.darker_sepia,1], [tc.darkest_grey,4], [tc.darkest_grey,7], [tc.darkest_sepia,10], [tc.darkest_grey,13]], game_map.dungeon_level), 'light_wall': from_dungeon_level([[tc.dark_grey,1], [tc.brass,4], [tc.dark_azure,7], [tc.desaturated_fuchsia,10], [tc.dark_flame,13]], game_map.dungeon_level), 'light_ground': from_dungeon_level([[tc.dark_sepia,1], [tc.darker_grey,4], [tc.darker_grey,7], [tc.darker_sepia,10], [tc.darker_grey,13]], game_map.dungeon_level), 'burning_ground': tc.dark_flame } # Draw the game map if fov_recompute: for y in range(game_map.height): for x in range(game_map.width): game_map.tiles[x][y].take_turn() visible = tc.map_is_in_fov(fov_map, x, y) wall = game_map.tiles[x][y].block_sight if visible: if wall: tc.console_set_default_foreground(con, colors.get('light_wall')) tc.console_put_char(con, x, y, '#', tc.BKGND_NONE) elif game_map.tiles[x][y].burning: tc.console_set_default_foreground(con, colors.get('burning_ground')) tc.console_put_char(con, x, y, '_', tc.BKGND_NONE) else: tc.console_set_default_foreground(con, colors.get('light_ground')) tc.console_put_char(con, x, y, '_', tc.BKGND_NONE) game_map.tiles[x][y].explored = True elif game_map.tiles[x][y].explored: if wall: tc.console_set_default_foreground(con, colors.get('dark_wall')) tc.console_put_char(con, x, y, '#', tc.BKGND_NONE) else: tc.console_set_default_foreground(con, colors.get('dark_ground')) tc.console_put_char(con, x, y, '_', tc.BKGND_NONE) entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value) # Draw all entities in the list for entity in entities_in_render_order: draw_entity(con, entity, fov_map, game_map, colors) tc.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0) tc.console_set_default_background(panel, tc.black) tc.console_clear(panel) # Print game messages y = 1 for message in message_log.messages: tc.console_set_default_foreground(panel, message.color) tc.console_print_ex(panel, message_log.x, y, tc.BKGND_NONE, tc.LEFT, message.text) y += 1 render_bar(panel, 1, 3, bar_width, 'HP', player.fighter.hp, player.fighter.max_hp, tc.light_red, tc.darker_red) render_bar(panel, 1, 5, bar_width, 'XP', player.level.current_xp, player.level.experience_to_next_level, tc.light_green, tc.darker_green) tc.console_print_ex(panel, 1, 1, tc.BKGND_NONE, tc.LEFT, 'Dungeon level: {0}'.format(game_map.dungeon_level)) tc.console_set_default_foreground(panel, tc.light_gray) tc.console_print_ex(panel, 1, 0, tc.BKGND_NONE, tc.LEFT, get_names_under_mouse(mouse, entities, fov_map)) tc.console_blit(panel, 0, 0, screen_width, panel_height, 0, 0, panel_y) if game_state in (GameState.SHOW_INVENTORY, GameState.DROP_INVENTORY): if game_state == GameState.SHOW_INVENTORY: inventory_title = 'Press the key next to an item to use it, or Esc to exit.\n' else: inventory_title = 'Press the key next to na item to drop it, or Esc to exit.\n' inventory_menu(con, inventory_title, player, 50, screen_width, screen_height) elif game_state == GameState.LEVEL_UP: level_up_menu(con, 'Level up! Choose a stat to raise:', player, 40, screen_width, screen_height) elif game_state == GameState.CHARACTER_SCREEN: character_screen(player, 30, 10, screen_width, screen_height) elif game_state == GameState.PAUSE: pause_menu(con, 'Game paused', 30, screen_width, screen_height)
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
def run_level(self): while self.running: self.game.screen.fill((0, 0, 255)) self.game.screen.blit( pygame.transform.scale(background, (round(game_width * self.scale), round(game_height * self.scale))), (round(-(self.player.x * self.scale - self.player.pos()[0])), round(-(self.player.y * self.scale - self.player.pos()[1])))) if not self.user_interaction_handler(): return if self.clicks > 0: self.clicks -= 1 if not self.paused: if self.player.size < 1: return game_over(self.game) pygame.draw.circle(self.game.screen, (0, 255, 255), self.player.pos(), self.player.scaled_size()) self.player.update() for enemy in self.enemies.copy() + [self.repulsor]: if enemy == self.repulsor and self.repulsor.size > 1: pygame.draw.circle(self.game.screen, (255, 255, 255), enemy.pos(), enemy.scaled_size()) enemy.update() elif enemy != self.repulsor: pygame.draw.circle(self.game.screen, enemy.color, enemy.pos(), enemy.scaled_size()) if not enemy.update(): self.enemies.remove(enemy) for enemy in self.enemies + [self.player, self.repulsor]: for enemy2 in self.enemies + [self.player, self.repulsor]: if enemy2 != enemy: if enemy == self.repulsor and enemy.size < enemy2.size: enemy.evade( get_trig_general(enemy, enemy2), distance(enemy, enemy2) ) # Repulsor evades from bigger spheres if collision(enemy, enemy2): attack(enemy, enemy2) pygame.draw.circle(self.game.screen, (150, 150, 150), [ pos + (self.player.scaled_size() * 1.5) * dir for pos, dir in zip(self.player.pos(), get_trig(self.player)) ], 5) if self.player.size > self.repulsor.size and self.repulsor.size < 1 and not self.won: self.won = true self.paused = true else: if self.won: if not pause_menu(self.game, won=true): return false else: self.paused = false else: if not pause_menu(self.game): return false else: self.paused = false pygame.display.update() self.game.clock.tick(self.game.fps)