class Mover(object): def __init__(self, x, y, r): self.location = PVector(x, y) self.velocity = PVector(0, 0) self.radius = r self.topspeed = 10 def display(self): screen.draw.filled_circle((self.location.x, self.location.y), self.radius, (255, 0, 0)) screen.draw.circle((self.location.x, self.location.y), self.radius, (0, 0, 0)) def update(self): self.acceleration = PVector.random2D() # self.acceleration.mult(0.5) self.acceleration.mult(random.uniform(0.0, 2.0)) self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity) def check_edges(self): if (self.location.x > WIDTH - RADIUS): self.location.x = WIDTH - RADIUS self.velocity.x *= -1 elif (self.location.x < RADIUS): self.location.x = RADIUS self.velocity.x *= -1 if (self.location.y > HEIGHT - RADIUS): self.location.y = HEIGHT - RADIUS self.velocity.y *= -1 elif (self.location.y < RADIUS): self.location.y = RADIUS self.velocity.y *= -1
class Mover(): def __init__(self): self.radius = 16 # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-2, 2) v_y = random.randrange(-2, 2) self.velocity = PVector(v_x, v_y) # Farbe self.color = (239, 242, 63) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): self.position.add(self.velocity) if (self.position.x >= WIDTH + self.radius): self.position.x = -self.radius elif (self.position.x <= -self.radius): self.position.x = WIDTH - self.radius if (self.position.y >= HEIGHT + self.radius): self.position.y = -self.radius elif (self.position.y <= -self.radius): self.position.y = HEIGHT - self.radius
class Mover(): def __init__(self): self.radius = 16 # Position, Velocity, and Acceleration self.position = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) self.acceleration = PVector(0.001, -0.01) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = (239, 242, 63) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.position.add(self.velocity) if (self.position.x >= WIDTH + self.radius): self.position.x = -self.radius elif (self.position.x <= -self.radius): self.position.x = WIDTH - self.radius if (self.position.y >= HEIGHT + self.radius): self.position.y = -self.radius elif (self.position.y <= -self.radius): self.position.y = HEIGHT - self.radius
class Mover(Actor): def __init__(self, x, y, im, rotspeed): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.rotspeed = rotspeed self.angle = 90 self.location = PVector(x, y) self.velocity = PVector(0, 0) self.topspeed = 10 def update(self): mouse_x, mouse_y = pygame.mouse.get_pos() mouse = PVector(mouse_x, mouse_y) dir = mouse - self.location dir.normalize() dir.mult(0.5) self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity) self.pos = (self.location.x, self.location.y) self.angle += self.rotspeed def bounce(self): if (self.location.x > WIDTH - RADIUS) or (self.location.x < RADIUS): self.velocity.x *= -1 if (self.location.y > HEIGHT - RADIUS) or (self.location.y < RADIUS): self.velocity.y *= -1
class Ball(): def __init__(self): self.radius = random.randrange(10, 20) # Position and Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Farbe self.color = random.choice(colorlist) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): self.position.add(self.velocity) if (self.position.x >= WIDTH - self.radius) or (self.position.x <= self.radius): self.velocity.x *= -1 if (self.position.y >= HEIGHT - self.radius) or (self.position.y <= self.radius): self.velocity.y *= -1
class Being(object): def __init__(self, canvas, width, height): self.canvas = canvas self.color = (0, 0, 200) self.speed = 4 self.sight_dist = 10 self.location = PVector(randint(0, width), randint(0, height)) self.velocity = PVector(randint(-self.speed, self.speed), randint(-self.speed, self.speed)) def update(self): self.location.add(self.velocity) def draw(self): pygame.draw.rect(self.canvas, self.color, [self.location.x, self.location.y, 4, 4]) def check_edges(self, width, height): if self.location.x > width: self.location.x = 0 elif self.location.x < 0: self.location.x = width - 4 if self.location.y > height: self.location.y = 0 elif self.location.y < 0: self.location.y = height - 4 def know_beings(self, humans, zombies): self.humans = humans self.zombies = zombies
class Particle(t.Turtle): def __init__(self, tshape, tcolor, x, y): t.Turtle.__init__(self) self.penup() self.shape(tshape) self.color(tcolor) self.speed = 1 self.max_speed = 10 self.location = PVector(x, y) self.setpos(self.location.x, self.location.y) self.acceleration = PVector(0, 0.05) self.velocity = PVector(r.uniform(-1.0, 1.0), r.uniform(-2.0, 0.0)) self.lifespan = 255 def update(self): self.velocity.add(self.acceleration) self.location.add(self.velocity) self.lifespan -= r.uniform(1.5, 3.0) if self.lifespan <= 0: self.lifespan = 0 self.color((round(self.lifespan), 0, 0)) self.setpos(self.location.x, self.location.y) def isDead(self): if self.lifespan <= 0: return True else: return False
class Rocket(Actor): def __init__(self, x, y, im): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.location = PVector(x, y) self.velocity = PVector(0, 0) self.frame = 0 self.angle = 90 self.topspeed = 10 def update(self): mouse_x, mouse_y = pygame.mouse.get_pos() mouse = PVector(mouse_x, mouse_y) dir = mouse - self.location dir.normalize() dir.mult(0.5) self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity) self.pos = (self.location.x, self.location.y) # self.angle = self.angle_to((mouse_x, mouse_y)) self.angle = -self.velocity.heading() * (180 / math.pi) def make_animation(self): if self.frame <= 5: self.image = "ship1" elif self.frame <= 10: self.image = "ship2" elif self.frame <= 15: self.image = "ship3" elif self.frame <= 20: self.image = "ship4" if self.frame >= 20: self.frame = 0 self.frame += 1 def check_edges(self): if (self.location.x > WIDTH - RADIUS): self.location.x = RADIUS elif (self.location.x < RADIUS): self.location.x = WIDTH - RADIUS if (self.location.y > HEIGHT - RADIUS): self.location.y = RADIUS elif (self.location.y < RADIUS): self.location.y = HEIGHT - RADIUS
class Ball(Actor): def __init__(self, x, y, v_x, v_y, radius, image): super().__init__(self, image) self.radius = radius self.position = PVector(x, y) self.velocity = PVector(v_x, v_y) def show(self): self.draw() def move(self): self.position.add(self.velocity) if (self.position.x > WIDTH - RADIUS) or (self.position.x < RADIUS): self.velocity.x *= -1 if (self.position.y > HEIGHT - RADIUS) or (self.position.y < RADIUS): self.velocity.y *= -1
class Ball(): def __init__(self, x, y, v_x, v_y, radius, color): self.position = PVector(x, y) self.radius = radius self.color = color self.velocity = PVector(v_x, v_y) def show(self, screen): screen.draw.filled_circle((self.position.x, self.position.y), self.radius, self.color) def move(self): self.position.add(self.velocity) if (self.position.x > WIDTH - self.radius) or (self.position.x < self.radius): self.velocity.x *= -1 if (self.position.y > HEIGHT - self.radius) or (self.position.y < self.radius): self.velocity.y *= -1
class Mover(): def __init__(self, m, x, y): self.mass = m self.radius = m*5 self.color = random.choice(colorlist) self.location = PVector(x, y) self.velocity = PVector(0, 0) self.acceleration = PVector(0, 0) def draw(self): arcade.draw_circle_filled(self.location.x, self.location.y, self.radius, self.color) arcade.draw_circle_outline(self.location.x, self.location.y, self.radius, arcade.color.BLACK) def apply_force(self, force): f = PVector.sdiv(force, self.mass) self.acceleration.add(f) def update(self): self.velocity.add(self.acceleration) self.location.add(self.velocity) self.acceleration.mult(0) if (self.location.x >= WIDTH - self.radius): self.location.x = WIDTH - self.radius self.velocity.x *= -1 elif (self.location.x <= self.radius): self.location.x = self.radius self.velocity.x *= -1 if (self.location.y >= HEIGHT - self.radius): self.location.y = HEIGHT - self.radius self.velocity.y *= -1 elif (self.location.y <= self.radius): self.location.y = self.radius self.velocity.y *= -1
class Mover(): def __init__(self): self.radius = random.randrange(10, 20) self.mouse = PVector(200, 200) # Position und Velocity x = random.randrange(self.radius, WIDTH - self.radius) y = random.randrange(self.radius, HEIGHT - self.radius) self.position = PVector(x, y) v_x = random.randrange(-10, 10) v_y = random.randrange(-10, 10) self.velocity = PVector(v_x, v_y) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = random.choice(colorlist) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): dir = self.mouse - self.position dir.normalize() dir.mult(0.5) self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.position.add(self.velocity) if (self.position.x >= WIDTH + self.radius): self.position.x = -self.radius elif (self.position.x <= -self.radius): self.position.x = WIDTH - self.radius if (self.position.y >= HEIGHT + self.radius): self.position.y = -self.radius elif (self.position.y <= -self.radius): self.position.y = HEIGHT - self.radius
class Mover(object): def __init__(self): self.location = PVector(randint(0, HEIGHT), randint(0, WIDTH)) self.velocity = PVector(0, 0) self.topspeed = randint(6, 12) self.radius = randint(8, 24) self.color = choice(colorlist) def display(self): screen.draw.filled_circle((self.location.x, self.location.y), self.radius, self.color) screen.draw.circle((self.location.x, self.location.y), self.radius, (0, 0, 0)) def update(self): mouse_x, mouse_y = pygame.mouse.get_pos() mouse = PVector(mouse_x, mouse_y) dir = mouse - self.location dir.normalize() dir.mult(0.5) self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity) def check_edges(self): if (self.location.x > WIDTH - self.radius): self.location.x = WIDTH - self.radius self.velocity.x *= -1 elif (self.location.x < self.radius): self.location.x = self.radius self.velocity.x *= -1 if (self.location.y > HEIGHT - self.radius): self.location.y = HEIGHT - self.radius self.velocity.y *= -1 elif (self.location.y < self.radius): self.location.y = self.radius self.velocity.y *= -1
class Mover(): def __init__(self): self.radius = 16 self.mouse = PVector(200, 200) # Position und Velocity self.position = PVector(WIDTH / 2, HEIGHT / 2) self.velocity = PVector(0, 0) # Maximalgeschwindigkeit self.topspeed = 10 # Farbe self.color = (239, 242, 63) def draw(self): arcade.draw_circle_filled(self.position.x, self.position.y, self.radius, self.color) arcade.draw_circle_outline(self.position.x, self.position.y, self.radius, arcade.color.BLACK) def update(self): # Richtung dir = self.mouse - self.position dir.normalize() dir.mult(0.5) # Acceleration self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.position.add(self.velocity) if (self.position.x >= WIDTH + self.radius): self.position.x = -self.radius elif (self.position.x <= -self.radius): self.position.x = WIDTH - self.radius if (self.position.y >= HEIGHT + self.radius): self.position.y = -self.radius elif (self.position.y <= -self.radius): self.position.y = HEIGHT - self.radius
class Mover(Actor): def __init__(self, x, y, im, rotspeed): super().__init__(im, (x, y)) self.pos = (x, y) self.im = im self.rotspeed = rotspeed self.angle = 90 self.location = PVector(x, y) self.velocity = PVector(random.uniform(-2, 2), random.uniform(-2, 2)) # self.acceleration = PVector(-0.001, 0.01) self.topspeed = 10 def update(self): self.acceleration = PVector.random2D() self.acceleration.mult(0.5) # self.acceleration.mult(random.uniform(0, 2)) self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity) self.pos = (self.location.x, self.location.y) self.angle += self.rotspeed def check_edges(self): if (self.location.x > WIDTH - RADIUS): self.location.x = RADIUS elif (self.location.x < RADIUS): self.location.x = WIDTH - RADIUS if (self.location.y > HEIGHT - RADIUS): self.location.y = RADIUS elif (self.location.y < RADIUS): self.location.y = HEIGHT - RADIUS def bounce(self): if (self.location.x > WIDTH - RADIUS) or (self.location.x < RADIUS): self.velocity.x *= -1 if (self.location.y > HEIGHT - RADIUS) or (self.location.y < RADIUS): self.velocity.y *= -1
class Mover(object): def __init__(self, x, y, r): self.location = PVector(x, y) self.velocity = PVector(0, 0) self.radius = r self.topspeed = 10 def display(self): screen.draw.filled_circle((self.location.x, self.location.y), self.radius, (255, 0, 0)) screen.draw.circle((self.location.x, self.location.y), self.radius, (0, 0, 0)) def update(self): mouse_x, mouse_y = pygame.mouse.get_pos() mouse = PVector(mouse_x, mouse_y) dir = mouse - self.location dir.normalize() dir.mult(0.5) self.acceleration = dir self.velocity.add(self.acceleration) self.velocity.limit(self.topspeed) self.location.add(self.velocity) def check_edges(self): if (self.location.x > WIDTH - RADIUS): self.location.x = WIDTH - RADIUS self.velocity.x *= -1 elif (self.location.x < RADIUS): self.location.x = RADIUS self.velocity.x *= -1 if (self.location.y > HEIGHT - RADIUS): self.location.y = HEIGHT - RADIUS self.velocity.y *= -1 elif (self.location.y < RADIUS): self.location.y = RADIUS self.velocity.y *= -1
location = PVector(100, 100) velocity = PVector(1.0, 3.3) win = gfx.GraphWin("Bouncing Ball", WIDTH, HEIGHT, autoflush = False) win.setBackground("yellow") c = gfx.Circle(gfx.Point(location.x, location.y), RADIUS) c.setFill("red") c.setOutline("black") c.draw(win) keep_going = True i = 0 while keep_going: c.undraw() location.add(velocity) # check border if (location.x > WIDTH - RADIUS) or (location.x < RADIUS): velocity.x *= -1 if (location.y > HEIGHT - RADIUS) or (location.y < RADIUS): velocity.y *= -1 while i > 1200: keep_going = False win.close() sys.exit() i += 1 c.move(velocity.x, velocity.y) c.draw(win) gfx.update()