首页 新闻 搜索 专区 学院

python初学遇到的问题

0
悬赏园豆:20 [已解决问题] 解决于 2014-10-10 09:06

最近在学习 与孩子一起学编程(Computer Programming for Kids and Other Beginners,4Ed)这本书作为Python的入门,在第十章的时候有个skier的游戏代码。对照课本敲出来代码,运行时总是报如下错误信息

Traceback (most recent call last):
  File "D:\cmdworkspace\pyws\skier.py", line 90, in <module>
    map0 = create_map(20, 29)
  File "D:\cmdworkspace\pyws\skier.py", line 64, in create_map
    obstacle = ObstacleClass(img, location, type1)
  File "C:\Python25\Lib\site-packages\pygame\sprite.py", line 92, in __init__
    if groups: self.add(groups)

从网站上导下来的源码又能正常运行

我的代码如下:

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 = pyganme.image.load("skier_down.png")
        self.rect = selt.image.get_rect()
        selft.rect.center = [320,100]
        self.angle = 0

    def turn(self,direction):

        self.angle = self.angle + direction
        if self.angle < -2: self.direction = -2
        if self.angle > 2:  self.direction = 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,type1):
        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.type1 = type1
        self.passwd = False

    def scroll(self, terrainPos):
        self.rect.centery = self.loaction[1] - terrainPos



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)
            type1 = random.choice(["tree","flag"])
            if type1 == "flag": img = "skier_flag.png"
            elif type1 == "tree": img = "skier_tree.png"
            obstacle = ObstacleClass(img, location, type1)
            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.trun(1)
    skier.move(speed)
    map_position += speed[1]




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


    for obstacle in obstacles:
        obstacle.scroll(map_postion)


    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()
                 
    

 

希望大侠们指正一下,帮一下小弟我

genngenn的主页 genngenn | 初学一级 | 园豆:184
提问于:2014-10-09 11:40
< >
分享
最佳答案
0

代码太长没看,,不过我没记错的话python的 init函数前后应该有两个下划线吧,,,你的代码里只有一个。。;

另外你说网站上导出来到的可以运行,自己写的不能。那你对比一下么。看看差异在哪里

收获园豆:20
龙卷风摧毁停车场! | 初学一级 |园豆:10 | 2014-10-09 18:21

嗯,多谢!我已经解决了。确实是下划线的问题,还有其它的问题我也解决了。

genngenn | 园豆:184 (初学一级) | 2014-10-10 09:06

@genngenn: 楼主有没有SkierClass没有定义的问题的解决方法啊?

Traceback (most recent call last):
File "F:\lianxi\wmm code\skierwmm\list10.py", line 77, in <module>
skier = SkierClass()
File "F:\lianxi\wmm code\skierwmm\list10.py", line 11, in __init__
self.rect = self.image.get_rect()
AttributeError: 'SkierClass' object has no attribute 'image'

爱学习的小瓜牛 | 园豆:200 (初学一级) | 2016-02-22 15:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册