Dominik C Dominik C - 3 months ago 17
Python Question

Maze/labirynth game - few questions - Python 2.7.3 Pyglet 1.1.4

I am creating a maze/labirynth game using Python 2.7.3 and Pyglet 1.1.4 and
I have a problem and a question.

My code can be found in here: https://github.com/czarnot/labirynth_py

To start the program, launch main.py with python.

To move CPU use "a" button. Here is my main problem. I want to make CPU moving without user inputs - I want it to move like 1 step per 0.5 or 1 second.

I tried using time.sleep() between cpu steps and it simply slept for all steps, and then teleported into the end. I think it is something with Pyglet - probably with draw function, because sleep is working well (when prints are there, it prints after sleeping).

Here is the part of code that it's working now with "A" button (main.py):

elif symbol == key.A:
if win == 0:
#for item in cpu:
item = cpu[x]
window.clear()
player.draw()
if item == 'North':
player.y += fieldlength
elif item == 'South':
player.y -= fieldlength
elif item == 'East':
player.x += fieldlength
elif item == 'West':
player.x -= fieldlength
x += 1


And the code that I tried (I also tried few others combinations...):

elif symbol == key.A:
if win == 0:
for item in cpu:
window.clear()
player.draw()
if item == 'North':
player.y += fieldlength
elif item == 'South':
player.y -= fieldlength
elif item == 'East':
player.x += fieldlength
elif item == 'West':
player.x -= fieldlength
time.sleep(1)


Another question:

My map generation algorithm is very simple - it picks random cell, then picks random neighbour of that cell, remove this cells from theirs' neighbour's lists, and puts walls into WallList.

I want to make better map generation algorithm - like this from wikipedia: http://en.wikipedia.org/wiki/Maze_generation_algorithm#Depth-first_search

I am not sure if my wall drawing method is efficient - wouldn't it be better to store 12 images for every wall combination and store a "WallNumber" value in MyMap values?
Here is an example of WallNumber value: http://imgur.com/DDfL1

I also ask for this, because I feel like the method I have now, will be very confusing and tricky to write better map generation algorithm for this.

@@EDIT
@zenpoy: I tried using this function earlier, but I must have done sth wrong, now I tried it and it works :).

Answer

I have no experience with Pyglet but in general most graphic applications are looping through two functions update() and draw(). The draw() function is called every frame and draws the representation of the model to the screen. the update() function on the other hand is called to update the model. So you should do your sleeping inside the update() function and not inside the draw() function.

Instead of waiting and freezing the whole loop you should use the clock module of pyglet to schedule the update every time delta: http://www.pyglet.org/doc/api/pyglet.clock-module.html.

clock.schedule_interval(move_player, .5)
Comments