#python oop referencing attributes in objects
# problem is the angle function in my class.
import random
class people():
def __init__(self,x,y,size_of_other_team):
self.x=x
self.y=y
self.target = random.randint(0,size_of_other_team) # choose a
random guy from other team
def angle(self, other_teams_x, other_teams_y): # get angle
between this guy and his target
dx = other_teams_x[self.target]-self.x
dy = other_teams_y[self.target]-self.y
theta = math.atan(dy/dx)
return(theta)
def move(self):
self.x+=1
red_guys = 10
blue_guys = 10
red_x=[]
red_y=[]
red=[] # objects list
for i in range(red_guys):
red_x.append(random.randint(0,100))
red_y.append(random.randint(0,100))
red_person = people(red_x[i],red_y[i],blue_guys)
red.append(red_person)
blue_x=[]
blue_y=[]
blue=[] # object list
for i in range (blue_guys):
blue_x.append(random.randint(0,100))
blue_y.append(random.randint(0,100))
blue_person = people(blue_x[i],blue_y[i],red_guys)
blue.append(blue_person)
a=0
while a < 9999:
for i in red:
i.move()
print(i.angle(blue.x,blue,y)) ## i want to get the angle between
each red guy and his chosen blue target
## problem is here blue.x doesnt work like i want.
#i want blue.x to = ALL blue x's
## DO NOT USE THE BLUE_X LIST. i want to reference x and y from the
objects
You can change the code to something like this
Earlier
print(i.angle(blue.x, blue.y))
My suggestion,
print(i.angle([x for b.x in blue],[y for b.y in blue]))
Here blue is a list of people object. angle requires list as argument.
So we give list like mentioned above.
Hope this is what you require.
Regards.
#python oop referencing attributes in objects # problem is the angle function in my class. import...
## python oop creating a large number of instances ## im trying to create a large number of instances for a class. could you show me a way to make it work import math import random class soldiers: def __init__(self,x,y): self.x = x self.y = y def get_target(self,x,y,number_red_soldiers,number_blue_soldiers): pass # self.target = def aiming_angle(self,x,y,target,enemy_x,enemy_y): dy = self.y-enemy_y[target] dx = self.x-enemy_x[target] angle =math.atan(dy/dx) a = 0 number_blue_soldiers= 10 number_red_soldiers = 10 for i in range(number_blue_soldiers): blue_x = random.randint(0,100,10) blue_y = random.randint(0,100,10)...
[Problem 5] List all that the following Python class is missing in order to function. [10 Points] import math class TwoCoordinatePoints (object): #This class represents a point A = (x, y) with integer values for x and y. -_init__(x, y): self.x = x self.y = y def -_str__(self): "(%, %)" % (self.x, self.y) def distance (self, P): #computes the distance between two points (instances) of this class return math.sqrt((self.x-P.x)**2+(self.y-P.y)**2)
Here is a Python program. import turtle class Box: def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height def show(self): turtle.up() turtle.goto(x, y) turtle.down() for _ in range(2): turtle.forward(width) turtle.right(90) turtle.forward(height) turtle.right(90) bigbox = Box(-100, -150, 200, 300) bigbox.show() turtle.done() If you run the above program, it will produce an error. What is the keyword that has...
Here is my code for minesweeper in python and it has something
wrong. Could you please help me to fix it?
import tkinter as tk
import random
class Minesweeper:
def __init__(self):
self.main = tk.Tk()
self.main.title("mine sweeper")
self.define_widgets()
self.mines = random.sample( [(i,j) for i in range(25) for j in
range(50) ],self.CustomizeNumberOfMines())
print(self.mines)
self.main.mainloop()
self.CustomizeNumberOfMines()
def define_widgets(self):
""" Define a canvas object, populate it with squares and
possible texts """
self.canvas = tk.Canvas(self.main, width = 1002, height=502,
bg="#f0f0f0")
self.canvas.grid(row=0, column=0)
self.boxes =...
How to add class objects to a dictionary? *PYTHON ONLY* NO IMPORT RANDOM Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict (outside of the Employee class) that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses. The function should take the first value...
Using Python
a) Add a function to the Projectle class below so it can print out the current status seconds, position, and total velocity (not the x,y components) It should work as ilustrated bekow, so you can directly print the instance of the Projectie object b) If the projectile reaches ts maximum height automatically print a message with the current time and height. Make sure this only prints once if time is updated continucusty You do not need to calculate...
I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...
I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...
Question 2 : Virus Wars A popular subgenre of strategy game is the so-called Virus War format, where the player is shown a field of cells, each with a virus count, and may attack other cells within a certain range. We are going to write some classes in Python to implement a simple Virus Wars game. 2a) The position class (9 points) Everything within this game is going to require a position, so it makes sense to define it up...
What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...