9 Replies - 3329 Views - Last Post: 15 January 2010 - 02:10 AM Rate Topic: -----

#1 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

python game troubles

Posted 12 January 2010 - 02:16 AM

Hi i was writting a very simple skiing game but i am having some troubles...I have gone through and fixed as many errors as i can, but i can't find the source of this error. IDLE is telling me that;
skier.move(speed)
AttributeError: 'SkierClass' object has no attribute 'move'
I have looked over the code many times but as i am still a beginner to python i have not been able to identify the error.
import pygame, sys, random

skier_images = ["skier_down.png", "skier_right1.png", "skier_right2.png",
				"skier_left2.png", "skier_left1.png"]

class SkierClass(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load("skier_down.png")
		self.rect = self.image.get_rect()
		self.rect.center = [320, 100]
		self.angle = 0

def turn(self, direction):
	self.angle = self.angle + direction
	if self.angle < -2: self.angle = -2
	if self.angle >  2: self.angle = 2
	center = self.rect.center
	self.image = pygame.image.load(skier_images[self.angle])
	self.rect = self.image.get_rect()
	self.rect.center = center
	speed = [self.angle, 6 - abs(self.angle) * 2]
	return speed

def move(self, speed):
	self.rect.centerx = self.rect.centerx + speed[0]
	if self.rect.centerx < 20: self.rect.centerx = 20
	if self.rect.centerx > 620: self.rect.centerx = 620

class ObstacleClass(pygame.sprite.Sprite):
	def __init__(self, image_file, location, type):
		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.type = type
		self.passed = False

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

def create_map(start, end):
	obstacles = pygame.sprite.Group()
	gates = pygame.sprite.Group()
	locations = []
	for i in range(10):
		row = random.randint(start, end)
		col = random.randint(0, 9)
		location = [col * 64 + 20, row * 64 + 20]
		if not (location in locations):
			locations.append(location)
			type = random.choice(["tree", "flag"])
			if type == "tree": img = "skier_tree.png"
			elif type == "flag": img = "skier_flag.png"
			obstacle = ObstacleClass(img, location, type)
			obstacles.add(obstacle)
	return obstacles
def animate():
	screen.fill([255, 255, 255])
	pygame.display.update(obstacles.draw(screen))
	screen.blit(skier.image, skier.rect)
	screen.blit(score_text, [10, 10])
	pygame.display.flip()

def updateObstacleGroup(map0, map1):
	obstacles = pygame.sprite.Group()
	for ob in map0: obstacles.add(ob)
	for ob in map1: obstacles.add(ob)
	return obstacles

pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0, 6]
map_position = 0
points = 0
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0

obstacles = updateObstacleGroup(map0, map1)

font = pygame.font.Font(None, 50)

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:
				speed = skier.turn(-1)
			elif event.key == pygame.K_RIGHT:
				speed = skier.turn(1)
	skier.move(speed)
	map_position += speed[1]

	if map_postition >=640 and activeMap == 0:
		activeMap = 1
		map0 = create_map(20, 29)
		obstacles = updateObstacleGroup(map0, map1)
	if map_position >=1280 and activeMap == 1:
		activeMap = 0
		for ob in map0:
			ob.location[1] = ob.location[1] - 1280
		map_positon = map_positon - 1280
		map1 = create_map(10, 19)
		obstacles = updateObstacleGroup(map0, map1)

	for obstacle in obstacles:
		obstacle.scroll(map_position)

	hit = pygame.sprite.spritecollide(skier, obstacles, False)
	if hit:
		if hit[0].type == "tree" and not hit[0].passed:
			points = points - 100
			skier.image = pygame.image.load("skier_crash.png")
			animate()
			pygame.time.delay(1000)
			skier.image = pygame.image.load("skier_down.png")
			skier.angle = 0
			speed = [0, 6]
			hit[0].passed = True
		elif hit[0].type == "flag" and not hit[0].passed:
			points += 10
			obstacles.remove(hit[0])

	score_text = font.render("score: " + str(points), 1, (0, 0, 0))
	animate()



please help me if you can (sorry about the long code)
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: python game troubles

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: python game troubles

Posted 12 January 2010 - 02:45 PM

