Dec-21-2019, 09:21 PM
I've been learning myself the turtle module lately, and as part of that I have been following a tutorial for how to make a little maze game. However, after getting through most of the tutorial I've found myself completely stuck. When I run my code my player object wont recognize a collision that is supposed to happen with a treasure object. I have coded in a function that is supposed to catch it when it nears the treasure objects position. However, this does not happen. Since I have replicated most of the code from the tutorial and get no error messages I have no clue what is wrong here.
Here is the pastebin link if you want to run it yourself. https://pastebin.com/LdcJtC17
Here is the pastebin link if you want to run it yourself. https://pastebin.com/LdcJtC17
from __future__ import division
import turtle
import math
from math import *
from turtle import *
wn = turtle.Screen()
wn.bgcolor('black')
wn.title('The maze game!')
wn.setup(700,700)
#Create pen
class Pen(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape('square')
self.color('white')
self.penup()
self.speed(0)
class Player(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape('square')
self.color('blue')
self.penup()
self.speed(0)
self.gold = 0
def go_up(self):
#Calculate the spot to move to
move_to_x = player.xcor()
move_to_y = player.ycor() + 24
#Check if the space has a wall
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_down(self):
#Calculate the spot to move to
move_to_x = player.xcor()
move_to_y = player.ycor() - 24
#Check if space has a wall
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_left(self):
#Calculate the spot to move to
move_to_x = player.xcor() - 24
move_to_y = player.ycor()
#Check if space has a wall
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_right(self):
#Calculate the spot to move to
move_to_x = player.xcor() + 24
move_to_y = player.ycor()
#Check if space has a wall
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
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
class Treasure(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape('circle')
self.color('gold')
self.penup()
self.speed(0)
self.gold = 100
self.goto(x, y)
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
#Create levels list
levels =['']
#Define first level '''' COUNTING IS....
# ONLY THERE MOMENTARILY'''
level_1 = [
'XXXXXXXXXXXXXXXXXXXXXXXXX',
'XP XXXXXXX XXXXX',
'X XXXXXXX XXXXXX XXXXX',
'X XX XXXXXX XXXXX',
'X XX XXX XX',
'XXXXXX XX XXX XX',
'XXXXXX XX XXXXXX XXXXX',
'XXXXXX XX XXXX XXXXX',
'X XXX XXXT XXXXX',
'X XXX XXXXXXXXXXXXXXXXX',
'X XXXXXXXXXXXXXXX',
'X XXXXXXXX',
'XXXXXXXXXXXXXX XXXX X',
'XXX XXXXXXX XXXX X',
'XXX X',
'XXX X',
'XXXXXXXXXX XXXXXXXXXXXXX',
'XXXXXXXXXX XXXXXXXXXXXXX',
'XXXXXXXXXX X',
'XX XXXXX X',
'XX XXXXXXXXXXXXX XXXXX',
'XX YXXXXXXXXXXX XXXXX',
'XX XXXX X',
'XXXX X',
'XXXXXXXXXXXXXXXXXXXXXXXXX',
]
#Add a list of treasures
treasures = []
#Add maze to mazes list
levels.append(level_1)
#Create level setup function
def setup_maze(level):
for y in range(len(level)):
for x in range(len(level[y])):
#get the character at each x,y coordinate
#Note the order of y and x in the next line
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 (representing a wall)
if character == 'X':
pen.goto(screen_x, screen_y)
pen.stamp()
walls.append((screen_x,screen_y))
#Check if it is a player
if character == 'P':
player.goto(screen_x, screen_y)
#Check if it is a T (representing a treasure)
if character == 'T':
treasures.append(Treasure(screen_x, screen_y))
#Create class instances
pen = Pen()
player = Player()
#Create wall coordinate list
walls = []
#Set up the level
setup_maze(levels[1])
#Keyboard Bindings
wn.listen()
wn.onkey(player.go_up,'w')
wn.onkey(player.go_down,'s')
wn.onkey(player.go_left,'a')
wn.onkey(player.go_right,'d')
wn.mainloop()
#Turn off screen updates
wn.tracer(0)
#Main game loop
while True:
#Check for player collision with treasure
#Iterate through treasure list
for treasure in treasures:
if player.is_collision(treasure):
#Add the treasure gold to the player gold
player.gold += treasure.gold
print ('Player Gold: {}'.format(player.gold))
#Destroy the treasure
treasure.destroy()
#Remove the treasure
treasures.remove(treasure)
#Update screen
wn.update()
