• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Aku Lalala

Beginner - How to make walkable/not walkable wall

2 posts in this topic

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
[url="http://arvinbadiola.wordpress.com/2012/01/22/rendering-background-image-using-pygame-in-python-2-7/"]i.am.arvin[/url]. 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 :)

[CODE]
# -*- 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()
[/CODE]
0

Share this post


Link to post
Share on other sites
A solution could be making your MAP ítem (p, g, s,...) a dict in the form {Rect, boolean}, meaning the boolean that you can visit that tile or not. If you need more info for your tiles, you can use an int instead of a boolean.
0

Share this post


Link to post
Share on other sites
hi,

thanks for your quick reply. i manage to solve this problem. but i dont know is it the right way of doing this for tile based game.
any way this is my new code. i just draw a rectangle before i draw the tile map, then i use the rectangle for collision detection. is it correct?

[CODE]
# -*- 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()
#pygame.key.set_repeat(1000,1000)
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 --------------------------------------
class Wall(pygame.sprite.Sprite):
def __init__(self,height,width,x,y):
pygame.sprite.Sprite.__init__(self)
# Make a blue wall, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill((0,0,255))
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
# ------------------------------------ end function to render tile --------------------------------
# ---------------------------------------- bird class ------------------------------------------------
class Bird(pygame.sprite.Sprite):
def __init__(self, startpos=(48, 208)):
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.dy)
self.old_x = self.rect.centerx
self.old_y = self.rect.centery
def moves(self, dx, dy, wall):
self.wall = wall
self.old_x =self.dx
self.old_y = self.dy
self.dx += dx
self.dy +=dy
self.rect.center = (self.dx, self.dy)
collide = pygame.sprite.spritecollide(self,self.wall,False)
if collide:
self.rect.center = (self.old_x, self.old_y)
self.dx = self.old_x
self.dy = self.old_y
print 'collide'
def RenderTiles(self):
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])
def update(self, second):
self.seconds = seconds
self.rect.center = (self.dx, self.dy)
self.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()
# ----------------------------------------------------------------------------------------------------------------
# Make the walls. (height, width, x_pos, y_pos)
wall_list=[]
for y in range(len(MAP)):
for x in range(len(MAP[y])):
location = (x*32, y*32)
#print MAP[y][x].topleft
tl = MAP[y][x].topleft[0]
if tl == 96:
wall_list.append(Wall(32,32,x*32,y*32))

wallSprites = pygame.sprite.Group(wall_list)
# ----------------------------------------------------------------------------------------------------------------

# 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, wallSprites)
if e.key == K_DOWN:
b.moves(0, 32, wallSprites)
if e.key == K_RIGHT:
b.moves(32, 0, wallSprites)
if e.key == K_LEFT:
b.moves(-32, 0, wallSprites)
birdgroup.clear(screen, background)
birdgroup.update(seconds)
birdgroup.draw(screen)
pygame.display.flip()
pygame.quit()
[/CODE]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0