Reputation: 441
I am creating a game like pacman in pygame using python. I want to assign a image of a jewel to the jewel
class. In the maze 'J' Represents the jewel
. So just to make it clear how would i assign an image to the Jewel Class so that all the J's in the maze map are that image?
The class for the jewel is
class Jewel(object):
"""Class to hold Jewel sprite properties"""
def __init__(self, pos):
jewels.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
The class for the wall and maze is
class Wall(object):
"""Class to hold Wall sprite properties"""
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
walls = [] #List to contain walls
jewels = []#List to contain Jewels
#!-----------------------Maze Layout----------------------!
#Table used to create the level, where W = wall
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W J W JJJ W",
"W W W W",
"W WWWW WWWWW WWWWW W WWWWWWWWWW W",
"W W W W W W W",
"W W JJJJ W WWWWWWW W W",
"W W WW W W W",
"W W W WWWW W W W",
"W WWW W W W W W WWWWW W",
"W W W W W",
"WWWW WWWWWWWWWWWWWWWWWWWWWW W",
"W W WW W W",
"W WW W WWWWWWWWWWWWWWWWW",
"W WWW W W",
"W WW WW WWW W W",
"W W WWW WWWWWWW WWWWWWWWWWWWWWWW W",
"W W W WWW WW W W W",
"W WW W W W W",
"W W W WWWWWWWWWWWWWWWWWWWWWWWW W W",
"WWWW WWWWW WW WWW W W",
"W W W W WWWWWWWWWWWWWWWWW W",
"W W WWWW W W W W W",
"W W W WWW W W W",
"W WWWW W W WWWWWWWWWWWWWWWWWWWWWWW",
"W WW W WWW W W JJJ W",
"W W W W W J W",
"W W WWW W WWW W",
"W WWW WWWWWWWWWW WWWW W",
"W W W WWWWWWW W W",
"WW W WWWW WWWWWW WWWW WWWW W",
"WJ W W W W W",
"W W J WWW WWWWW W W WWWW",
"WWW W W W W WWWW W",
"W W WWWWWW WWWW W W W",
"W WWW W W WWWWW WWWW WWWWW W",
"W WWW W W W W",
"W W W W W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
#Draw the wall rects as shown in the table above
x = y = 0
for row in level:
for column in row:
if column == "W":
Wall((x, y))
if column == "J":
Jewel((x, y))
x += 16
y += 16
x = 0
#Draw walls and jewels
for wall in walls:
pygame.draw.rect(screen, (WHITE), wall.rect)
for jewel in jewels:
pygame.draw.rect(screen, (BLUE), jewel.rect)
Upvotes: 3
Views: 21905
Reputation: 110186
Just add the image as an attribute for your objects -
for example, you can load them at the __init__
method like:
def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
self.image = pygame.image.load("/path/to/image_file.png")
and then, on your game loop, instead of drawing a rect, call the
blit
method of the "screen" (which is a pygame.Surface
object) passing the image:
for jewel in jewels:
screen.blit(jewel.image, jewel.rect)
Later on when you have got a little more structure on the game, you should put your game objects in specialized sprite groups which can then make use of the .image
and .rect
attributes of the sprites to blit them to screen when the group draw
method is called.
(check the docs on http://www.pygame.org/docs/ref/sprite.html), instead of calling blit
directly.
Upvotes: 9