OK, I’m giving up on python. Even when I code it correct it gives me an error for no reason. So can someone point me to how to reinforcement learn in java?
Well if it gives you an error then you’re not coding it correct…
Show us your code that you’re having trouble with, we can help
ok here the code
first main:
from Car import *
car = Car()
def setup():
size(600,600)
global img
img = loadImage("car.png")
def draw():
background(0)
car.show()
car:
class Car:
pos = (0,0,0)
def show(self):
global img
image(self.img,0,0)
i also tried
main:
from Car import *
global img
img = loadImage("car.png")
car = Car()
def setup():
size(600,600)
def draw():
background(0)
car.show()
car:
class Car:
pos = (0,0,0)
def show(self):
global img
image(self.img,0,0)
also tried
main:
from Car import *
car = Car()
def setup():
size(600,600)
def draw():
background(0)
car.show()
car:
class Car:
pos = (0,0,0)
global img
img = loadImage("car.png")
def show(self):
global img
image(self.img,0,0)
all 3 can’t find img for the image
Is the image in your data folder?
yes, it worked fine when I tried the image like:
main:
def setup():
size(600,600)
global img
img = loadImage("car.png")
def draw():
background(0)
image(img,0,0)
but not when I try moving it into a class
What’s the error message you’re getting?
for try 1: Car instance has no attribute ‘img’
for try 2: Car instance has no attribute ‘img’
for try 3: Car instance has no attribute ‘img’
so all 3 can’t find img to draw the car. idk how to draw a rect without a library so I’m stuck with trying to get this picture working
Do you want each Car to have its own image? Or do you want any car to use the same image?
well in the final be great to have different cars but for now be fine with all one
In that case your Car class needs an init(img). In it, create a local (not global) self.img, and load it with the image. Pass the image file path string to it when you initialize the Car – car = Car("myimgpath")
Now you can access image(car.img).
For an example of how to use init and object variables correctly, see the Processing.py object tutorial.
do you mean like this?
main:
from Car import *
car = Car("car.png")
def setup():
size(600,600)
def draw():
background(0)
car.show()
car:
class Car:
def __init__(self,img):
pos = (0,0,0)
this.img = loadImage(img)
def show(self):
image(car.img,0,0)
Looks like almost, last line of Car class should be
image(self.img,0,0)
And in init don’t use this
that is a Java keyword. Use self
but I didn’t test.
Also, you may want to initialize a global car in setup, not in the header.
it says Car instance has no attribute ‘img’
main:
from Car import *
car = Car("car.png")
def setup():
size(600,600)
def draw():
background(0)
car.show()
car:
class Car:
def __init__(self,img):
pos = (0,0,0)
img = loadImage(img)
def show(self):
image(self.img,0,0)
I said don’t use this
, use self
. You just deleted this
I added self to init:
def __init__(self,img):
pos = (0,0,0)
self.img = loadImage(img)
but still getting an error nullpointerExceptionat processing.core.PGraphics…
you may want to initialize a global car in setup, not in the header.
When I move the car to setup the one in draw won’t work.
car not defined it says
main:
from Car import *
def setup():
size(600,600)
global car = Car("car.png")
def draw():
background(0)
car.show()
car
class Car:
def __init__(self,img):
pos = (0,0,0)
self.img = loadImage(img)
def show(self):
image(self.img,0,0)
You can’t do global name = something
in Python.
def setup():
global car
size(600, 600)
car = Car("car.png")