def tick(self, turn_rate1=0, turn_rate2=0): """ Promene polozaja objekata na svaki otkucaj tajmera""" if self.game_over or self.won: return # pomeranje paddle-ova old_x1 = self.paddle1.left # self.paddle1.move(turn_rate1) # old_x2 = self.paddle2.left # self.paddle2.move(turn_rate2) # self.normalize_paddle_location() # pomeranje lopte self.ball.move(self.paddle1.left - old_x1) self.reflect_ball() if self.ball.middle > self.border_line: self.kill_player() if self.level_completed: self.player1.score += 100 * self.current_level self.player2.score += 100 * self.current_level if not self.get_next_level(): self.won = True # detekcija kolizije lopte i bloka blocks_to_remove = set() for block in self.level.blocks: if block.contact_two_frames(self.ball): blocks_to_remove.add(block) if len(blocks_to_remove) != 0: self.remove_blocks(blocks_to_remove) self.remove_bonuses() # detekcija kolizije lopte i paddle-a, racunanje smera odbijanja lopte if self.ball.contact_two_frames(self.paddle1): self.paddle_reflect_ball = 1 mid = self.paddle1.right - self.paddle1.width / 2 ball_mid = self.ball.right - self.ball.width / 2 self.ball.direction = Vector.from_angle(-pi / 2 + (pi / 2.75 * (ball_mid - mid) / (self.paddle1.width / 2))) if self.ball.contact_two_frames(self.paddle2): self.paddle_reflect_ball = 2 mid = self.paddle2.right - self.paddle2.width / 2 ball_mid = self.ball.right - self.ball.width / 2 self.ball.direction = Vector.from_angle(-pi / 2 + (pi / 2.75 * (ball_mid - mid) / (self.paddle2.width / 2)))
def _update(self): self._update_cnt += 1 temp_list = self.p_list.copy() for i, _ in enumerate(temp_list): temp_list[i] = temp_list[i].copy() for i, p in enumerate(temp_list): if p.is_alive is False: continue # update velocity a = Vector() for other_p in temp_list: if other_p == p or other_p.is_alive is False: continue elif (other_p.pos - p.pos).norm < 1: collide(p, other_p) continue else: a += gravitation(p, other_p) / p.m v = update_vec(p.v, a) # update position pos = update_vec(p.pos, p.v) temp_list[i].pos = pos temp_list[i].v = v if self._update_cnt % 50 == 0: self.line_list[i][0].append(pos.x) self.line_list[i][1].append(pos.y) if len(self.line_list[i][0]) > 1000: self.line_list[i][0].pop(0) self.line_list[i][1].pop(0) self.p_list = temp_list.copy()
def move(): # Mov of the ball and target if randrange(40) == 0: y = randrange(-150, 150) target = Vector(200, y) targets.append(target) for target in targets: target.x -= 0.5 if inside(ball): speed.y -= 0.35 ball.move(speed) duplicate = targets.copy() targets.clear() for target in duplicate: if abs(target - ball) > 13: targets.append(target) draw() for target in targets: if not inside(target): return ontimer(move, 50)
def __init__(self, x, y, size, velocity, direction): super().__init__(x, y, size) self.velocity = velocity self.direction = Vector(*direction)
from random import * from turtle import * from base import Vector ant = Vector(0, 0) aim = Vector(2, 0) def wrap(value): return value def draw(): ant.move(aim) ant.x = wrap(ant.x) ant.y = wrap(ant.y) aim.move(random() - 0.5) aim.rotate(random()*10 - 5) clear() goto(ant.x, ant.y) dot(4) if running: ontimer(draw, 60) setup(420, 420, 370, 0) hideturtle() tracer(False) up() running = True draw() done()
from random import * from turtle import * from base import Vector ball = Vector(-200, -200) speed = Vector(0, 0) targets = [] def inside(x_y): return -200 < x_y.x < 200 and -200 < x_y.y < 200 def tap(x, y): # Launch the ball when the screen is tapped # Only one ball at time if not inside(ball): ball.x = -199 ball.y = -199 speed.x = (x + 200) / 5 speed.y = (y + 200) / 25 def draw(): # Draw balls and targets clear() for target in targets: goto(target.x, target.y) dot(20, "Blue") if inside(ball):
from base import Vector, Particle from PlayBoard import PlayBoard if __name__ == '__main__': p_list = [ Particle(60, Vector(10, 20), Vector(0.5, -0.5)), Particle(60, Vector(50, 10), Vector(-1.5, 0)), Particle(60, Vector(35, 0), Vector(2, 0.2)), ] pb = PlayBoard(*p_list) pb.simulate()
from random import * from turtle import * from base import Vector screen = Screen() turtle = Turtle() def Value(): # This function will generate value from [-5, -3] to [3, 5] return (3 + random() * 2) * choice([-1, 1]) ball = Vector(0, 0) aim = Vector(Value(), Value()) def draw(): # Move the ball and draw the screen ball.move(aim) x = ball.x y = ball.y if x < -200 or x > 200: aim.x = -aim.x if y < -200 or y > 200: aim.y = -aim.y clear()