Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- # Define some colors
- black = ( 0, 0, 0)
- white = ( 255, 255, 255)
- red = ( 255, 0, 0)
- background_image = pygame.image.load("background.png")
- player_y = 20
- y_speed = 0
- obj_speed = 0.5
- sky = 0
- player_x = 50
- skyChange = False
- # This class represents the ball
- # It derives from the "Sprite" class in Pygame
- class Block(pygame.sprite.Sprite):
- # Constructor. Pass in the color of the block,
- # and its x and y position
- def __init__(self, width, height):
- # Call the parent class (Sprite) constructor
- pygame.sprite.Sprite.__init__(self)
- # Create an image of the block, and fill it with a color.
- # This could also be an image loaded from the disk.
- self.image = pygame.image.load("rock.png")
- # Fetch the rectangle object that has the dimensions of the image
- # image.
- # Update the position of this object by setting the values
- # of rect.x and rect.y
- self.rect = self.image.get_rect()
- self.rect.x -= 0.5
- class Player(pygame.sprite.Sprite):
- def __init__(self, width, height):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("player.png")
- self.rect=self.image.get_rect()
- class Wall(pygame.sprite.Sprite):
- def __init__(self, width, height):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("Wall_of_Flesh.png")
- self.rect=self.image.get_rect()
- # Initialize Pygame
- pygame.init()
- # Set the height and width of the screen
- screen_width=800
- screen_height=400
- screen=pygame.display.set_mode([screen_width,screen_height])
- pygame.display.set_icon(pygame.image.load("gameicon.png"))
- # This is a list of 'sprites.' Each block in the program is
- # added to this list. The list is managed by a class called 'Group.'
- block_list = pygame.sprite.Group()
- player_list = pygame.sprite.Group()
- wof_list = pygame.sprite.Group()
- # This is a list of every sprite. All blocks and the player block as well.
- all_sprites_list = pygame.sprite.Group()
- for i in range(10):
- # This represents a block
- block = Block(20, 15)
- # Set a random location for the block
- block.rect.x = random.randrange(400,2000)
- block.rect.y = random.randrange(161,180)
- # Add the block to the list of objects
- block_list.add(block)
- all_sprites_list.add(block)
- # Create a red player block
- player = Player(300, 300)
- all_sprites_list.add(player)
- player_list.add(player)
- wof = Wall(144,400)
- all_sprites_list.add(wof)
- wof_list.add(player)
- #Loop until the user clicks the close button.
- done = False
- # Used to manage how fast the screen updates
- clock = pygame.time.Clock()
- score = 0
- # -------- Main Program Loop -----------
- while done==False:
- for event in pygame.event.get(): # User did something
- if event.type == pygame.QUIT: # If user clicked close
- done = True # Flag that we are done so we exit this loop
- if event.type == pygame.KEYDOWN: #user press down a key
- if event.key == pygame.K_UP:
- y_speed -= 3
- if event.key == pygame.K_DOWN:
- y_speed += 3
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_UP:
- y_speed += 3
- if event.key == pygame.K_DOWN:
- y_speed -= 3
- # Clear the screen
- player.rect.y = player_y
- player_y += y_speed
- player.rect.x = player_x
- wof.rect.x = (player_x - 400)
- wof.rect.y = (player_y - 450)
- # See if the player block has collided with anything.
- player_hit_list = pygame.sprite.spritecollide(block, player_list, True)
- player_hit_list = pygame.sprite.spritecollide(wof, player_list, True)
- block_hit_list = pygame.sprite.spritecollide(wof, block_list, True)
- if player in player_hit_list:
- print("uded")
- # Check the list of collisions.
- # Draw all the spites
- while sky == 0:
- screen.blit(pygame.image.load("background.png"), [0,0])
- all_sprites_list.draw(screen)
- if player_y < 10:
- sky = 1
- while sky == 1:
- player_y = 390
- screen.blit(pygame.image.load("sky.png"), [0,0])
- player_list.draw(screen)
- wof_list.draw(screen)
- for block in block_list:
- block.rect.x -= 0.1
- player_x += 1
- # Limit to 20 frames per second
- clock.tick(60)
- # Go ahead and update the screen with what we've drawn.
- pygame.display.flip()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment