Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Demonstration of Treasure Game
- Author: Harry Zhang
- Date: June 5, 2016
- Release Notes:
- - No variable dictionary
- - Limited commenting
- - Working game
- !IMPORTANT!
- If any plagiarism or cheating is found, all students involved will get a ZERO mark.
- Readme:
- Knowing the above, to have this code work for you,
- please change the image files located in the classes
- and the fontface to be used
- """
- import pygame, sys, os, time, random, platform
- from pygame.locals import *
- from pygame.color import THECOLORS
- if platform.system() == "Windows":
- os.environ['SDL_VIDEODRIVER'] = 'windib'
- # Enemy template
- class Enemy (pygame.sprite.Sprite):
- def __init__(self, xCoord, yCoord, xDirection, yDirection, speed):
- pygame.sprite.Sprite.__init__(self)
- # Create and load image for enemy
- self.image = pygame.Surface((30, 30))
- shapeImage = pygame.image.load("ENEMY IMAGE").convert()
- self.image.blit(shapeImage, (0, 0))
- self.image.set_colorkey((255, 255, 255))
- # Set it's bounding box and coordinates
- self.rect = self.image.get_rect()
- self.rect.centery = yCoord
- self.rect.centerx = xCoord
- # Set it's motion and directions
- self.xdir = xDirection
- self.ydir = yDirection
- self.speed = speed
- def update(self):
- # Move the sprite everytime update() is called
- self.rect.centerx = self.rect.centerx + self.xdir * self.speed
- self.rect.centery = self.rect.centery + self.ydir * self.speed
- # Make sure the sprite is within the limits of the playing field
- if self.rect.centerx <= 15 or self.rect.centerx >= screen.get_width() - 15:
- self.xdir = -self.xdir
- if self.rect.centery <= 15 or self.rect.centery >= screen.get_height() - 55:
- self.ydir = -self.ydir
- # Treasure Template
- class Treasure (pygame.sprite.Sprite):
- def __init__(self, xCoord, yCoord):
- pygame.sprite.Sprite.__init__(self)
- # Create and load picture for treasure
- self.image = pygame.Surface((30, 30))
- shapeImage = pygame.image.load("TREASURE IMAGE").convert()
- self.image.blit(shapeImage, (0, 0))
- self.image.set_colorkey((255, 255, 255))
- # Set it's bounding box and coordinates
- self.rect = self.image.get_rect()
- self.rect.centery = yCoord
- self.rect.centerx = xCoord
- # Player object template
- class Player (pygame.sprite.Sprite):
- def __init__(self, xCoord, yCoord):
- pygame.sprite.Sprite.__init__(self)
- # Load and set image for player
- self.image = pygame.Surface((30, 30))
- shapeImage = pygame.image.load("PLAYER IMAGE").convert()
- self.image.blit(shapeImage, (0, 0))
- self.image.set_colorkey((255, 255, 255))
- # Set bounding box and location for player
- self.rect = self.image.get_rect()
- self.rect.centery = yCoord
- self.rect.centerx = xCoord
- # Start pygame engine
- pygame.init()
- clock = pygame.time.Clock()
- # Create a 800x600 window and name it "Treasure Hunt"
- window = pygame.display.set_mode((800, 600))
- pygame.display.set_caption("Treasure Hunt")
- screen = pygame.display.get_surface()
- # Define font to be used in displaying score and text objects
- font = pygame.font.SysFont("CHOOSE YOUR FONT", 25)
- # Define score and lives variables
- score = 0
- lives = 40
- # Group containing all the enemies
- enemies = pygame.sprite.Group()
- # Group containing all the treasures
- treasures = pygame.sprite.Group()
- # Create 10 Enemies
- for i in range(10):
- # Assign a random x,y coordinate within the game area
- xCoord = random.randrange(15, screen.get_width() - 15)
- yCoord = random.randrange(15, screen.get_height() - 55)
- # Assign a random direction and speed
- xdir = random.choice([-1, 1])
- ydir = random.choice([-1, 1])
- speed = random.randrange(4, 11)
- # Create and add the new enemy to the list of enemies
- enemy = Enemy(xCoord, yCoord, xdir, ydir, speed)
- enemies.add(enemy)
- # Create 30 Treasure locations
- for i in range(30):
- # Assign a random x,y location within the game area
- xCoord = random.randrange(15, screen.get_width() - 15)
- yCoord = random.randrange(15, screen.get_height() - 55)
- # Create and add the new treasure object to the list of treasures
- treasure = Treasure(xCoord, yCoord)
- treasures.add(treasure)
- # Create the player sprite
- player = Player(screen.get_width() // 2, screen.get_height() - 55)
- # Clear the screen for the first frame
- screen.fill((255, 255, 255))
- # Draw and move all of the enemies
- enemies.draw(screen)
- enemies.update()
- # Draw all of the treasure objects
- treasures.draw(screen)
- # Draw the player
- screen.blit(player.image, player.rect)
- # Draw the bottom display for score and livesr
- pygame.draw.rect(screen, THECOLORS['yellow'], [0, screen.get_height() - 40, screen.get_width(), 40])
- text = font.render("Life: {} {} Score: {}".format(lives, " " * 10, score), True, THECOLORS['black'])
- screen.blit(text, (screen.get_width() // 2 - text.get_width() // 2, screen.get_height() - 40))
- # The game has not finished
- gameOver = False
- # Allow the game to keep going
- keepGoing = True
- while keepGoing:
- clock.tick(30) # FPS
- # What to do if the game has not yet ended
- if not gameOver:
- # Clear the screen
- screen.fill((255, 255, 255))
- # Draw and move all of the enemies
- enemies.draw(screen)
- enemies.update()
- # Draw all of the treasure objects
- treasures.draw(screen)
- # Draw the player
- screen.blit(player.image, player.rect)
- # Draw the bottom display for lives and score
- pygame.draw.rect(screen, THECOLORS['yellow'], [0, screen.get_height() - 40, screen.get_width(), 40])
- font = pygame.font.SysFont("comicsansms", 25)
- text = font.render("Life: {} {} Score: {}".format(lives, " " * 10, score), True, THECOLORS['black'])
- screen.blit(text, (screen.get_width() // 2 - text.get_width() // 2, screen.get_height() - 40))
- # Flip the drawn objects into view
- pygame.display.flip()
- # Get pressed keys
- keys = pygame.key.get_pressed()
- # If the up key is pressed move the player object up as long as it is within the game area
- if keys[pygame.K_UP] and player.rect.centery > 15:
- player.rect.centery -= 5
- # If the down key is pressed move the player down as long as it is within the game area
- elif keys[pygame.K_DOWN] and player.rect.centery < screen.get_height() - 55:
- player.rect.centery += 5
- # If the left key is pressed move the player left as long as it is within the game area
- elif keys[pygame.K_LEFT] and player.rect.centerx > 15:
- player.rect.centerx -= 5
- # If the right key is pressed move the player right as long as it is within the game area
- elif keys[pygame.K_RIGHT] and player.rect.centerx < screen.get_width() - 15:
- player.rect.centerx += 5
- # Search for any collision on the player with treasure objects
- for treasure in treasures:
- # Check if the current treasure object being polled is in collision with the player
- collided = pygame.sprite.collide_rect(player, treasure)
- if collided:
- # If it is add one to the score and remove that treasure object
- score = score + 1
- treasures.remove(treasure)
- # Search for any collision on the player with enemies
- for enemy in enemies:
- # Check if the current enemy being polled is in collision with the player
- collided = pygame.sprite.collide_rect(player, enemy)
- if collided:
- # If it is reset the players position and remove one life
- lives = lives - 1
- player.rect.centerx = screen.get_width() // 2
- player.rect.centery = screen.get_height() - 55
- # Check if the player has either won or lost the game
- if score >= 30 or lives <= 0:
- gameOver = True
- pygame.time.delay(20)
- else:
- # If the player has won or lost
- # Clear the screen
- screen.fill((255, 255, 255))
- # Draw and display the bottom bar for score and lives
- pygame.draw.rect(screen, THECOLORS['yellow'], [0, screen.get_height() - 40, screen.get_width(), 40])
- text = font.render("Life: {} {} Score: {}".format(lives, " " * 10, score), True, THECOLORS['black'])
- screen.blit(text, (screen.get_width() // 2 - text.get_width() // 2, screen.get_height() - 40))
- # If the player has won
- if score >= 30:
- # Draw and display a winning text
- text = font.render("Congratulations! You got all the treasure!", True, THECOLORS['green'])
- screen.blit(text, (screen.get_width() // 2 - text.get_width() // 2, \
- screen.get_height() // 2 - text.get_height() // 2))
- else:
- # If the player lost, draw and display a try again text
- text = font.render("Sorry, you died. Better luck next time!", True, THECOLORS['red'])
- screen.blit(text, (screen.get_width() // 2 - text.get_width() // 2, \
- screen.get_height() // 2 - text.get_height() // 2))
- # Flip the drawn objects into view
- pygame.display.flip()
- # Event loop
- events = pygame.event.get()
- for ev in events:
- if ev.type == QUIT:
- keepGoing = False # Stop the program, it's detected quit...
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement