All pastes #1667068 Raw Edit

platformer

public text v1 · immutable
#1667068 ·published 2009-11-11 21:23 UTC
rendered paste body
#####Initialize and Import
import os, pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

#####Classes#####
class Game(object):

    def __init___(self):
        
        self.clock = pygame.time.Clock()

        self.camera = pygame.Rect (0, 0, self.display.get_width(), self.display.get_height())

        self.background = pygame.image.load(os.path.join("images", "background.png")).convert()

        # Lists for various game entities
        self.enemies = []

        self.platforms = []

        self.items = []

        self.entities = [self.enemies, self.platforms, self.items]



class Platform(object): #Base class for a platform

    def __init__(self, pos): #Takes a list with starting x, y values for platform

        #Image is loaded in specific platform classes
        self.pos = pos  #The position for the platform in the level

        self.draw_pos = [0, 0] #The position for drawing the platform on the screen
        
        self.rect = pygame.Rect(self.pos[0], self.pos[1] + 80, 101, 91) #Creates a collision rect for platform

        self.onscreen = False

        game.platforms.append(self) #Adding the platform to list of entities
        
        
    def update(self):  #Changes platform location based on its offset

        self.draw_pos[0] = self.pos[0] - game.camera.x
        self.draw_pos[1] = self.pos[1] - game.camera.y

        
class Dirt(Platform):  #Dirt platform

    def __init__(self, pos):
        self.image = pygame.image.load(os.path.join("images", "Dirt Block.png")).convert_alpha()
        Platform.__init__(self, pos)

class Grass(Platform):  #Grass platform
    
    def __init__(self, pos):
        self.image = pygame.image.load(os.path.join("images", "Grass Block.png")).convert_alpha()
        Platform.__init__(self, pos)
        
class Stone(Platform):  #Stone platform
    
    def __init__(self, pos):
        self.image = pygame.image.load(os.path.join("images", "Stone Block.png")).convert_alpha()
        Platform.__init__(self, pos)




class Gem(object):  # A class for gems

    def __init__(self, pos):

        self.image = pygame.image.load(os.path.join("images", "Gem Blue.png")).convert_alpha()
        self.image = pygame.transform.smoothscale(self.image, (50, 85))
        
        self.pos = pos

        self.draw_pos = [0, 0]

        self.offset = [0, 0]

        self.rect = pygame.Rect(self.pos[0], self.pos[1]+29, 50, 53)

        self.onscreen = False

        game.items.append(self)

    def update(self):
        
        self.draw_pos[0] = self.pos[0] - game.camera.x
        self.draw_pos[1] = self.pos[1] - game.camera.y

        self.on_collision()

    def on_collision(self):

        if player.rect.colliderect(self.rect):
            player.score += 25
            game.items.remove(self)



class Player: #Class for the player

    def __init__(self):

        self.draw_pos = [50, 100] #A list with the position the player should be drawn on the screen

        self.offset = [34, 71]  # A list containing the x and y offset for positioning the image
    
        self.jumping = False # Bool value for checking if the player is jumping

        self.vel_y = 0 # Holds the player's y velocity while jumping

        self.accel = .55 # The constant value for the player's jump acceleration    

        self.image = pygame.image.load(os.path.join("images", "Character Horn Girl.png")).convert_alpha() #Loads the player image

        self.rect = pygame.Rect(self.draw_pos[0] + self.offset[0], self.draw_pos[1] + self.offset[1], 33, 78) #Creates a collision rect for the player sprite

        self.score = 0  # Holds the player's score
        
    def update(self):
        
        # Update position based on key input
        pressed_keys = pygame.key.get_pressed()

        if pressed_keys[K_a]:
            self.move(-4, 0)

        elif pressed_keys[K_d]:
            self.move(4, 0)

        if pressed_keys[K_SPACE] and self.jumping == False:
            self.jumping = True
            self.vel_y = -12

        # Move up and adjust y velocity if jumping, otherwise apply gravity constant
        if self.jumping:
            self.vel_y += self.accel
            self.move(0, self.vel_y)
        else:
            self.move(0, 10)
        
        

    def move_single_axis(self, dx, dy):
        
        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        #Change draw position based on new rect position

        self.draw_pos[0] = self.rect.x - self.offset[0] - game.camera.x
        self.draw_pos[1] = self.rect.y - self.offset[1] - game.camera.y
        

        # If collide with an object, move out along the same axis you hit with
        # Notice the bottom collision also stops jumping

        for platform in level_list:
            if self.rect.colliderect(platform.rect):
                if dx > 0:
                    self.rect.right = platform.rect.left
                if dx < 0:
                    self.rect.left = platform.rect.right
                if dy > 0:
                    self.rect.bottom = platform.rect.top
                    self.jumping = False
                if dy < 0:
                    self.rect.top = platform.rect.bottom
    
    def move(self, dx, dy):

        # Move and check collision for each axis separately
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)
        

        
#####Drawing Level#####

game = Game()

print game.entities

level = [
"                      ",
"                      ",
"                      ",
"                      ",
"                      ",
"     M    S           ",
"GGG GG  GGS   DDD     ",
"                      ",
]

x = 0
y = 20
for row in level:
    for col in row:
        if col == "G":
            Grass([x, y])
        if col == "S":
            Stone([x, y])
        if col == "D":
            Dirt([x, y])
        if col == "M":
            Gem([x + 25, y + 41])
        x += 101
    y += 43
    x = 0
entity_list.reverse()


#####Game Loop#####
player = Player()

while True:

    game.clock.tick(60)
    
    for event in pygame.event.get(): #Exit event
        if event.type == QUIT:
            exit()


    screen.blit(game.background, (0, 0))

    for entity in entity_list:
        entity.update()
        screen.blit(entity.image, entity.draw_pos)
        #pygame.draw.rect(screen, (255, 0, 0), entity.rect, 1)
        
    player.update()

    game.camera.centerx = player.rect.centerx
    if game.camera.left < 0:
        game.camera.left = 0

    default_font = pygame.font.SysFont("arial", 12)
    hud_score = default_font.render(str(player.score), True, (0, 0, 0))

    #pygame.draw.rect(screen, (0, 255, 0), camera, 1)

    screen.blit(player.image, player.draw_pos)
    
    #pygame.draw.rect(screen, (0, 0, 255), player.rect, 1)

    screen.blit(hud_score, (0, 0))

    pygame.display.update()