python - How can i add a level 2 in my game? -


i making space invaders-like game. have finished first first level has 4 rows of 10 aliens. want add level there 4 rows of 17 aliens make difficult , reduce time limit. have made separate module name 'level2' have tried import , run after level 1 completed. can please me figure out.

thanks in advance

main game(level 1):

import pygame, random, sys time import sleep pygame.locals import * import subprocess import level2  black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) blue = (0, 0, 255) navyblue = (0,0,45) grey = (128,128,128) screen_width = 900 screen_height = 650  pygame.init() openscreen = pygame.display.set_mode([screen_width, screen_height])   screen = pygame.display.set_mode([screen_width, screen_height]) class block(pygame.sprite.sprite):     def __init__(self, image):         aliens = pygame.image.load('aliens.png')         aliens = pygame.transform.scale(aliens, (20,20))         super(block, self).__init__()          self.image = aliens          self.rect = self.image.get_rect()  class player(pygame.sprite.sprite):     def __init__(self):         pygame.mouse.set_visible(0)         ship = pygame.image.load('spaceship.png')         ship = pygame.transform.scale(ship, (20,30))         super(player, self).__init__()          self.image = ship         self.rect = self.image.get_rect()     def update(self):         pos = pygame.mouse.get_pos()         self.rect.x = pos[0]  class bullet(pygame.sprite.sprite):     def __init__(self):         bullets = pygame.image.load('missile.png')         bullets = pygame.transform.scale(bullets, (15,25))         super(bullet, self).__init__()         self.image = bullets         self.rect = self.image.get_rect()      def update(self):         self.rect.y -= 1.25  all_sprites_list = pygame.sprite.group() block_list = pygame.sprite.group() bullet_list = pygame.sprite.group()   in range(10):     block = block(block)     block.rect.x = (40 * i) + 200     block.rect.y = 50     block_list.add(block)     all_sprites_list.add(block)  in range(10):     block = block(block)     block.rect.x = (40 * i) + 200     block.rect.y = 75     block_list.add(block)     all_sprites_list.add(block)  in range(10):     block = block(block)     block.rect.x = (40 * i) + 200     block.rect.y = 100     block_list.add(block)     all_sprites_list.add(block)  in range(10):     block = block(block)     block.rect.x = (40 * i) + 200     block.rect.y = 125     block_list.add(block)     all_sprites_list.add(block)     player = player() all_sprites_list.add(player) done = false clock = pygame.time.clock() score = 0 time = 3000 player.rect.y = 615 font = pygame.font.sysfont(none, 30)    while not done:     openscreen.fill(white)     fonts = pygame.font.sysfont("impact", 55)     display = fonts.render('press space play', true, (black))     openscreen.blit(display, (200 , 200))     font1 = pygame.font.sysfont("impact", 25)     display1 = font.render("press escape quit", true, (black))     openscreen.blit(display1, (670,600))     pygame.display.flip()     event in pygame.event.get():         if event.type == pygame.quit:             done = true         pressed = pygame.key.get_pressed()          if pressed[pygame.k_escape]:             done = true          elif pressed[pygame.k_space]:             while not done:                  event in pygame.event.get():                     if event.type == pygame.quit:                         done = true                      elif event.type == pygame.mousebuttondown:                         bullet = bullet()                         bullet.rect.x = player.rect.x + 3.5                         bullet.rect.y = player.rect.y                         all_sprites_list.add(bullet)                         bullet_list.add(bullet)                    all_sprites_list.update()                  bullet in bullet_list:                     block_hit_list = pygame.sprite.spritecollide(bullet, block_list, true)                     block in block_hit_list:                          bullet_list.remove(bullet)                         all_sprites_list.remove(bullet)                         score += 1                     if bullet.rect.y < -10:                         bullet_list.remove(bullet)                         all_sprites_list.remove(bullet)                  screen.fill(navyblue)                 scoredisplay = font.render("score {0}".format(score), 1, (white))                 screen.blit(scoredisplay, (5, 10))                 all_sprites_list.draw(screen)                 timedisplay = font.render("time {0}".format(time), 1, (white))                 screen.blit(timedisplay, (5, 30))                 pygame.display.flip()                  time -= 1                 if time == 0:                     done = true                     screen.fill(white)                     font= pygame.font.sysfont("impact", 36)                     display = font.render("you ran out of time!! :( final score {}".format(score), 1, (black))                     screen.blit(display, (100,200))                     pygame.display.flip()                     pygame.time.wait(1500)                  if len(block_list)  == 0:                     done == true                     len(bullet_list) == 0                     screen.fill(white)                     time = 0                     font = pygame.font.sysfont("impact", 55)                     display = font.render('you won!',true ,(black))                     display1 = font.render('press space level 2',true ,(black))                     display2 = font.render('escape quit',true ,(black))                     screen.blit(display,(350,200))                     screen.blit(display1,(350,400))                     screen.blit(display2,(500,500))                     clock.tick(60)                     pygame.display.flip()                     pygame.time.wait(1500)                     event in pygame.event.get():                         if event.type == pygame.quit:                             done = true                         pressed = pygame.key.get_pressed()                         if pressed[pygame.k_escape]:                             done = true                         elif pressed[pygame.k_space]:                             done = false                             subprocess.call("level2.py", shell=true)   pygame.quit() 

