How do I do this? In JustBasic I simply read from a text file and coverted the letter it found into an image that it loaded. I want it to load the image I need with this class:

def load_image(name, colorkey=None):
    """When this statement is accessed, coupled with the name and key,
    the specified image is imported, and stored"""
    fullname = os.path.join('Data\RPG', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

and read each line of a text file into a list, splitting at the ","s then using the position of the character in the list with the tile size (16) to place them on the background.
y = 0

y = y+1 tilex = (line[i]+1) * 16 tiley = y * 16 TEXT FILE: G,G,G G,G,G G,G,G if line[i] = G then place grass image elif line[i] = ........ repeat for each different tile. As it stands I only have the one tile right now. How would I implement this?[code to read the line]
y = y+1

tilex = (line+1) * 16
tiley = y * 16


TEXT FILE:
G,G,G
G,G,G
G,G,G

if line = G then place grass image
elif line = ........
repeat for each different tile. As it stands I only have the one tile right now. How would I implement this?

Recommended Answers

All 7 Replies

Here's how I've tried to make it work. How can I fix it?

txtfile = GGG
            GGG
            GGG
#RPG.py
#Copyright Chris O'Leary, 2007
#
#An attempt at making an RPG engine
#Import Modules
import pygame, os
from pygame.locals import *
if not pygame.font:
    print 'Warning, fonts disabled'
if not pygame.mixer:
    print 'Warning, sound disabled'
 
#Define the image importing and sound importing methods
def load_image(name, colorkey=None):
    """When this statement is accessed, coupled with the name and key,
    the specified image is imported, and stored"""
    fullname = os.path.join('Data\RPG', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()
def load_sound(name):
    """When this statement is accessed, coupled with the filename, the
    specified sound is imported and stored."""
    class NoneSound:
        def play(self):
            pass
    if not pygame.mixer:
        return NoneSound()
    fullname = os.path.join('Data\RPG', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error, message:
        print 'Cannot load sound:', wav
        raise SystemExit, message
    return sound
class mapScreen(pygame.sprite.Sprite):
    def __init__(self, mapName):
        pygame.sprite.Sprite.__init__(self)
        if mapName == 'testMap':
            imgGrass = load_image('mapTile1.bmp')
            ypos = 0
            xpos = 0
            fileone = open('Data\RPG\mapData1.txt')
            for line in fileone:
                tilesH = line
                print tilesH
                ypos = ypos + 1
                for char in tilesH:
                    xpos = xpos+1
                    if char == 'G':
                        sprGrass = pygame.sprite.RenderPlain(imgGrass)
                        sprGrass.update()
                        sprGrass.draw(screen)
                        pygame.display.flip()
 
pygame.init()
screen = pygame.display.set_mode((50, 50))
pygame.display.set_caption('RPG')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, (0,0))
mapScreen('testMap')

what should I change?

Somehow ypos and xpos are established but never used.

What are your error messages?

Here's the error message:

Traceback (most recent call last):
File "C:\Python24\PyGamePractice\RPG.py", line 83, in -toplevel-
mapScreen('testMap')
File "C:\Python24\PyGamePractice\RPG.py", line 68, in __init__
sprGrass = pygame.sprite.RenderPlain(imgGrass)
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 368, in __init__
self.add(*sprites)
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 239, in add
elif not self.has_internal(sprite):
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 195, in has_internal
return self.spritedict.has_key(sprite)
TypeError: unhashable type

What does this mean?

I can make it place one tile, but how do I alter it to draw all the specified ones? The Pattern is:

W.W
W.W
W.W
W.W
W.W

My code is:

import pygame, os, sys
from pygame.locals import *
def load_image(name, colourkey = None):
    fullname = os.path.join('Spec\Images', name)
    print fullname
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    if colourkey is not None:
        if colourkey is -1:
            colourkey = image.get_at((0,0))
            image.set_colorkey(colourkey, RLEACCEL)
    return image, image.get_rect()
class Teleport(pygame.sprite.Sprite):
    def __init__(self):
        pass
class Object:
    class Wall(pygame.sprite.Sprite):
        def __init__ (self):
            pygame.sprite.Sprite.__init__(self)
            self.image, self.rect = load_image('Wall.bmp')
 
        def update(self):
            ypos = 0
            xpos = 0
            levelPos = open('Spec\Level 1\Level Layout.txt', 'r')
            for line in levelPos:
                row = line.split(',')
                xpos = xpos+1
                print xpos
                for letter in row:
                    if letter == 'W':
                        ypos = ypos+1
                        print ypos
                        pos = xpos, ypos
                        self.rect.topleft = pos
 
pygame.init()
screen = pygame.display.set_mode((48, 80))
pygame.display.set_caption('Maze Game')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0,0,0))
screen.blit(background, (0,0))
pygame.display.flip()
wall = Object.Wall()
allsprites = pygame.sprite.RenderPlain((wall))
while 1:
    allsprites.update()
    screen.blit(background, (0,0))
    allsprites.draw(screen)
    pygame.display.flip()

How do I make the pattern continue building? It just constantly loops, without drawing anything:
1
2
3
4
5
1
2
3
4
5
...
Is the pattern!

Here's the error message:
Traceback (most recent call last):
File "C:\Python24\PyGamePractice\RPG.py", line 83, in -toplevel-
mapScreen('testMap')
File "C:\Python24\PyGamePractice\RPG.py", line 68, in __init__
sprGrass = pygame.sprite.RenderPlain(imgGrass)
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 368, in __init__
self.add(*sprites)
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 239, in add
elif not self.has_internal(sprite):
File "C:\Python24\Lib\site-packages\pygame\sprite.py", line 195, in has_internal
return self.spritedict.has_key(sprite)
TypeError: unhashable type

imgGrass is a tuple, but its second item imgGrass[1] --> <rect(0, 0, 20, 20)> is not hashable and connot be can not be used as a dictionary key. Also means that pygame.sprite.RenderPlain(imgGrass) does not get the correct argument. Where did you pick this code from?

imgGrass is a tuple, but its second item imgGrass[1] --> <rect(0, 0, 20, 20)> is not hashable and connot be can not be used as a dictionary key. Also means that pygame.sprite.RenderPlain(imgGrass) does not get the correct argument. Where did you pick this code from?

Most of it is from the line-by-line chimp example, but the for loops are my own. So how do I fix the problem?

Here is a small pygame wallpaper sample code ...

# experiments with module pygame
# free from: [url]http://www.pygame.org/[/url]
# load and display an image as a wallpaper
 
import pygame as pg
 
# initialize pygame
pg.init()
 
# pick wallpaper image you have (.bmp  .jpg  .png  .gif)
image_file = "Grass.gif"
 
# set width and height of window
w1 = 300
h1 = 300
 
# create window/screen
screen = pg.display.set_mode((w1, h1))
pg.display.set_caption('Pygame Wallpaper')
 
# load image from a file
image1 = pg.image.load(image_file)
 
# determine size tuple (x, y)
size1 = image1.get_size()
#print size1  # test
 
# draw image, position image ulc at (x, y)
for x in range(0, w1, size1[0]):
    for y in range(0, h1, size1[1]):
        #print x, y  # test
        screen.blit(image1, (x, y))
 
# nothing gets displayed until one updates the screen
pg.display.flip()
 
# start event loop and wait until 
# the user clicks on the window corner x
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.