Tänkte börja med ett monopolspel då det innehåller många men ganska enkla moment.
Efter att ha läst några tutorialer som behandlar grunder samt en om sprites har jag börjat koda.
Problemet är att jag får följande fel:
Kod: Markera allt
File "/usr/lib/python2.6/dist-packages/pygame/sprite.py", line 325, in add
self.add(spr)
File "/usr/lib/python2.6/dist-packages/pygame/sprite.py", line 325, in add
self.add(spr)
File "/usr/lib/python2.6/dist-packages/pygame/sprite.py", line 325, in add
self.add(spr)
File "/usr/lib/python2.6/dist-packages/pygame/sprite.py", line 316, in add
if isinstance(sprite, Sprite):
RuntimeError: maximum recursion depth exceeded while calling a Python object
Koden:
Kod: Markera allt
#!/usr/bin/env python
import os, sys
import pygame
from board import board
class MonopolMain:
"""The Main Monopol Class - This class handles the main
initialization and creating of the Game."""
def __init__(self, width=640,height=640):
"""Initialize PyGame"""
pygame.init()
"""Set the window Size"""
self.width = width
self.height = height
"""Create the Screen"""
self.screen = pygame.display.set_mode((self.width
, self.height))
def MainLoop(self):
self.LoadSprites();
pygame.display.flip()
while pygame.event.poll().type != KEYDOWN:
screen.fill([0, 0, 0]) # blank the screen.
for b in board_img_list.sprites(): screen.blit(b.image, b.rect)
pygame.display.update()
def LoadSprites(self):
self.path = os.path.join('data', 'images')
"""Load all of the sprites that we need"""
board_img_list = pygame.sprite.Group()
for image, location in [("0.bmp", [0, 0]),
("1.bmp", [84, 0]),
("2.bmp", [141, 0]),
("3.bmp", [198, 0]),
("4.bmp", [255, 0]),
("5.bmp", [312, 0]),
("6.bmp", [369, 0]),
("7.bmp", [426, 0]),
("8.bmp", [483, 0]),
("9.bmp", [540, 0]),
("10.bmp", [597, 0]),
("11.bmp", [598, 85]),
("12.bmp", [598, 142]),
("13.bmp", [598, 199]),
("14.bmp", [598, 256]),
("15.bmp", [598, 313]),
("16.bmp", [598, 370]),
("17.bmp", [598, 427]),
("18.bmp", [598, 483]),
("19.bmp", [598, 541]),
("20.bmp", [597, 598]),
("21.bmp", [540, 598]),
("22.bmp", [483, 598]),
("23.bmp", [426, 598]),
("24.bmp", [369, 598]),
("25.bmp", [312, 598]),
("26.bmp", [255, 598]),
("27.bmp", [198, 598]),
("28.bmp", [141, 598]),
("29.bmp", [84, 598]),
("30.bmp", [0, 598]),
("31.bmp", [0, 541]),
("32.bmp", [0, 483]),
("33.bmp", [0, 427]),
("34.bmp", [0, 370]),
("35.bmp", [0, 313]),
("36.bmp", [0, 256]),
("37.bmp", [0, 199]),
("38.bmp", [0, 142]),
("39.bmp", [0, 85]),
("board.bmp", [84, 85])]:
board_img_list.add(image, location, self.path)
if __name__ == "__main__":
MainWindow = MonopolMain(670,675)
MainWindow.MainLoop()
Kod: Markera allt
import pygame
class board(pygame.sprite.Sprite):
def __init__(self, image, position, path):
pygame.sprite.Sprite.__init__(self)
# Load the image
image = os.path.join(path, image)
self.image = pygame.image.load(image)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.topleft = initial_position
