[Tutor] robots question

Roelof Wobben rwobben at hotmail.com
Thu Sep 16 17:16:11 CEST 2010


Hello, 

As a exercise from this book ( Thinking like a computer scientist ) I have to make this programm on this page(http://openbookproject.net/thinkcs/python/english2e/ch12.html)
Exercise 11 

#
# robots.py
#
from gasp import *

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
GRID_WIDTH = SCREEN_WIDTH/10 - 1
GRID_HEIGHT = SCREEN_HEIGHT/10 - 1


def place_player():
    # x = random.randint(0, GRID_WIDTH)
    # y = random.randint(0, GRID_HEIGHT)
    x, y = GRID_WIDTH/2 + 3, GRID_HEIGHT/2
    return {'shape': Circle((10*x+5, 10*y+5), 5, filled=True), 'x': x, 'y': y}

def place_robot(x,y, junk):
    x = random.randint(0, GRID_WIDTH)
    y = random.randint(0, GRID_HEIGHT)
    return {'shape': Box((10*x, 10*y), 10, 10), 'x': x, 'y': y}


def place_robots(numbots):
    robots = []
    # for i in range(numbots):
    #    x = random.randint(0, GRID_WIDTH)
    #    y = random.randint(0, GRID_HEIGHT)
    #    robots.append(place_robot(x, y))
    robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 + 2, junk= False))
    robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 - 2, junk = False))
    return robots

def move_player(player):
    update_when('key_pressed')
    if key_pressed('escape'):
        return True
    elif key_pressed('4'):
        if player['x'] > 0: player['x'] -= 1
    elif key_pressed('7'):
        if player['x'] > 0: player['x'] -= 1
        if player['y'] < GRID_HEIGHT: player['y'] += 1
    elif key_pressed('8'):
        if player['y'] < GRID_HEIGHT: player['y'] += 1
    elif key_pressed('9'):
        if player['x'] < GRID_WIDTH: player['x'] += 1
        if player['y'] < GRID_HEIGHT: player['y'] += 1
    elif key_pressed('6'):
        if player['x'] < GRID_WIDTH: player['x'] += 1
    elif key_pressed('3'):
        if player['x'] < GRID_WIDTH: player['x'] += 1
        if player['y'] > 0: player['y'] -= 1
    elif key_pressed('2'):
        if player['y'] > 0: player['y'] -= 1
    elif key_pressed('1'):
        if player['x'] > 0: player['x'] -= 1
        if player['y'] > 0: player['y'] -= 1
    elif key_pressed('0'):
       player['x'] = random.randint(0, GRID_WIDTH)
       player['y'] = random.randint(0, GRID_HEIGHT)
    else:
        return False

    move_to(player['shape'], (10*player['x']+5, 10*player['y']+5))

    return False

def collided(thing1, thing2):
    return thing1['x'] == thing2['x'] and thing1['y'] == thing2['y']

def check_collisions(robots, junk, player):
    # check whether player has collided with anything
    for thing in robots + junk:
        if collided(thing, player):
            return True
    return False



def move_robot(robot, player):
    if robot['x'] < player['x']: robot['x'] += 1
    elif robot['x'] > player['x']: robot['x'] -= 1

    if robot['y'] < player['y']: robot['y'] += 1
    elif robot['y'] > player['y']: robot['y'] -= 1

    move_to(robot['shape'], (10*robot['x'], 10*robot['y']))

def move_robots(robots, player):
    for robot in robots:
        move_robot(robot, player)


def play_game(robots):
    begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)
    player = place_player()
    robot = place_robots(4)
    junk = [place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk="true" )]
    defeated = False

    while not defeated:
        quit =  move_player(player)
        if quit:
            break
        move_robots(robot, player)
        defeated = check_collisions(robot, player, junk)

    if defeated:
        remove_from_screen(player['shape'])
        for thing in robots + junk:
            remove_from_screen(thing['shape'])
        Text("They got you!", (240, 240), size=32)
        sleep(3)

    end_graphics()



if __name__ == '__main__':
    play_game(2)

But now Im getting this error message :

Traceback (most recent call last):
  File "/root/workspace/test2/src/test.py", line 120, in <module>
    play_game(2)
  File "/root/workspace/test2/src/test.py", line 106, in play_game
    defeated = check_collisions(robot, player, junk)
  File "/root/workspace/test2/src/test.py", line 73, in check_collisions
    for thing in robots + junk:
TypeError: can only concatenate list (not "dict") to list

I understand that robots is a dict and junk is a list.

Is that right ?

And why does the book say that when this message is appearing. 

Roelof


 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100916/0e5feb38/attachment-0001.html>


More information about the Tutor mailing list