Python Forum
Player object wont recognize collision with other objects.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Player object wont recognize collision with other objects.
#1
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




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()
Reply
#2
The line that reads
wn.mainloop()
Is calling a function that you don't have. Maybe it's a built-in turtle function, don't know why else you wouldn't get an error, but that's as far as the code gets read.

Take out that line so python will read down to the loop you do have with your treasure collection code. It'll work fine.
Reply
#3
Thank you! That fixed it, I don't know how i missed that one. Originally I had to put that mainloop call in the keybindings because I had a really weird thing where if I didn't have it there I wouldn't be able to use any keybinding functions at all.
Reply
#4
@Mike your right and wrong wn.mainloop() is a function of tkinter and the turtle module is based on tkinter. the mainloop is blocking the while true fyi,
Joe
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why wont this path work one way, but will the other way? cubangt 2 568 Sep-01-2023, 04:14 PM
Last Post: cubangt
  python cant recognize PIP siubikYT 2 642 Jul-19-2023, 06:30 PM
Last Post: Lahearle
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,102 Feb-25-2022, 06:31 PM
Last Post: jao
  My program won't recognize the filename. braingoblins 1 1,053 Jan-07-2022, 06:18 PM
Last Post: deanhystad
Star I was making code for collision for my character izmamonke 2 2,026 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  Is there a library for recursive object creation using config objects johsmi96 0 1,789 May-03-2021, 08:09 PM
Last Post: johsmi96
Star NameError – function doesn't recognize imported modules Sir 4 3,337 Dec-01-2020, 06:36 AM
Last Post: Sir
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 10,969 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque
  bouncing ball with variable collision points (in time) Zhaleh 1 2,289 Jul-24-2020, 02:54 PM
Last Post: Marbelous
  When I import a Module it wont run PyNovice 17 6,080 Oct-26-2019, 11:14 AM
Last Post: PyNovice

Forum Jump:

User Panel Messages

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