Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I recently downloaded a game and under level.py this is the code. Im lost with this code can someone please help!

Python
#! /usr/bin/env python
import pygame, os
from pygame.locals import *
from sprites import *

def load_level(file):
    img = pygame.image.load(os.path.join("data", file))
    return img.convert()

class Level:

    def __init__(self, level=1):

        self.level = load_level("level.png")

        platform_color = (0, 0, 0, 255)
        collidables = [platform_color]
        self.x = self.y = 0

        while self.y < self.level.get_height():
            color = self.level.get_at((self.x, self.y))
            if color == (0, 255, 0, 255):
                Monster((self.x*24, self.y*24))
            if color == (255, 0, 0, 255):
                HealthUp((self.x*24 + 4, self.y*24 + 4))
            if color == (255, 255, 0, 255):
                Chemical((self.x*24, self.y*24))
            if color == (255, 0, 255, 255):
                AmmoUp((self.x*24, self.y*24))
            if color in collidables:
                top = bottom = left = right = False
                if self.get_tile_above() not in collidables:
                    top = True
                if self.get_tile_under() not in collidables:
                    bottom = True
                if self.get_tile_left() not in collidables:
                    left = True
                if self.get_tile_right() not in collidables:
                    right = True
                Block((self.x*24, self.y*24), top, bottom, left, right)

            self.x += 1
            if self.x >= self.level.get_width():
                self.x = 0
                self.y += 1

    def get_size(self):
        return [self.level.get_size()[0]*24, self.level.get_size()[1]*24]

    def get_tile_above(self):
        try:
            return self.level.get_at((self.x, self.y-1))
        except:
            pass
    def get_tile_under(self):
        try:
            return self.level.get_at((self.x, self.y+1))
        except:
            pass
    def get_tile_left(self):
        try:
            return self.level.get_at((self.x-1, self.y))
        except:
            pass
    def get_tile_right(self):
        try:
            return self.level.get_at((self.x+1, self.y))
        except:
            pass
Posted
Updated 30-Jan-12 4:59am
v2
Comments
LanFanNinja 30-Jan-12 10:59am    
Sorry I was going to try and write some comments in the code for you but I ran out of time to do so.

The code doesn't seem very complex a few tutorials should get you up to speed fairly quickly.

Google pygame tutorials and also 2d game development tutorials.

Make sure you have learned the python language and have at least written a few simple programs with it first before attempting game development with pygame as not doing so is a sure fire way to have a nervous breakdown.

NOTE:
If you know zero programming I seriously recommend you take a couple of steps back and really learn programming in general first. Game programming even 2D game programming is very hard to do! Trying to go in blindly with no programming knowledge is hazardous to your health and will surely end badly for you.

Good luck.
Sergey Alexandrovich Kryukov 30-Jan-12 12:00pm    
Agree. This is a bad kind of question, not worth answering. OP approaches the problem from wrong side.
--SA
Kreagan Naicker 30-Jan-12 16:42pm    
This code snippet is taken out of context which makes it almost impossible to provide any usable input.

Also the question is extremely vague.
What is the ultimate objective?
If learning pygame is the objective, then i would second what LanFanNinja has already pointed out in terms of difficulty and being an intermediate developer at least.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900