This topic is READ ONLY
AshwinM (2) [Avatar] Offline
#1
Dear Sir,
I recently bought your book "Hello World!" and it was awesome!!! I tried to create a game in Python and I did the following:

"import pygame, sys

class PlayerCarClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init(self)
self.image = pygame.image.load("player car.png")
self.rect = self.image.get_rect()
self.rect.center = [120, 80]
self.fly == False
self.size = size

def move(self, speed):
self.rect.centerx = self.rect.centerx + speed[0]
if self.rect.left <= 0: self.rect.left = 0
if self.rect.right >= 120: self.rect.right = 120

class PoliceCarClass(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.location = location
self.rect = self.image.get_rect()
self.rect.center = location
self.passed = False

def scroll(self, t_ptr):
self.rect.centery = self.location[1] - t_ptr

def create_map(start, end):
police_cars = pygame.sprite.Group()
gates = pygame.sprite.Group()
locations = []
for i in range(1, 3):
col = random .randint(1, 3)
location = [0, col * 40 - 20]
if not (location in locations):
locations.append(location)
img = "policecar.png"
policecar = PoliceCarClass( img, location)
policecars.add(policecar)
return policecars

def animate(loc_x, loc_y):
screen.fill ([144, 144, 144])
pygame.display.update(policecars.draw(screen))
screen.blit(playercar.image, playercar.rect)
screen.blit(road_stripes)
screen.blit(score_text, [10, 10])
screen.blit(fly_text, [80, 10])
pygame.display.flip()

def updatePoliceCarGroup(map0, map1):
policecars = pygame.sprite.Group()
for pc in map0: policecars.add(pc)
for pc in map1: policecars.add(pc)
return obstacles

pygame.init()
screen = pygame.display.set_mode([120, 240])
clock = pygame.time.Clock()
playercar = PlayerCarClass()
playercar.size == playercarsize
map_position = 0
speed = [0, 1]
points = 0
fly = 0
road_stripe = 20
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0
obstacles = updatePoliceCarGroup(map0, map1)
font = pygame.font.Font(None, 30)

while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playercar.location[0] -= 5
if event.key == pygame.K_RIGHT:
playercar.location[0] += 5
if event.key == pygame.K_UP:
playercar.fly == True
for fly_loop in range(5):
pygame.time.delay(500)
playercar.size += 1
if playercar.size == playercarsize + 5:
pygame.time.delay (3000)
for fly_reverse_loop in range(5):
pygame.time.delay(500)
playercar.size -= 1
playercar.fly == False

playercar.move(speed)
map_position += speed[1]

for road_stripe in range(0, 241, 20):
pygame.draw.rect(screen, [255, 255, 255], [38, road_stripe, 4, 15])
road_stripes.append(road_stripe)

for rode_stripe in range(0, 241, 20):
pygame .draw.rect(screen, [255, 255, 255], [78, rode_stripe, 4, 14])
rode_stripes.append(rode_stripe)



if map_position >=240 and activeMap == 0:
activeMap = 1
map0 = create_map(20, 29)
policecars = updatePoliceCarGroup(map0, map1)
if map_position >= 480 and activeMap == 1:
activeMap = 0
for pc in map0:
pc.location[1] = pc.location[1] - 1280
map_position = map_position - 1280
map1 = create_map(10, 19)
policecars = udatePoliceCarGroup(map0, map1)

for policecar in policecars:
policecar.scroll(map_position)
hit = pygame.sprite.spritecollide(playercar, policecars, False)
if hit:
if playercar.fly == False and not hit[0].passed:
playercar.image = "explosion.png"
hit[0].image = "explosion.png"
animate()
hit[0].passed = True

for policecar in policecars:
policecars.remove(policecar)
if policecar.location[1] >= 240:
policecar.passed == True
points += 1
fly += 0.1
flypower = int(fly)

score_text = font.render("Score: " +str(points), 1, (0, 0, 0))
fly_text = font.render("Fly: " +str(flypower), 1, (0, 0, 0))
animate()"


---with help from the Skier.
However, when I ran it, I recieved the following message
"Traceback (most recent call last):
File "C:/Documents and Settings/FOX/Desktop/Python - Car Game/F1_RACE.py", line 62, in <module>
playercar = PlayerCarClass()
File "C:/Documents and Settings/FOX/Desktop/Python - Car Game/F1_RACE.py", line 5, in __init__
pygame.sprite.Sprite.__init(self)
AttributeError: type object 'Sprite' has no attribute '_PlayerCarClass__init' "

I was not able to figure out why. I saved the pictures in the same folder and the Indentation was also fine.
But it refuses to work.
Please be kind enough to give me an answer and send it to the following email id
"achuaditi@gmail.com".

I also bought the book "Game Development with Python and Pygame" as it was prescribed in your book. However, I was unable to understand most of the stuff. If you would only write a second part which covers game development in 3d, I'll be one of the most grateful

Yours sincerely,
Ashwin M
warren.sande (211) [Avatar] Offline
#2
Re: A small doubt...
Ashwin,

Thanks for the kind comments about our book.

We can't really look at your code properly, since this forum strips out the whitespace.

If you could send it as an attachment to cp4khelp@yahoo.com, we'll try to have a look at it.


Regards,
Warren Sande