<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Hello, <br><br>I changed a lot because of your suggestions.<br><br>But one thing is still a puzzle.<br><br>The robots don't move anymore.<br><br>What I have is this :<br><br>#<br># robots.py<br>#<br>from gasp import *<br><br>SCREEN_WIDTH = 640<br>SCREEN_HEIGHT = 480<br>GRID_WIDTH = SCREEN_WIDTH/10 - 1<br>GRID_HEIGHT = SCREEN_HEIGHT/10 - 1<br><br><br>def place_player():<br>&nbsp;&nbsp;&nbsp; # x = random.randint(0, GRID_WIDTH)<br>&nbsp;&nbsp;&nbsp; # y = random.randint(0, GRID_HEIGHT)<br>&nbsp;&nbsp;&nbsp; x, y = GRID_WIDTH/2 + 3, GRID_HEIGHT/2<br>&nbsp;&nbsp;&nbsp; return {'shape': Circle((10*x+5, 10*y+5), 5, filled=True), 'x': x, 'y': y}<br><br>def place_robot(x, y, junk=False):<br>&nbsp;&nbsp;&nbsp; return {'shape': Box((10*x, 10*y), 10, 10, filled = junk), 'x': x, 'y': y}<br><br><br>def place_robots(numbots):<br>&nbsp;&nbsp;&nbsp; robots=[]<br>&nbsp;&nbsp;&nbsp; # for i in range(numbots):<br>&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp; x = random.randint(0, GRID_WIDTH)<br>&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp; y = random.randint(0, GRID_HEIGHT)<br>&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp; robots.append(place_robot(x, y))<br>&nbsp;&nbsp;&nbsp; robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 + 2, junk = False))<br>&nbsp;&nbsp;&nbsp; robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 - 2, junk = False))<br>&nbsp;&nbsp;&nbsp; return robots<br><br>def move_player(player):<br>&nbsp;&nbsp;&nbsp; update_when('key_pressed')<br>&nbsp;&nbsp;&nbsp; if key_pressed('escape'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return True<br>&nbsp;&nbsp;&nbsp; elif key_pressed('4'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['x'] &gt; 0: player['x'] -= 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('7'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['x'] &gt; 0: player['x'] -= 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['y'] &lt; GRID_HEIGHT: player['y'] += 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('8'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['y'] &lt; GRID_HEIGHT: player['y'] += 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('9'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['x'] &lt; GRID_WIDTH: player['x'] += 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['y'] &lt; GRID_HEIGHT: player['y'] += 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('6'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['x'] &lt; GRID_WIDTH: player['x'] += 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('3'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['x'] &lt; GRID_WIDTH: player['x'] += 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['y'] &gt; 0: player['y'] -= 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('2'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['y'] &gt; 0: player['y'] -= 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('1'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['x'] &gt; 0: player['x'] -= 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if player['y'] &gt; 0: player['y'] -= 1<br>&nbsp;&nbsp;&nbsp; elif key_pressed('0'):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player['x'] = random.randint(0, GRID_WIDTH)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player['y'] = random.randint(0, GRID_HEIGHT)<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return False<br><br>&nbsp;&nbsp;&nbsp; move_to(player['shape'], (10*player['x']+5, 10*player['y']+5))<br><br>&nbsp;&nbsp;&nbsp; return False<br><br>def collided(thing1, thing2):<br>&nbsp;&nbsp;&nbsp; return thing1['x'] == thing2['x'] and thing1['y'] == thing2['y']<br><br>def check_collisions(robots, junk, player):<br>&nbsp;&nbsp;&nbsp; # check whether player has collided with anything<br>&nbsp;&nbsp;&nbsp; for thing in robots + junk:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if collided(thing, player):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return True<br>&nbsp;&nbsp;&nbsp; return False<br><br><br><br>def move_robot(robot, player):<br>&nbsp;&nbsp;&nbsp; if robot['x'] &lt; player['x']: robot['x'] += 1<br>&nbsp;&nbsp;&nbsp; elif robot['x'] &gt; player['x']: robot['x'] -= 1<br><br>&nbsp;&nbsp;&nbsp; if robot['y'] &lt; player['y']: robot['y'] += 1<br>&nbsp;&nbsp;&nbsp; elif robot['y'] &gt; player['y']: robot['y'] -= 1<br><br>&nbsp;&nbsp;&nbsp; move_to(robot['shape'], (10*robot['x'], 10*robot['y']))<br><br>def move_robots(robots, player):<br>&nbsp;&nbsp;&nbsp; for robot in robots:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; move_robot(robot, player)<br><br><br>def play_game():<br>&nbsp;&nbsp;&nbsp; begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)<br>&nbsp;&nbsp;&nbsp; player = place_player()<br>&nbsp;&nbsp;&nbsp; robots = []<br>&nbsp;&nbsp;&nbsp; place_robots(4)<br>&nbsp;&nbsp;&nbsp; junk = [ place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk=True )]<br>&nbsp;&nbsp;&nbsp; defeated = False<br><br>&nbsp;&nbsp;&nbsp; while not defeated:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quit =&nbsp; move_player(player)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if quit:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; move_robots(robots, player)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; defeated = check_collisions(robots, junk, player)<br><br>&nbsp;&nbsp;&nbsp; if defeated:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remove_from_screen(player['shape'])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for thing in robots + junk:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remove_from_screen(thing['shape'])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text("They got you!", (240, 240), size=32)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep(3)<br><br>&nbsp;&nbsp;&nbsp; end_graphics()<br><br><br><br>if __name__ == '__main__':<br>&nbsp;&nbsp;&nbsp; play_game()<br><br>Roelof<br><br><br>&gt; To: tutor@python.org<br>&gt; From: alan.gauld@btinternet.com<br>&gt; Date: Fri, 17 Sep 2010 01:14:32 +0100<br>&gt; Subject: Re: [Tutor] robots question<br>&gt; <br>&gt; <br>&gt; "Roelof Wobben" &lt;rwobben@hotmail.com&gt; wrote<br>&gt; <br>&gt; <br>&gt; #<br>&gt; # robots.py<br>&gt; <br>&gt; This is pretty weird code, there are several odd things in it.<br>&gt; <br>&gt; def place_player():<br>&gt;     # x = random.randint(0, GRID_WIDTH)<br>&gt;     # y = random.randint(0, GRID_HEIGHT)<br>&gt;     x, y = GRID_WIDTH/2 + 3, GRID_HEIGHT/2<br>&gt;     return {'shape': Circle((10*x+5, 10*y+5), 5, filled=True), 'x': x, <br>&gt; 'y': y}<br>&gt; <br>&gt; So this returns a dictionary which always contains the same data.<br>&gt; <br>&gt; def place_robot(x,y, junk):<br>&gt;     x = random.randint(0, GRID_WIDTH)<br>&gt;     y = random.randint(0, GRID_HEIGHT)<br>&gt;     return {'shape': Box((10*x, 10*y), 10, 10), 'x': x, 'y': y}<br>&gt; <br>&gt; This returns a similar dict but with random data.<br>&gt; It ignores the values of x and y passed in and does not use junk at <br>&gt; all.<br>&gt; <br>&gt; def place_robots(numbots):<br>&gt;     robots = []<br>&gt;     # for i in range(numbots):<br>&gt;     #    x = random.randint(0, GRID_WIDTH)<br>&gt;     #    y = random.randint(0, GRID_HEIGHT)<br>&gt;     #    robots.append(place_robot(x, y))<br>&gt;     robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 + 2, <br>&gt; junk= False))<br>&gt;     robots.append(place_robot(GRID_WIDTH/2 - 4, GRID_HEIGHT/2 - 2, <br>&gt; junk = False))<br>&gt;     print type(robots)<br>&gt;     return robots<br>&gt; <br>&gt; This returns a list of 2 dictionaries. The x,y parameters are ignored <br>&gt; by the function.<br>&gt; <br>&gt; <br>&gt; def move_player(player):<br>&gt;     update_when('key_pressed')<br>&gt;     if key_pressed('escape'):<br>&gt;         return True<br>&gt;     elif key_pressed('4'): ...<br>&gt;     else:<br>&gt;         return False<br>&gt;     move_to(player['shape'], (10*player['x']+5, 10*player['y']+5))<br>&gt;     return False<br>&gt; <br>&gt; This seems OK, it returns True for escape otherwise False.<br>&gt; <br>&gt; def collided(thing1, thing2):<br>&gt;     return thing1['x'] == thing2['x'] and thing1['y'] == thing2['y']<br>&gt; <br>&gt; This returns a boolean<br>&gt; <br>&gt; <br>&gt; def check_collisions(robots, junk, player):<br>&gt;     # check whether player has collided with anything<br>&gt;     for thing in robots + junk:<br>&gt;         if collided(thing, player):<br>&gt;             return True<br>&gt;     return False<br>&gt; <br>&gt; Could be simplified to just<br>&gt; <br>&gt; for thing in robots + junk:<br>&gt;      return collided(thing, player)<br>&gt; <br>&gt; It requires that robots and junk are capable of being added together<br>&gt; and the result being iterable.<br>&gt; <br>&gt; def move_robot(robot, player):<br>&gt;     if robot['x'] &lt; player['x']: robot['x'] += 1<br>&gt;     elif robot['x'] &gt; player['x']: robot['x'] -= 1<br>&gt; <br>&gt;     if robot['y'] &lt; player['y']: robot['y'] += 1<br>&gt;     elif robot['y'] &gt; player['y']: robot['y'] -= 1<br>&gt; <br>&gt;     move_to(robot['shape'], (10*robot['x'], 10*robot['y']))<br>&gt; <br>&gt; I don't see move_to so assume its part of the module you imported?<br>&gt; <br>&gt; def move_robots(robots, player):<br>&gt;     for robot in robots:<br>&gt;         move_robot(robot, player)<br>&gt; <br>&gt; ok<br>&gt; <br>&gt; <br>&gt; def play_game():<br>&gt;     begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)<br>&gt;     player = place_player()<br>&gt;     robot = place_robots(4)<br>&gt;     junk = [ place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk="true" )]<br>&gt;     robots = []<br>&gt;     defeated = False<br>&gt; <br>&gt; So at this point<br>&gt; player is a dict<br>&gt; robot is a list of 2 dicts<br>&gt; junk is a list of one dict<br>&gt; robots is an empty list<br>&gt; <br>&gt; <br>&gt;     while not defeated:<br>&gt;         quit =  move_player(player)<br>&gt;         if quit:<br>&gt;             break<br>&gt;         move_robots(robots, player)<br>&gt;         print "type robots", type(robots)<br>&gt;         print "type junk", type(junk)<br>&gt;         print "type player", type(player)<br>&gt;         defeated = check_collisions(robots, player, junk)<br>&gt; <br>&gt; You now call check_collisions passing an empty list and a dict and a <br>&gt; list of a dict<br>&gt; The order in the definition is:<br>&gt; <br>&gt; def check_collisions(robots, junk, player):<br>&gt; <br>&gt; so it looks like you swapped the last two arguments<br>&gt; <br>&gt; <br>&gt; And now Im getting this message :<br>&gt; <br>&gt; ** Message: pygobject_register_sinkfunc is deprecated (GtkWindow)<br>&gt; ** Message: pygobject_register_sinkfunc is deprecated (GtkInvisible)<br>&gt; ** Message: pygobject_register_sinkfunc is deprecated (GtkObject)<br>&gt; &lt;type 'list'&gt;<br>&gt; <br>&gt; Not sure where that lot came from...<br>&gt; <br>&gt; type robotsTraceback (most recent call last):<br>&gt;  &lt;type 'list'&gt;<br>&gt; type junk &lt;type 'list'&gt;<br>&gt; type player &lt;type 'dict'&gt;<br>&gt;   File "/root/workspace/test2/src/test.py", line 125, in &lt;module&gt;<br>&gt;     play_game()<br>&gt;   File "/root/workspace/test2/src/test.py", line 111, in play_game<br>&gt;     defeated = check_collisions(robots, player, junk)<br>&gt;   File "/root/workspace/test2/src/test.py", line 74, in <br>&gt; check_collisions<br>&gt;     for thing in robots + junk:<br>&gt; TypeError: can only concatenate list (not "dict") to list<br>&gt; <br>&gt; But this is valid because of the swapped arguments.<br>&gt; <br>&gt; &gt; So far I can see the problem is that player is a dict and the rest <br>&gt; &gt; is a list.<br>&gt; &gt; Is this the correct conclusion ?<br>&gt; <br>&gt; Yes, but you missed the fact that you changed the order of the <br>&gt; arguments.<br>&gt; When you get type errors check the types at your interfaces(functions, <br>&gt; classes etc)<br>&gt; match the definitions.<br>&gt; <br>&gt; -- <br>&gt; Alan Gauld<br>&gt; Author of the Learn to Program web site<br>&gt; http://www.alan-g.me.uk/<br>&gt; <br>&gt; <br>&gt; _______________________________________________<br>&gt; Tutor maillist  -  Tutor@python.org<br>&gt; To unsubscribe or change subscription options:<br>&gt; http://mail.python.org/mailman/listinfo/tutor<br>                                               </body>
</html>