mardi 26 janvier 2021

Why won't my death flag work? It says "player not defined" even though I've done this MULTIPLE TIMES [closed]

code:

#Gilbert
#Platforms Move
import pygame
import random
import math
import time
level_list = []
# Global constants
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
GREEN = (0, 255, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
remove = (255,255,255)
randcolor = (random.randrange(0,256),random.randrange(0,256),random.randrange(0,256))
PI = 3.14159265
current_level_no = 0
dead = False
# Screen dimensions
SCREEN_WIDTH =  800
SCREEN_HEIGHT = 600

class Player(pygame.sprite.Sprite):
  def __init__(self):
      """ Constructor function """
    
      super().__init__()
      self.image = pygame.Surface([10,10])
      self.image = pygame.image.load("mario2.png").convert()
      self.image.set_colorkey(remove)
      self.rect = self.image.get_rect()
      # Sets a reference to the image rect.
      self.rect = self.image.get_rect()
      # Set speed vector of player
      self.change_x = 0
      self.change_y = 0
      # List of sprites we can bump against
      self.level = None
  def update(self): 
      self.calc_grav()
      # Move left/right
      self.rect.x += self.change_x
      ''' See if we hit anything'''  ''''change 3'''
      block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
      for block in block_hit_list:
          if self.change_x > 0:
              self.rect.right = block.rect.left
          elif self.change_x < 0: 
              # Otherwise if we are moving left, do the opposite.
              self.rect.left = block.rect.right
      # Move up/down
      self.rect.y += self.change_y
      #Check and see if we hit anything
      '''Change 5'''
      block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
      for block in block_hit_list:
          # Reset our position based on the top/bottom of the object.
          if self.change_y > 0:
              self.rect.bottom = block.rect.top
          elif self.change_y < 0:                 
              self.rect.top = block.rect.bottom
          # Stop our vertical movement
          self.change_y = 0
          #Change 7 instead of jump'''
          if isinstance(block, movingplatform):
              self.rect.x += block.change_x
  def calc_grav(self):
      #Calculate effect of gravity. """
      if self.change_y == 0:
          self.change_y = 1
      else:
          self.change_y += .35
      if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
          self.change_y = 0
          self.rect.y = SCREEN_HEIGHT - self.rect.height
  def jump(self):
      self.rect.y += 2
      platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
      self.rect.y -= 2
      # If it is ok to jump, set our speed upwards
      if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
          self.change_y = -10
  # Player-controlled movement:
  def go_left(self):
      #Called when the user hits the left arrow. """
      self.change_x = -4
      self.image = pygame.image.load("mario3.png").convert()
      self.image.set_colorkey(remove)
  def go_right(self):
      #Called when the user hits the right arrow. """
      self.change_x = 4
      self.image = pygame.image.load("mario2.png").convert()
      self.image.set_colorkey(remove)
  def stop(self):
      #Called when the user lets off the keyboard.
      self.change_x = 0

class Platform(pygame.sprite.Sprite):
  #Platform the user can jump on
  def __init__(self, width, height):
      #Platform constructor. Assumes constructed with user passing in
      #an array of 5 numbers like what's defined at the top of this code.
      super().__init__()
      self.image = pygame.Surface([width, height])
      self.image.fill(black)
      self.rect = self.image.get_rect()
  def update(self): 
    super().__init__()
    randcolor = (random.randrange(0,256),random.randrange(0,256),random.randrange(0,256))
    self.image.fill(red)
    
class deathplatform(Platform):
       change_x = 0
       change_y = 0
       boundary_top = 0
       boundary_bottom = 0
       boundary_left = 0
       boundary_right = 0
       player = None #To push player around if collides with platform
       level = None
       def update(self):
              self.image.fill(black)
              self.rect.x += self.change_x   
              hit = pygame.sprite.collide_rect(self, self.player)
              if hit:
                     dead = True
                     self.player.kill()
                     if dead:
                                 current_level_no = 1
                                 print("current_level_no is " + str(current_level_no))
                                 current_level = level_list[current_level_no]
                                 player.level = current_level              
              hit = pygame.sprite.collide_rect(self, self.player)
              if hit:
                     dead = True
                     self.player.kill()
                     if dead:
                                 current_level_no = 1
                                 print("current_level_no is " + str(current_level_no))
                                 current_level = level_list[current_level_no]
                                 player.level = current_level

              if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
                     self.change_y *= -1
              cur_pos = self.rect.x - self.level.world_shift
              if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
                     self.change_x *= -1
class movingplatform(Platform):
       change_x = 0
       change_y = 0
       boundary_top = 0
       boundary_bottom = 0
       boundary_left = 0
       boundary_right = 0
       player = None  
       level = None
       def update(self):
             # Move left/right
              self.rect.x += self.change_x  
              randcolor = (random.randrange(0,256),random.randrange(0,256),random.randrange(0,256))
              self.image.fill(red)
              hit = pygame.sprite.collide_rect(self, self.player)
              if hit:
                     if self.change_x < 0:
                            self.player.rect.right = self.rect.left
                     else:
                            self.player.rect.left = self.rect.right
              if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
                     self.change_y *= -1
              cur_pos = self.rect.x - self.level.world_shift
              if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
                     self.change_x *= -1
  
class Level(object):
  def __init__(self, player):
       self.platform_list = pygame.sprite.Group()
       self.enemy_list = pygame.sprite.Group()
       self.player = player
       self.background = None
       self.world_shift = 0
       self.level_limit = -1000

  def update(self):
      """ Update everything in this level."""
      self.platform_list.update()
      self.enemy_list.update()
      
  def draw(self, screen):
       screen.fill(blue)
       self.platform_list.draw(screen)
       self.enemy_list.draw(screen)
       
  def shift_world(self, shift_x):
      self.world_shift += shift_x
      for platform in self.platform_list:
              platform.rect.x += shift_x
      for enemy in self.enemy_list:
              enemy.rect.x += shift_x
 
class Level_01(Level):
       def __init__(self, player):
      # Call the parent constructor
              Level.__init__(self, player)
              self.level_limit = -1500
              level = [ [5,5,5,5]      ]

              for platform in level:
                     block = Platform(platform[0], platform[1],)
                     block.rect.x = platform[2]
                     block.rect.y = platform[3]
                     block.player = self.player
                     self.platform_list.add(block)

class Level_02(Level):
       def __init__(self, player):
              Level.__init__(self, player)
              self.level_limit = -1500
      # Array with width, height, x, and y of platform
              level = [[210, 70, 500, 500],
                       [210, 70, 700, 500],
                      [210, 70, 600, 400],
                      [210, 70, 500, 500],
                      [10, 1900, 1300, 200],
                      [10, 850, 130, 40],
                      [10, 1700, 1500, 200],
                      [10, 900, 500, 500],
                    
                      ]
      # Go through the array above and add platforms
              for platform in level:
                     block = Platform(platform[0], platform[1],)
                     block.rect.x = platform[2]
                     block.rect.y = platform[3]
                     block.player = self.player
                     self.platform_list.add(block)
      
              block = movingplatform(70, 40)
              block.rect.x = 1350   #sets location originally off screen
              block.rect.y = 250
              block.boundary_bottom = 100 #sets platform's boundaries
              block.boundary_top = 550
              block.change_y = 1  #initial direction
              block.player = self.player  #reference block to player
              block.level = self
              self.platform_list.add(block)
              
              block = movingplatform(70, 40)
              block.rect.x = 840  
              block.rect.y = 250
              block.boundary_left = 350  
              block.boundary_right = 1000
              block.change_x = -2  
              block.player = self.player   
              block.level = self
              self.platform_list.add(block)
       
              block = deathplatform(100, 40)
              block.rect.x = 840  
              block.rect.y = 500
              block.boundary_left = 350  
              block.boundary_right = 1000
              block.change_x = -2   
              block.player = self.player
              block.level = self
              self.platform_list.add(block)

class Level_03(Level):
  """ Definition for level 2. """
  def __init__(self, player):
      """ Create level 1. """
      # Call the parent constructor
      Level.__init__(self, player)
      self.level_limit = -1000
      # Array with type of platform, and x, y location of the platform.
      level = [[210, 70, 500, 550],
               [210, 70, 800, 400],
               [210, 70, 1000, 500],
               [210, 70, 1120, 280],
               [10,1300, 1600, 200]
               ]
      # Go through the array above and add platforms
      for platform in level:
          block = Platform(platform[0], platform[1])
          block.rect.x = platform[2]
          block.rect.y = platform[3]
          block.player = self.player
          self.platform_list.add(block)
      # Add a custom moving platform
      block = movingplatform(70, 70)
      block.rect.x = 1500
      block.rect.y = 300
      block.boundary_top = 100
      block.boundary_bottom = 550
      block.change_y = -1
      block.player = self.player
      block.level = self
      self.platform_list.add(block)
      block = movingplatform(70, 40)
      block.rect.x = 1500 #ginally off screen
      block.rect.y = 100
      block.boundary_left = 1300 #sets platform's boundaries
      block.boundary_right = 1500
      block.change_x = -13  #initial direction
      block.player = self.player  #reference block to player
      block.level = self
      self.platform_list.add(block)
      
class Level_04(Level):
  def __init__(self, player):
      # Call the parent constructor
      Level.__init__(self, player)
      self.level_limit = -1000
      # Array with type of platform, and x, y location of the platform.
      level = [[120, 20, 500, 550],
               [310, 80, 800, 400],
               [630, 70, 1000, 500],
               [420, 40, 1120, 250],
               [120,130, 300, 1000]
               ]
      # Go through the array above and add platforms
      for platform in level:
          block = Platform(platform[0], platform[1])
          block.rect.x = platform[2]
          block.rect.y = platform[3]
          block.player = self.player
          self.platform_list.add(block)
      # Add a custom moving platform
      block = movingplatform(190, 100)
      block.rect.x = 1500
      block.rect.y = 300
      block.boundary_top = 100
      block.boundary_bottom = 550
      block.change_y = -1
      block.player = self.player
      block.level = self
      self.platform_list.add(block)
     # Add a custom moving platform
      block = movingplatform(400, 100)
      block.rect.x = 1100
      block.rect.y = 290
      block.boundary_left = 100
      block.boundary_right = 550
      block.change_x = -5
      block.player = self.player
      block.level = self
      self.platform_list.add(block)
      
class Level_05(Level):
  def __init__(self, player):
      Level.__init__(self, player)
      self.level_limit = -1000
      level = [[120, 20, 500, 550],
               [310, 80, 800, 400],
               [630, 70, 1000, 500],
               [420, 40, 1120, 280],
               [120,130, 300, 1000]
               ]
      for platform in level:
          block = Platform(platform[0], platform[1])
          block.rect.x = platform[2]
          block.rect.y = platform[3]
          block.player = self.player
          self.platform_list.add(block)

      block = movingplatform(190, 100)
      block.rect.x = 1500
      block.rect.y = 300
      block.boundary_top = 100
      block.boundary_bottom = 550
      block.change_y = -1
      block.player = self.player
      block.level = self
      self.platform_list.add(block)

      block = deathplatform(230, 100)
      block.rect.x = 1100
      block.rect.y = 290
      block.boundary_top = 100
      block.boundary_bottom = 450
      block.change_y = -5
      block.player = self.player
      block.level = self
      self.platform_list.add(block)
      
def main():
       
       pygame.init()
       size = [SCREEN_WIDTH, SCREEN_HEIGHT]
       screen = pygame.display.set_mode(size)
       pygame.display.set_caption("Platformer with moving platforms")
       player = Player()
 
       level_list.append(Level_01(player))
       level_list.append(Level_02(player))
       level_list.append(Level_03(player))
       level_list.append(Level_04(player))
       level_list.append(Level_05(player))
  
       current_level_no = 0
       current_level = level_list[current_level_no]
       active_sprite_list = pygame.sprite.Group()
       player.level = current_level
       player.rect.x = 340
       player.rect.y = SCREEN_HEIGHT - player.rect.height
       active_sprite_list.add(player)
       done = False
       dead = False
       clock = pygame.time.Clock()
  # -------- Main Program Loop -----------
       while not done:
              for event in pygame.event.get():
                     if event.type == pygame.QUIT:
                            pygame.quit()
                            done = True
                     if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_LEFT:
                                   player.go_left()
                            if event.key == pygame.K_RIGHT:
                                   player.go_right()
                            if event.key == pygame.K_UP:
                                   player.jump()
                            if event.key == pygame.K_DOWN:
                                   current_level_no = 1
                                   print("current_level_no is " + str(current_level_no))
                                   current_level = level_list[current_level_no]
                                   player.level = current_level
                                   
                     if event.type == pygame.KEYUP:
                            if event.key == pygame.K_LEFT and player.change_x < 0:
                                   player.stop()
                            if event.key == pygame.K_RIGHT and player.change_x > 0:
                                   player.stop()
                   

              active_sprite_list.update()
              current_level.update()
    
              if player.rect.right >= 500:
                     diff = player.rect.right - 500
                     player.rect.right = 500
                     current_level.shift_world(-diff)

              if player.rect.left <= 120:
                     diff = 120 - player.rect.left
                     player.rect.left = 120
                     current_level.shift_world(diff)

              current_position = player.rect.x + current_level.world_shift
              if dead == True:
                current_level_no = 1
                current_level = 1
                player.level = current_level
              if current_position < current_level.level_limit:
                     if current_level_no < len(level_list) - 1:
                            player.rect.x = 120
                            current_level_no += 1
                            current_level = level_list[current_level_no]
                            player.level = current_level
                     else:
                            current_level_no = 1
      # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
              current_level.draw(screen)
              active_sprite_list.draw(screen)
              if current_level_no == 0:
                     font2 = pygame.font.SysFont("windgdings",40)
                     text2 = font2.render("Finish all the levels! Arrow keys to move! ",True, red)
                     text3= font2.render("This is: STAR WARRIOR",True,red)  #only one string can be rendered at a time so append variables to the string as a string
                     text4 = font2.render("Ps: black blocks harm you, beware!",True,white)
                     text5 = font2.render("to go to world 1, just use the down key. ",True, GREEN)
                     screen.blit(text2,[100,0])
                     screen.blit(text3,[100,50])
                     screen.blit(text4,[100,100])
                     screen.blit(text5,[100,150])
                     time.sleep(3)
                      
              else:
                     font2 = pygame.font.SysFont("windgdings",40)
                     text2 = font2.render("Finish all the levels! Arrow keys to move! ",True, red)
                     text3= font2.render("This is: STAR WARRIOR",True,red)  #only one string can be rendered at a time so append variables to the string as a string
                     text4 = font2.render("Ps: black blocks harm you, beware!",True,white)
                     screen.blit(text2,[0,1000])
                     screen.blit(text3,[100,1000])
                     screen.blit(text4,[100,1000])
              pygame.display.flip()
              clock.tick(600)
       pygame.quit()
if __name__ == "__main__":
  main()
 
 



Aucun commentaire:

Enregistrer un commentaire