Aug-15-2021, 06:17 AM
I have created a scenario for a bouncing ball
Firstly i have successfully created a class, and code was working wonders, later i tried to separate the coda as a Module which i will be explaining below and is giving error, Kindly help me to resolve
First i have created a Module in ..\drawing\crazyball.py, and code is as below
Thanks in Advance.
Firstly i have successfully created a class, and code was working wonders, later i tried to separate the coda as a Module which i will be explaining below and is giving error, Kindly help me to resolve
First i have created a Module in ..\drawing\crazyball.py, and code is as below
import pygame, sys import random import math from pygame.locals import * # In[2]: class Particle: def __init__(self): self.x = 150 self.y = 150 self.size = 50 self.colour = (255, 0, 0) self.speed = 0 self.angle = 0 def display(self): pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size) def move(self): self.x += math.sin(self.angle) * self.speed self.y -= math.cos(self.angle) * self.speed #Here is the bounce function. def bounce(self): #This if statement is for when the circle exceeds the right wall. #This means the circle's x value is greater than the windows width subtract its size, meaning it exceeded the right wall. if self.x > width - self.size: #This is the reflected position represented as an x-coordinate. self.x = 2*(width - self.size) - self.x #This reflects the circle off the right wall at the same angle at which it hit the wall. self.angle = - self.angle #If the first 'if' statement is not true, then this 'elif' statement will be tested. #This means the circle's x value is lesser than the size of the circle, which means it has crossed the left wall/ elif self.x < self.size: self.x = 2*self.size - self.x self.angle = - self.angle #If the other statements aren't true, this statement will be tested. #This means the circle's y value is greater than windows height subtract the circles size. #Here, the circle exceeds the bottom wall. if self.y > height - self.size: self.y = 2*(height - self.size) - self.y #This line is a bit different because a horizontal boundary has an angle of pi, or 180 degrees. #Pi subtract the angle at which the circle hits the wall will give the reflected angle. self.angle = math.pi - self.angle #If the other statements aren't true, this statement will be tested. #This means the circle's y value is less than its size, which means it exceeded the top wall. elif self.y < self.size: self.y = 2*self.size - self.y self.angle = math.pi - self.angleand the module i am calling in Main code: __init__.pynb file, the code is as below:
import pygame, sys import random import math import importlib import drawing.crazyball importlib.reload(drawing.crazyball) from pygame.locals import * background_colour = ( 0,0,0) #You should make your window bigger so the circles have more space to move. width= 800 height= 600 screen = pygame.display.set_mode((width,height)) pygame.display.set_caption('Circles and boundaries') particle = Particle() particle.speed = 0.14 particle.angle = random.uniform(3, math.pi*2) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill(background_colour) particle.move() #This allows the circles to 'bounce' off the sides of the screen. particle.bounce(width,height) particle.display() pygame.display.flip()The code is giving me following error:
Error:---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-05360eb658bd> in <module>
6 screen = pygame.display.set_mode((width,height))
7 pygame.display.set_caption('Circles and boundaries')
----> 8 particle = Particle()
9 particle.speed = 0.14
10 particle.angle = random.uniform(3, math.pi*2)
NameError: name 'Particle' is not defined
Kindly help, this was working perfect when was on single code, but when i tried created a module it is giving the error.Thanks in Advance.