Jump to content

  • Log In with Google      Sign In   
  • Create Account

Biskut Merry

Member Since 18 Sep 2012
Offline Last Active Oct 31 2012 05:07 AM

Topics I've Started

2d platforming game using pygame

25 September 2012 - 03:50 AM

hi all,

i am new to this forum and also in game programming world. i'm trying to create
a 2d platforming game using pygame, but don't know how to do it. i don't know
how to properly create the map for each level, then load the sprite images
based on the map. i couldn't find any tutorials on how to do it.

hope someone here can help me explain the concepts or point me somewhere
on the net where i can learn the concept of doing this kind of thing using pygame.

i still learning pygame, build up a solid base in pygame but at the same time
would like to know how do people create those kind of map thing's in their game.
i couldn't find it anywhere on the net, try to understand from other people's code
from the game i downloaded but couldn't get the idea, maybe the code is to
advanced for me.

it would be very nice if i can get help from here.

thanks :-)

Beginner - How to make walkable/not walkable wall

18 September 2012 - 03:48 AM

hi all,

i'm just a newbie here and in pygame. i could'nt find any simple/easy tutorial of making walkable/not walkable wall.
here's my example code. i manage to create the background map using the tile-sprite image, based on tutorial from
i.am.arvin. i also add a bird as the player, but my problem now is i dont know how to make the wall (in the background)
walkable/not walkable.

hope someone can give idea how can this be done :)

# -*- coding: utf-8 *-*
import pygame
from pygame import *
import map1
import os
DEFAULT_SCREENSIZE = [512, 448] #16 X 14 grid with 32px X 32px cell
pygame.init()
screen = pygame.display.set_mode(DEFAULT_SCREENSIZE)
display.set_caption('Tile Based')
# -------------------------------- map source for background -------------------------------------
SOURCE = pygame.image.load('images/TileA4.png')
p = pygame.Rect(288, 0, 32, 32) #area of source image containing pavement
g = pygame.Rect(416, 0, 32, 32) #area of source image containing grass
s = pygame.Rect(288, 160, 32, 32) #area of source image containing sand/dirt
b = pygame.Rect(288, 320, 32, 32) #area of source image containing bush
w = pygame.Rect(96, 0, 32, 32) #area containing brick wall
t = pygame.Rect(288, 320, 32, 32) #area of source image containing grass
MAP = [[w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w],\
		    [w,t,t,t,w,t,t,t,w,t,w,t,w,t,t,w],\
		    [w,t,w,t,w,t,w,t,t,t,w,t,w,t,t,w],\
		    [w,t,w,t,w,t,w,t,w,t,w,t,w,t,t,w],\
		    [w,t,w,t,w,t,w,t,w,t,t,t,t,t,w,w],\
		    [w,t,w,t,t,t,w,t,w,t,w,t,t,w,t,w],\
		    [w,g,w,w,w,w,w,w,w,w,w,w,w,t,g,w],\
		    [w,t,w,t,t,t,w,t,w,t,t,t,t,t,w,w],\
		    [w,t,w,t,w,t,w,t,t,t,w,w,w,t,w,w],\
		    [w,t,w,t,w,t,w,t,w,t,t,t,t,t,t,w],\
		    [w,t,w,t,w,t,t,t,w,t,w,t,w,t,t,w],\
		    [w,t,w,t,w,w,w,t,w,t,t,w,t,w,t,w],\
		    [w,t,t,t,w,w,w,t,w,w,t,t,t,t,t,w],\
		    [w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w]]
# ------------------------------------ function to render tile --------------------------------------
def RenderTiles():
    for y in range(len(MAP)):
	    for x in range(len(MAP[y])):
		    location = (x*32, y*32)
		    screen.blit(SOURCE, location, MAP[y][x])
# ------------------------------------ end function to render tile --------------------------------
# ---------------------------------------- bird class ------------------------------------------------
class Bird(pygame.sprite.Sprite):
    def __init__(self, startpos=(32, 192)):
	    pygame.sprite.Sprite.__init__(self, self.groups)
	    self.image = pygame.image.load(os.path.join('images', 'babytux.png'))
	    self.image = self.image.convert_alpha()
	    self.rect = self.image.get_rect()
	    self.startpos = startpos
	    self.dx = self.startpos[0]
	    self.dy = self.startpos[1]
	    self.move = True
	    self.rect.center = (self.dx+self.rect.width/2, self.dy+self.rect.height/2)
    def moves(self, dx, dy):
	    if self.dx + dx <= 10 or self.dy + dy <= 10 or\
	    self.dx + dx >= 480 or self.dy + dy >= 416:
		    self.move = False
	    else:
		    self.move = True
	    if self.move:
		    self.dx += dx
		    self.dy += dy
    def update(self, second):
	    self.seconds = seconds
	    self.rect.center = (self.dx+self.rect.width/2, self.dy+self.rect.height/2)
	    RenderTiles()
# ------------------------------- end class bird ------------------------------------------------
FPS = 60
clock = pygame.time.Clock()
play = True
background = pygame.Surface(screen.get_size())
screen.blit(background, (0,0))
birdgroup = pygame.sprite.Group()
Bird.groups = birdgroup
b = Bird()
# main loop start
while play:
    miliseconds = clock.tick(FPS)
    seconds = miliseconds/1000
    for e in event.get():
	    if e.type == QUIT:
		    play = False
	    if e.type == KEYDOWN:
		    if e.key == K_ESCAPE:
			    play = False
		    if e.key == K_UP:
			    b.moves(0, -32)
		    if e.key == K_DOWN:
			    b.moves(0, 32)
		    if e.key == K_RIGHT:
			    b.moves(32, 0)
		    if e.key == K_LEFT:
			    b.moves(-32, 0)
    birdgroup.clear(screen, background)
    birdgroup.update(seconds)
    birdgroup.draw(screen)
    pygame.display.flip()
pygame.quit()

PARTNERS