[Tutor] FW: robots question
Roelof Wobben
rwobben at hotmail.com
Thu Sep 16 19:44:16 CEST 2010
From: rwobben at hotmail.com
To: __peter__ at web.de
Subject: RE: [Tutor] robots question
Date: Thu, 16 Sep 2010 17:43:41 +0000
Hello ,
I change everything to this :
#
# 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))
print type(robots)
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():
begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)
player = place_player()
robot = place_robots(4)
junk = [ place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk="true" )]
robots = []
defeated = False
while not defeated:
quit = move_player(player)
if quit:
break
move_robots(robots, player)
print "type robots", type(robots)
print "type junk", type(junk)
print "type player", type(player)
defeated = check_collisions(robots, 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()
And now Im getting this message :
** Message: pygobject_register_sinkfunc is deprecated (GtkWindow)
** Message: pygobject_register_sinkfunc is deprecated (GtkInvisible)
** Message: pygobject_register_sinkfunc is deprecated (GtkObject)
<type 'list'>
type robotsTraceback (most recent call last):
<type 'list'>
type junk <type 'list'>
type player <type 'dict'>
File "/root/workspace/test2/src/test.py", line 125, in <module>
play_game()
File "/root/workspace/test2/src/test.py", line 111, in play_game
defeated = check_collisions(robots, player, junk)
File "/root/workspace/test2/src/test.py", line 74, in check_collisions
for thing in robots + junk:
TypeError: can only concatenate list (not "dict") to list
So far I can see the problem is that player is a dict and the rest is a list.
Is this the correct conclusion ?
Roelof
> To: tutor at python.org
> From: __peter__ at web.de
> Date: Thu, 16 Sep 2010 18:10:13 +0200
> Subject: Re: [Tutor] robots question
>
> Roelof Wobben wrote:
>
> > 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
>
> > def check_collisions(robots, junk, player):
>
> > defeated = check_collisions(robot, player, junk)
>
> Just look at the argument names: check_collisions() is probably expecting a
> /list/ of robots where you are passing it a single one.
>
> > 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 ?
>
> Yes. You are getting a dict because a single robot's state is stored in a
> dictionary.
>
> > And why does the book say that when this message is appearing.
>
> The way the program is presented makes it hard to tell at what point your
> and Eckel's idea of the script start to differ without going through the
> entire chapter.
>
> Aside: that's why real projects use version control systems. When a program
> stops working after a change there is always the option to go back to the
> state of the program before the bug was introduced.
>
> Peter
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100916/23624c1c/attachment.html>
More information about the Tutor
mailing list