[Tutor] Newbie: game problem

Abel Daniel abli@freemail.hu
Mon, 8 Jul 2002 00:38:52 +0200


On Sun, Jul 07, 2002 at 10:57:05PM +0200 Kemal Taskin (kemal.taskin@pandora.be) wrote:
> def move_player():
> 	global player_x 
> 	global player_y
> 	keys = keys_pressed()
> 	if 'q' in keys:
> 		pass #later this will exit the game
> 	elif '1' in keys:
> 		move_to(player_shape, player_x - 10, player_y - 10)
> 		player_x = player_x
> 		player_y = player_y
> 		return
> 	elif '8' in keys:
> 		move_to(player_shape, player_x + 0, player_y + 10)
> 		player_x = player_x
> 		player_y = player_y
> 		return
> 	#and so on ....
> 
> place_player()
> finished = 0
> while not finished:
> 	move_player()
> 
I think the problem is that you dont modify the global variables
player_x and player_y in move_player().
You would need something like this:

elif '1' in keys:
	player_x = player_x - 10
	player_y = player_y - 10
	move_to(player_shape, player_x, player_y)
	return

Also, i dont think the current layout of the code is good.
You basically use a structure like this:
while 1:
	key = keys_pressed() # i guess here you only get _one_ key
	if 'q' in key:
		# do something
	elif 'n' in key:
		# do something else
	..and so on...
The difference is that you break this structure into two functions,
which means that your move_player() function will be called all the
time. This will mean a lot of overhead because calling a function is a
constly think (i.e. it takes time). Of course premature optimalization
is the root of all evil, but in this case it looks like this is the only
place, where you will be using this function, so there is no reason to
do it this way.

Rename that function to some think like move_player_until_quit()
(so that you can see at a glance what it wants to do. currently it does
more than the name implies, it is more like
get_one_keypress_and_move_player() and not move_player() )
and have the while loop entirely in it.

abli
abli@freemail.hu
p.s. i didnt look at the livewire package, i assumed the functions do
what you used them for.