here level 2:

import pygame, random, sys time import sleep pygame.locals import * black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) blue = (0, 0, 255) navyblue = (0,0,45) grey = (128,128,128) screen_width = 900 screen_height = 650  pygame.init() openscreen = pygame.display.set_mode([screen_width, screen_height])   screen = pygame.display.set_mode([screen_width, screen_height]) class block(pygame.sprite.sprite):     def __init__(self, image):         aliens = pygame.image.load('aliens.png')         aliens = pygame.transform.scale(aliens, (20,20))         super(block, self).__init__()          self.image = aliens          self.rect = self.image.get_rect()  class player(pygame.sprite.sprite):     def __init__(self):         pygame.mouse.set_visible(0)         ship = pygame.image.load('spaceship.png')         ship = pygame.transform.scale(ship, (20,30))         super(player, self).__init__()          self.image = ship         self.rect = self.image.get_rect()     def update(self):         pos = pygame.mouse.get_pos()         self.rect.x = pos[0]  class bullet(pygame.sprite.sprite):     def __init__(self):         bullets = pygame.image.load('missile.png')         bullets = pygame.transform.scale(bullets, (15,25))         super(bullet, self).__init__()         self.image = bullets         self.rect = self.image.get_rect()      def update(self):         self.rect.y -= 3  all_sprites_list = pygame.sprite.group() block_list = pygame.sprite.group() bullet_list = pygame.sprite.group()   in range(17):     block = block(block)     block.rect.x = (40 * i) + 110     block.rect.y = 50     block_list.add(block)     all_sprites_list.add(block)  in range(17):     block = block(block)     block.rect.x = (40 * i) + 110     block.rect.y = 75     block_list.add(block)     all_sprites_list.add(block)  in range(17):     block = block(block)     block.rect.x = (40 * i) + 110     block.rect.y = 100     block_list.add(block)     all_sprites_list.add(block)  in range(17):     block = block(block)     block.rect.x = (40 * i) + 110     block.rect.y = 125     block_list.add(block)     all_sprites_list.add(block)     player = player() all_sprites_list.add(player) done = false clock = pygame.time.clock() score = 0 time = 2000 player.rect.y = 615 font = pygame.font.sysfont(none, 30)    while not done:     event in pygame.event.get():         if event.type == pygame.quit:             done = true         pressed = pygame.key.get_pressed()          if pressed[pygame.k_escape]:             done = true          elif pressed[pygame.k_space]:             while not done:                  event in pygame.event.get():                     if event.type == pygame.quit:                         done = true                      elif event.type == pygame.mousebuttondown:                         bullet = bullet()                         bullet.rect.x = player.rect.x + 3.5                         bullet.rect.y = player.rect.y                         all_sprites_list.add(bullet)                         bullet_list.add(bullet)                    all_sprites_list.update()                  bullet in bullet_list:                     block_hit_list = pygame.sprite.spritecollide(bullet, block_list, true)                     block in block_hit_list:                          bullet_list.remove(bullet)                         all_sprites_list.remove(bullet)                         score += 1                     if bullet.rect.y < -10:                         bullet_list.remove(bullet)                         all_sprites_list.remove(bullet)                  screen.fill(navyblue)                 scoredisplay = font.render("score {0}".format(score), 1, (white))                 screen.blit(scoredisplay, (5, 10))                 all_sprites_list.draw(screen)                 timedisplay = font.render("time {0}".format(time), 1, (white))                 screen.blit(timedisplay, (5, 30))                 pygame.display.flip()                  time -= 1                 if time == 0:                     done = true                     screen.fill(white)                     font= pygame.font.sysfont("impact", 36)                     display = font.render("you ran out of time!! :( final score {}".format(score), 1, (black))                     screen.blit(display, (100,200))                     pygame.display.flip()                     pygame.time.wait(1500)                  if len(block_list)  == 0:                     done == true                     screen.fill(white)                     font = pygame.font.sysfont("impact", 55)                     display = font.render('you won!',true ,(black))                     screen.blit(display,(350,200))                     pygame.display.flip()                     pygame.time.wait(1500)                 clock.tick(60)  pygame.quit() 

take stuff in level 1 except class definitions, , put functions reasonable names "init" , "playgame." let them members of class called "level."

note aspects different in level 1 , level 2. make these things different parameters level's __init__ member, , use them initialize local variable reused playgame member.

now makes sure playgame , init functions work both levels.

it doesn't make sense rewrite code each level. way, can have many levels want.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -