How to fix "name 'render' is not defined" in Ursina Engine? - python

So I've just started with "Ursina" Engine, I'm still very new to it. I'm trying to make a Minecraft game on Youtube tutorial with this Engine. And for some reason, the program keeps giving me the error name "Name 'render' is not defined". And I don't understand what that's saying. I tried to fix my code and skimmed over the code but couldn't find the answer.
This is all my code:
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
Sky(texture = 'sky.jpg')
class Voxel(Button):
def __init__(self, position = (0,0,0)):
super().__init__(
model = 'cube',
texture = 'white_cube',
position = position,
color = color.white,
parent = scene,
origin_y = 0.5,
highlight_color = color.lime,
)
app = Ursina()
player = FirstPersonController()
for x in range(12):
for y in range(12):
voxel = Voxel(position = (y,0,x))
app.run()
This is the Traceback of the code:
PS C:\Users\lhnguyen1029> & "C:/Program Files (x86)/Python39-
32/python.exe" "c:/Users/lhnguyen1029/OneDrive - Mesa Public
Schools/Documents/CTE- Computer Science Principles/Ursina
practice/ursina_practice(2).py"
package_folder:
C:\Users\lhnguyen1029\AppData\Roaming\Python\Python39\site-
packages\ursina
asset_folder: c:\Users\lhnguyen1029\OneDrive - Mesa Public
Schools\Documents\CTE- Computer Science Principles\Ursina practice
screen resolution: (1366, 768)
Traceback (most recent call last):
File "c:\Users\lhnguyen1029\OneDrive - Mesa Public
Schools\Documents\CTE- Computer Science Principles\Ursina
practice\ursina_practice(2).py", line 7, in <module>
Sky(texture = 'sky.jpg')
File "C:\Users\lhnguyen1029\AppData\Roaming\Python\Python39\site-
packages\ursina\prefabs\sky.py", line 8, in __init__
parent = render,
NameError: name 'render' is not defined
PS C:\Users\lhnguyen1029>

Instantiate Ursina() before instantiating entities.

Related

Python - Creating an instance of a module, getting an error

I am creating a universal text field that can be used in many python turtle projects. I am trying to create an instance of it but I get this error:
>>> import TextField
>>> tf = TextField('None', False)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
tf = TextField('None', False)
TypeError: 'module' object is not callable
>>>
What in a module causes this type of error? I completely wrote this module and I'm getting an error creating an instance of it :( ... What do I need in this module to make it 'callable'? I have tried adding a def __call__(self): but that doesn't affect the problem at all, nor create any errors.
Here is the beginning of the script where the problem is most likely happening:
# Created by SUPERMECHM500 # repl.it
# Edited by cdlane # stackoverflow.com
class TextField:
TextFieldBorderColor = '#0019fc'
TextFieldBGColor = '#000000'
TextFieldTextColor = '#ffffff'
ShiftedDigits = {
'1':'!',
'2':'#',
'3':'#',
'4':'$',
'5':'%',
'6':'^',
'7':'&',
'8':'*',
'9':'(',
'0':')'
}
def __init__(self, command, CanBeEmpty): # Ex. textField = TextField('Execute()', True)
self.CmdOnEnter = command
self.turtle = Turtle()
self.CanBeEmpty = CanBeEmpty
self.turtle.speed('fastest')
self.inp = []
self.FullOutput = ""
self.TextSeparation = 7
self.s = self.TextSeparation
self.key_shiftL = False
......
The module is not the class. If your class TextField is in a module called TextField, then it is referred to as TextField.TextField.
Or change your import to
from TextField import TextField

Python turtle UnboundLocalError

I keep getting a UnboundLocalError when I try assigning turtle to turtle.Turtle() in a function. Why is it? Is the name used in turtle.py?
import turtle
def make_turtle():
turtle = turtle.Turtle()
return turtle
eu = make_turtle()
Traceback (most recent call last):
File "/home/usr/PycharmProjects/LearningToThinkLikeAComputerScientist/c4 - Functions/test.py", line 6, in <module>
eu = make_turtle()
File "/home/luis/PycharmProjects/LearningToThinkLikeAComputerScientist/c4 - Functions/test.py", line 3, in make_turtle
turtle = turtle.Turtle()
UnboundLocalError: local variable 'turtle' referenced before assignment
In the import statement you assign the name turtle.
Then you assign to it in the function. If I have understood this correctly, the python parser first esttablishes thet the name turtle is assigned which makes it local. Then it processes the line turtle = turtle.Turtle() fron right to left. On discovering that you try to retrieve the value pointed to by turtle it is not assigned, you get an error.
Instead try another name:
import turtle
def make_turtle():
cat = turtle.Turtle()
return cat
eu = make_turtle()

Creating a game using pyglet (sprites in particular)

Im currently in an intro coding class and for my final project, i am trying to learn the pyglet module to create a game with a picture in the background, and have a character on the left that a user can make jump, and then have jumps come from the right at a set speed that the user will jump over. i need to use classes for the assignment, and im really having a hard time using creating a sprite class. heres my current code:
import pyglet
window = pyglet.window.Window(700,700)
image = pyglet.image.load('IMG_3315.jpg')#use 10x10 in. image
#image_2 = pyglet.image.load('IMG_3559.jpg')
main_batch = pyglet.graphics.Batch()
score_label = pyglet.text.Label(text="Score: 0", x=570, y=650, batch=main_batch)
the_jump = pyglet.image.load("jumpsi.png")
#horse = pyglet.sprite.Sprite(image_2, x = 50, y = 50)
# background_sound = pyglet.media.load(
# 'Documents/Leave The Night On.mp3',
# streaming=False)
class Jump(pyglet.sprite.Sprite):
def __init__(self, img, x=0, y=0, blend_src=770, blend_dest=771, batch=None, group=None, usage='dynamic', subpixel=False):
self.img = the_jump
self.x = 50
self.y = 50
def draw(self):
self.draw()
# verticle = Jump('verticle')
#window.event
def on_draw():
window.clear()
image.blit(0, 0)
main_batch.draw()
window = Jump()
#horse.draw()
#background_sound.play()
if __name__ == "__main__":
sprite = Jump()
pyglet.app.run()
i know its probably wrong but everything else i have tried (using preexisting games as examples) hasn't worked either.
my current error message is:
Traceback (most recent call last):
File "jumper.py", line 39, in <module>
sprite = Jump()
TypeError: __init__() takes at least 2 arguments (1 given)
im just really stuck and have been trying to figure this out for hours and not made any leeway. any help you can offer would be greatly appreciated. Thanks so much!
UPDATE: i recently changed the code, noticing the problem that Gustav pointed out, and change the end call to
if __name__ == "__main__":
sprite = Jump(the_jump)
pyglet.app.run()
but now i get the error
Traceback (most recent call last):
File "jumper.py", line 39, in <module>
sprite = Jump(the_jump)
File "jumper.py", line 21, in __init__
self.x = 50
File "/Library/Python/2.7/site-packages/pyglet/sprite.py", line 459, in _set_x
self._update_position()
File "/Library/Python/2.7/site-packages/pyglet/sprite.py", line 393, in _update_position
img = self._texture
AttributeError: 'Jump' object has no attribute '_texture'
The error message is telling you exactly which line the error is in and exactly what is wrong. You are initializing the Jump class without passing in an argument for the required parameter img.
You can fix this by either changing the initialization method or your call to Jump().
Here's an example for how to read Python's traceback.

Python Ecotect Geco Scripting "Unscriptable"

import rhinoscriptsyntax as rs
import scriptcontext
import Rhino
if ACTIVE:
for i in range(len(GEO)):
scriptcontext.doc = ghdoc
GEO_id = GEO[i]
doc_object = rs.coercerhinoobject(GEO_id)
attributes = doc_object.Attributes
geometry = doc_object.Geometry
scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc
rhino_obj = scriptcontext.doc.Objects.Add(geometry, attributes)
rs.ObjectColor(rhino_obj, color[i])
mat_id = rs.AddMaterialToObject(rhino_obj)
rs.MaterialColor(mat_id, color[i])
"Runtime error (TypeErrorException): 'Color' object is unsubscriptable
Traceback:
line 23, in script"
I have no idea why the "ObjectColor" is unscriptable, I have matched it with the python API so many times. AM I not seeing something that I should?

Python does not detect soundtouch elements (bpmdetect and pitch) in gst-plugins-bad

I have installed gstreamer, gst-plugins-bad and its python bindings.
The following code selects a song from a given directory and plays it.
import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk
import os
class Main:
def __init__(self):
self.pipeline = gst.Pipeline("mypipeline")
self.musicFiles = os.listdir("musicFiles")
self.currentSong = self.musicFiles[0]
self.findFile = gst.element_factory_make("filesrc", "file")
self.findFile.set_property("location", "musicFiles/" + self.currentSong)
self.pipeline.add(self.findFile)
self.mad = gst.element_factory_make("mad", "mad")
self.pipeline.add(self.mad)
self.findFile.link(self.mad)
self.audioconvert = gst.element_factory_make("audioconvert", "convert")
self.pipeline.add(self.audioconvert)
self.mad.link(self.audioconvert)
self.audioresample = gst.element_factory_make("audioresample", "resample")
self.pipeline.add(self.audioresample)
self.audioconvert.link(self.audioresample)
self.getbpm = gst.element_factory_make("bpmdetect", "bpm")
self.pipeline.add(self.getbpm)
self.audioresample.link(self.getbpm)
self.sink = gst.element_factory_make("playsink", "sink")
self.pipeline.add(self.sink)
self.audioresample.link(self.sink)
self.pipeline.set_state(gst.STATE_PLAYING)
start=Main()
gtk.main()
However, when I try to create the bpmdetect element, it gives the following error:
Traceback (most recent call last):
File "/Users/shubhitsingh/Desktop/Stuff/COLLEGE/US/Carnegie Mellon/Fall '12/15-112/Term Project/Practice/gstreamertutorial-1.py", line 49, in <module>
start=Main()
File "/Users/shubhitsingh/Desktop/Stuff/COLLEGE/US/Carnegie Mellon/Fall '12/15-112/Term Project/Practice/gstreamertutorial-1.py", line 37, in __init__
self.getbpm = gst.element_factory_make("bpmdetect", "bpm")
ElementNotFoundError: bpmdetect
Other elements from gst-plugins-bad can be created without problems. Its just the two elements under soundtouch (bpmdetect and pitch) that cannot be created. Help?

Categories

Resources