Python Forum
Coding Problems With Mr.Stickman Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Problems With Mr.Stickman Game
#1
After I typed a lot of the coding for the game I ran it and fixed all the problems with it but I couldnt figure out what was wrong with it this time. Can someone please correct the errors.
from tkinter import *
import random
import time
import os

path = os.path.dirname(__file__)
os.chdir(path)

class Game:
    def __init__(self):
        self.tk = Tk()
        self.tk.title("Mr.Stickman Races for the Exit")
        self.tk.resizable(0, 0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=500, height=500, highlightthicknes=0)
        self.canvas.pack()
        self.tk.update()
        self.canvas_height = 500
        self.canvas_width = 500
        self.bg = PhotoImage(name="Background.gif")
        w = self.bg.width()
        h = self.bg.height()
        for x in range(0, 5):
            for y in range(0, 5):
                self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw')
        self.sprites = []
        self.running = True

    def mainloop(self):
        while 1:
            if self.running == True:
                for sprite in self.sprites:
                    sprite.move()
            self.tk.update_idletasks()
            self.tk.update()
            time.sleepp(0.01)

class coords:
    def __init__(self, x1=0, y1=0, x2=0, y2=0):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

def within_x(co1, co2):
    if co1.x1 > co2.x1 and co1.x1 < co2.x2:
        return True
    elif co1.x2 > co2.x1 and co1.x2 < co2.x2:
        return True
    elif co2.x1 > co1.x1 and co2.x1 < co1.x2:
        return True
    elif co2.x2 > co1.x1 and co2.x2 < co1.x2:
        return True
    else:
        return False

c1 = coords(40, 40, 100, 100)
c2 = coords(50, 50, 150, 150)
print(within_x(c1, c2))

def within_y(co1, co2):
    if (co1.y1 > co2.x1 <c02.x2)\
           or (co1.y2 > co2.y1 and co1.y2 < co2.y2)\
           or (co2.y1 > co1.y1 and co2.y1 < co2.y2)\
           or (co2.y2 > co1.y1 and co2.y2 < co1.y2):
        return True
    else:
        return False

def collided_left(co1, co2):
    if within_y(co1, co2):
        if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:
            return True
        return False

def collided_right(co1, co2):
    if within_y(co1, co2):
        if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:
            return True
        return False

def collided_top(co1, co2):
    if within_x(co1, co2):
        if co1.y1 <= co1.y2 and co1.y1 >= co2.y1:
            return True
        return False

def collided_bottom(y, co1, co2):
    if within_x(co1, co2):
        y_calc = co1.y2 + y
        if y_calc >= co2.y1 and y_calc <= co2.y2:
            return True
        return False

class Sprite:
    def __init__(self, game):
        self.game = game
        self.endgame = False
        self.coordinates = None
        def move(self):
            pass
        def coords(self):
            return self.coordinates

class PlatformSprite(Sprite):
    def __init__(self, game, photo_image, x, y, width, height):
        Sprite.__init__(self, game)
        self.photo_image = photo_image
        self.image = game.canvas.create_image(x, y, image=self.photo_image, anchor='nw')
        self.coordinates = Coords(x, y, x + width, y + height)
           
g = Game()
platform1 = PlatformSprite(g, PhotoImage(name="platform1.gif"), 0, 480, 100, 10)
g.sprites.append(platform1)
g.mainloop
Error:
Traceback (most recent call last): File "C:\Users\sheep\Desktop\Ayden's Apps\Mr.Stick Man.py", line 113, in <module> platform1 = PlatformSprite(g, PhotoImage(name="platform1.gif"), 0, 480, 100, 10) File "C:\Users\sheep\Desktop\Ayden's Apps\Mr.Stick Man.py", line 110, in __init__ self.coordinates = Coords(x, y, x + width, y + height) NameError: name 'Coords' is not defined
Output:
True
Reply
#2
At line 110 you are calling Coords() but it should be coords().

I suggest you to rename you class from:
class coords()
to...

class Coords()
And change this part too:
c1 = coords(40, 40, 100, 100)
c2 = coords(50, 50, 150, 150)
Reply
#3
You defined the class as coords not as Coords

BTW, the expression
co1.x1 > co2.x1 and co1.x1 < co2.x2
may be re-written as
co2.x1 < co1.x1 < co2.x2
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tic-Tac game (Beginner's coding) Shahmadhur13 5 3,100 Aug-29-2020, 08:40 PM
Last Post: deanhystad
  coding a 2-player die game Pepper887 3 2,438 Mar-17-2019, 04:02 PM
Last Post: ichabod801
  Guessing Game with .WAV Files - Python Coding NonEntity 8 4,348 Nov-20-2018, 12:53 AM
Last Post: NonEntity
  Easy equipment system for simple game and problems with it naisyrk 3 3,296 Sep-01-2018, 10:05 AM
Last Post: Gribouillis
  Problem with absolute path to file (Mr.Stickman Problems) SheeppOSU 3 6,220 Jun-26-2018, 10:05 AM
Last Post: Larz60+
  Bounce Game Problems SheeppOSU 3 3,553 May-07-2018, 07:58 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

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