Python Forum
Simple maze game controls
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple maze game controls
#1
My assignment is to create a simple maze game. So far everything is working, but i am unhappy with the movement of my sprite.
turtle.listen()
turtle.onkeypress(player.go_left,"a")
turtle.onkeypress(player.go_right,"d")
turtle.onkeypress(player.go_up,"w")
turtle.onkeypress(player.go_down,"s")
At the moment this code can make my sprite move using "w", "a", "s", "d" keys to move and you are able to hold down the key to continuously move. What i need help with is the sprite will move one "block" wait a second then start moving. I was wondering if there anyway to make it move as soon as the keys presses?


Sorry if there's any grammar or punctuation errors
Reply
#2
Could you show all the code
Reply
#3
this is it.
import turtle
import math
#background and UI
wn = turtle.Screen()
wn.bgcolor('#ffd26c')
wn.title('Desert Temple Maze')
wn.setup(700, 700)
wn.tracer(0)

#maze walLs
class Pen(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape('square')
        self.color('#ff7227')
        self.penup()
        self.speed(0)
#player model
class Player(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("circle")
        self.color('#fe5d5d')
        self.penup()
        self.speed(0)
        self.gems = 0
        
    #movement of player   
    def go_up(self):
        move_to_x=player.xcor()
        move_to_y=player.ycor() + 24
        
        #so player detects the walls
        if (move_to_x,move_to_y) not in walls:
            self.goto(move_to_x, move_to_y)
  
    def go_down(self):
        move_to_x=player.xcor()
        move_to_y=player.ycor() - 24
    
        if (move_to_x,move_to_y) not in walls:
            self.goto(move_to_x, move_to_y)  
        
    def go_right(self):
        move_to_x=player.xcor() + 24
        move_to_y=player.ycor()
    
        if (move_to_x,move_to_y) not in walls:
            self.goto(move_to_x, move_to_y) 
        
    def go_left(self):
        move_to_x=player.xcor() - 24
        move_to_y=player.ycor()
    
        if (move_to_x,move_to_y) not in walls:
            self.goto(move_to_x, move_to_y)

    #detects items near player
    def is_collision(self, other):
        a = self.xcor()-other.xcor()
        b = self.ycor()-other.ycor()
        distance = math.sqrt((a ** 2) + (b ** 2))

        if distance < 5:
            return True
        else:
            return False 
#create treasure
class Treasure(turtle.Turtle):
    def __init__(self, x, y):
        turtle.Turtle.__init__(self)
        self.shape("circle")
        self.color('light green')
        self.penup()
        self.speed(0)
        self.gems = 100
        self.goto(x,y)

    def destroy(self):
        self.goto(2000, 2000)
        self.hideturtle()

        
levels = [""]

level_1 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XPX         X           X",
"X X XXXXX XXXXXX XXXXXX X",
"X X    TX        X      X",
"X XXXXXXX XXXXXXXXXXXXX X",
"X         X  TX X     X X",
"XXXXX XXXXX XXX X XXXXX X",
"X   X X     X X         X",
"X X X   XXXXX X XXXXXXXXX",
"X X XXXXX               X",
"X X       XXXXXXXXX XXX X",
"X XXXXXX XX       X XTX X",
"X      X  X XXXXX X X X X",
"XXXXXX XX X     X X X X X",
"X       XTX XX XXXX X X X",
"X XXXXXXXXX X     X X   X",
"X X     X   XXXXX X XXXXX",
"X XXX X X X X  TX X    TX",
"X     X X X X XXX XXXXXXX",   
"XXXXX X   X X   X X     X",
"X   X XXXXXXXXX X   XXX X",
"X XTX X       X XXXXX   X",
"X XXX XXXXX X X   XTX   X",
"X           X   X   X   X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]

#creates treasure list
treasures = []

#Add maze to mazes list
levels.append(level_1)

#Level Setup

def setup_maze(level):
  for y in range(len(level)):
    for x in range(len(level[y])):
      character = level[y][x]
      #Calculate the screen x, y coordinates
      screen_x = -288 + (x * 24)
      screen_y = 288 - (y * 24)
      
      #Check if it is an X
      if character == "X":
        pen.goto(screen_x, screen_y)
        pen.stamp()
        walls.append((screen_x,screen_y))

      #representing player on maze  
      if character == "P":
        player.goto(screen_x, screen_y)

      #representing treasure on maze
      if character == 'T':
          treasures.append(Treasure(screen_x, screen_y))
        

#variables
pen = Pen()
player = Player()

#creates list for wall coordinates
walls = []

#sets up level
setup_maze(levels[1])
print(walls)
  
#so the code responds to key presses
turtle.listen()
turtle.onkeypress(player.go_left,"a")
turtle.onkeypress(player.go_right,"d")
turtle.onkeypress(player.go_up,"w")
turtle.onkeypress(player.go_down,"s")

#turn off screen updates
wn.tracer(0)

#game loop
while True:
    for treasure in treasures:
        if player.is_collision(treasure):
            player.gems += treasure.gems
            print('Player gems: {}'.format(player.gems))
            treasure.destroy()
            treasures.remove(treasure)
    #update screen
    wn.update()
Reply
#4
I believe that is just the built in "onkeypress". The key is pressed and so it moves, to prevent multiple moves with one press it waits and then checks if it is still being pressed. If so it will move the player multiple moves without waiting. If you want you may be able to go to the turtle .py file and find the function. Then you could edit it to wait .5 seconds rather than 1 second

If your on windows it will be somewhere in here -
C:\Users\Name\Appdata\local\programs\python\python(some numbers)\Lib\Then find the turtle folder. Next locate the function or file. It could be a function in main or it might have its own file. Good luck
Reply
#5
thank you very much i appreciate the help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Heuristic for GBFS Maze RockBlok 0 304 Jan-30-2024, 12:12 AM
Last Post: RockBlok
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,128 Dec-14-2021, 12:23 AM
Last Post: supuflounder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020