It appears to be an indention problem...
class SkierClass(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load("skier_down.png")
		self.rect = self.image.get_rect()
		self.rect.center = [320, 100]
		self.angle = 0

# On the same level as the class, so it is thought to be a normal 
# method... Indent everything by a tab or 4 spaces(Whichever you use).
def turn(self, direction):
	self.angle = self.angle + direction
	if self.angle < -2: self.angle = -2
	if self.angle >  2: self.angle = 2
	center = self.rect.center
	self.image = pygame.image.load(skier_images[self.angle])
	self.rect = self.image.get_rect()
	self.rect.center = center
	speed = [self.angle, 6 - abs(self.angle) * 2]
	return speed

# Same problem here.
def move(self, speed):
	self.rect.centerx = self.rect.centerx + speed[0]
	if self.rect.centerx < 20: self.rect.centerx = 20
	if self.rect.centerx > 620: self.rect.centerx = 620


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: python game troubles

Posted 12 January 2010 - 04:09 PM

If you didn't catch the error, you couldn't have written the code.

I'd suggest you actually read the book it came from: http://www.manning.com/sande/

Then steal the code directly, rather than cut and paste: http://www.manning.c...r/hw_ch10_code/
Was This Post Helpful? 0
  • +
  • -

#4 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: python game troubles

Posted 13 January 2010 - 05:20 PM

^ Good catch.

This post has been edited by SpeedisaVirus: 13 January 2010 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: python game troubles

Posted 13 January 2010 - 09:05 PM

Hey thanks for the help!
aslo baavgai just so you know i have actually purchased the book from a book store and i am following the tutorial but, i have revised the code and compared with the book at least twice (more on individual parts) but still couldn't find the error so i posted for help rather than just copying the code and cheating my way through the book and only learning half of what it has to offer!
just so you know! I did not write the code originally but i did manually wirte the posted version up. :P

This post has been edited by Eric115: 13 January 2010 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: python game troubles

Posted 13 January 2010 - 09:07 PM

So, did the indention problem fix it? or do you still need help?
Was This Post Helpful? 0
  • +
  • -

#7 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: python game troubles

Posted 13 January 2010 - 10:08 PM

Dogstopper i did what you said (I think i did) and i am still having the same problem.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: python game troubles

Posted 14 January 2010 - 05:51 AM

View PostEric115, on 13 Jan, 2010 - 10:05 PM, said:

just so you know! I did not write the code originally but i did manually wirte the posted version up. :P


Right. So, to the real question, "how do I get this cool code I found in a book to run?"

I've already posted the answer, but to be clear. Most books will have web sites with all the code in them available on the site. Rather than deal with any kind of validation, anyone can usually download the stuff even if they never purchased the book.

What you want to do is download the code and run that. If you can't run the official code then chances are your version of python is lacking a library. The results of trying to run it will should tell you what library.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: python game troubles

Posted 14 January 2010 - 07:35 PM

View PostEric115, on 13 Jan, 2010 - 11:08 PM, said:

Dogstopper i did what you said (I think i did) and i am still having the same problem.


Yep, DEFINITELY look at the link baavgai gave you!

This is what I was talking about. (I got it from the site)
class SkierClass(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load("skier_down.png")
		self.rect = self.image.get_rect()
		self.rect.center = [320, 100]
		self.angle = 0
		
	def turn(self, direction): 
		# load new image and change speed when the skier turns
		self.angle = self.angle + direction
		if self.angle < -2:  self.angle = -2
		if self.angle >  2:  self.angle =  2 
		center = self.rect.center
		self.image = pygame.image.load(skier_images[self.angle])
		self.rect = self.image.get_rect()
		self.rect.center = center
		speed = [self.angle, 6 - abs(self.angle) * 2]
		return speed
	
	def move(self, speed):
		# move the skier right and left
		self.rect.centerx = self.rect.centerx + speed[0]
		if self.rect.centerx < 20:  self.rect.centerx = 20
		if self.rect.centerx > 620: self.rect.centerx = 620 



See the indentation?
Was This Post Helpful? 0
  • +
  • -

#10 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: python game troubles

Posted 15 January 2010 - 02:10 AM

When i downloaded Idle from the books website it came with all the listings in the book and i did go over the code as much as possible but now that i haven't looked at it for a day or two i will revise the code now. Thanks for your help with this :D

EDIT:
I did try the code from the book (The one that came with IDLE) and it did work i did try that before posting.
Also when i try to run the script it says skierClass has no attribute 'move'.
skier.move(speed). I checked the script and i can't find an attribute move under skierClass but the script for the website doesn't have that attribute either...so i am not sure what i have done wrong and thats why i posted here originally i have compared the scripts but was unable to find where they have attributed this and i haven't.

This post has been edited by Eric115: 15 January 2010 - 02:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1