class ShotTracker: def __init__(self, win, angle, velocity, height): """win is the GraphWin to display the shot. angle, velocity, and height are initial projectile parameters. """ self.proj = Projectile(angle, velocity, height) self.marker = Circle(Point(0, height), 3) self.marker.setFill("red") self.marker.setOutline("red") self.marker.draw(win) def update(self, dt): """Move the shot dt seconds farther along its flight """ # Update the projectile self.proj.update(dt) # Moves the circle to the new projectile location center = self.marker.getCenter() dx = self.proj.getX() - center.getX() dy = self.proj.getY() - center.getY() self.marker.move(dx, dy) def getX(self): """ return the current x coordinate of the shot's center """ return self.proj.getX() def getY(self): """ return the current y coordinate of the shot's center """ return self.proj.getY() def undraw(self): """ undraw the shot """ self.marker.undraw()
class Wheel(): def __init__(self, center, wheel_radius, tire_radius): self.tire_circle = Circle(center, tire_radius) self.wheel_circle = Circle(center, wheel_radius) def draw(self, win): self.tire_circle.draw(win) self.wheel_circle.draw(win) def move(self, dx, dy): self.tire_circle.move(dx, dy) self.wheel_circle.move(dx, dy) def set_color(self, wheel_color, tire_color): self.tire_circle.setFill(tire_color) self.wheel_circle.setFill(wheel_color) def undraw(self): self.tire_circle.undraw() self.wheel_circle.undraw() def get_size(self): return self.tire_circle.getRadius() def get_center(self): return self.tire_circle.getCenter() def animate(self, win, dx, dy, n): if n > 0: self.move(dx, dy) win.after(100, self.animate, win, dx, dy, n - 1)
class Asteroid: def __init__(self, pos, dest, win, vel, type): self.pos = pos self.dest = Point(dest.getX() - pos.getX(), dest.getY() - pos.getY()) self.vel = vel self.type = type self.aliveFlag = True self.outOfBounds = False r = 25 * type self.object = Circle(self.pos, r) self.object.setFill(color_rgb(randrange(100, 255), 240, 90)) self.draw(win) def draw(self, win): self.object.draw(win) def undraw(self): self.object.undraw() def update(self, dt, win): #normalize values dest = norm(self.dest.getX(), self.dest.getY()) #calculate x and y movement x = dest.getX() * self.vel * dt y = dest.getY() * self.vel * dt self.object.move(x, y) self.pos = self.object.getCenter() if self.pos.getX() < -100: self.outOfBounds = True def getAliveFlag(self): return self.aliveFlag def setAliveFlag(self, statement=True): self.aliveFlag = statement def getOutOfBounds(self): return self.outOfBounds def getType(self): return self.type def getObject(self): return self.object
class Bullet: def __init__(self, pos, win, r, dest, vel=1.0, c=color_rgb(255, 255, 255)): self.pos = pos #position self.dest = Point(dest.getX() - pos.getX(), dest.getY() - pos.getY()) #destination self.radius = r #radius self.vel = vel #velocity self.object = Circle(self.pos, r) #Circle self.object.setFill(c) #set colour self.object.setOutline('black') self.draw(win) #draw self.aliveFlag = True def getAliveFlag(self): return self.aliveFlag def update(self, dt, win): #normalize values dest = norm(self.dest.getX(), self.dest.getY()) #calculate x and y movement x = dest.getX() * self.vel * dt y = dest.getY() * self.vel * dt #move object self.object.move(x, y) #set position self.pos = self.object.getCenter() #Split into two if statements for readability if self.pos.getX() > win.getWidth() or self.pos.getY() > win.getHeight( ): self.aliveFlag = False elif self.pos.getX() < 0 or self.pos.getY() < 0: self.aliveFlag = False def getObject(self): return self.object def draw(self, win): self.object.draw(win) #remove object from screen def undraw(self): self.object.undraw()
class Particle: def __init__(self, window, p=Point(0, 0)): self.particle = None self.drawn = False self.color = "RED" self.position = p self.x = p.getX() self.y = p.getY() self.size = 3 self.dX = 0 self.dY = 0 self.win = window self.particleTurnCount = 0 def setCoord(self, x, y): self.x = x self.y = y def setColor(self, color): self.color = color if self.particle: self.particle.setFill(color) def setSize(self, size): self.size = size if self.drawn: self.undraw() self.draw() def draw(self): self.particle = Circle(Point(self.x, self.y), self.size) self.particle.setFill(self.color) self.particle.draw(self.win) self.drawn = True def undraw(self): self.particle.undraw() self.drawn = False def setParticleMovement(self, dx, dy): self.dX = dx self.dY = dy def move(self): self.particle.move(self.dX, self.dY) self.position = Point(self.position.getX() + self.dX, self.position.getY() + self.dY) self.particle.undraw() self.particle.draw(self.win)
def __init__(self,window,P1,level): from graphics import GraphWin,Circle import random from time import time self.p1=P1 self.window=window self.cir=Circle(self.p1,0.4) self.level=level self.cir.setWidth(0) i=random.randrange(6,9+self.level) self.color=i if self.color==6: #颜色对应数字 self.cir.setFill("red") if self.color==7: self.cir.setFill("green") if self.color==8: self.cir.setFill("black") if self.color==9: self.cir.setFill("blue") if self.color==10: self.cir.setFill("orange") for s in range(100): cirs=Circle(self.p1,0.0+s*0.004) if self.color==6: cirs.setFill("red") if self.color==7: cirs.setFill("green") if self.color==8: cirs.setFill("black") if self.color==9: cirs.setFill("blue") if self.color==10: cirs.setFill("orange") cirs.draw(self.window) empty=0 #空循环控制时间(time间隔太大) while empty<=30000: empty=empty+1 cirs.undraw() self.cir.draw(self.window) self.activate=True
class ShotTracker: def __init__(self, win, angle, velocity, height): self.proj = Projectile(angle, velocity, height) self.marker = Circle(Point(0, height), 3) # 圆心, 半径 self.marker.setFill('red') self.marker.setOutline('red') self.marker.draw(win) def update(self, dt): self.proj.update(dt) center = self.marker.getCenter() dx = self.proj.getX() - center.getX() dy = self.proj.getY() - center.getY() self.marker.move(dx, dy) def getX(self): return self.proj.getX() def getY(self): return self.proj.getY() def undraw(self): self.marker.undraw()
class circle: def __init__(self,window,P1,level): from graphics import GraphWin,Circle import random from time import time self.p1=P1 self.window=window self.cir=Circle(self.p1,0.4) self.level=level self.cir.setWidth(0) i=random.randrange(6,9+self.level) self.color=i if self.color==6: #颜色对应数字 self.cir.setFill("red") if self.color==7: self.cir.setFill("green") if self.color==8: self.cir.setFill("black") if self.color==9: self.cir.setFill("blue") if self.color==10: self.cir.setFill("orange") for s in range(100): cirs=Circle(self.p1,0.0+s*0.004) if self.color==6: cirs.setFill("red") if self.color==7: cirs.setFill("green") if self.color==8: cirs.setFill("black") if self.color==9: cirs.setFill("blue") if self.color==10: cirs.setFill("orange") cirs.draw(self.window) empty=0 #空循环控制时间(time间隔太大) while empty<=30000: empty=empty+1 cirs.undraw() self.cir.draw(self.window) self.activate=True def click(self,pr): from graphics import Circle import time pd=False if self.activate==True and (pr.getX()-self.p1.getX())**2+(pr.getY()-self.p1.getY())**2<=0.24: for i in range(3): #点击动画 self.cir.move(0,-0.12/3.0) time.sleep(0.01) for i in range(3): self.cir.move(0,0.12/3.0) time.sleep(0.01) for i in range(3): self.cir.move(0,-0.12/3.0) time.sleep(0.01) for i in range(3): self.cir.move(0,0.12/3.0) time.sleep(0.01) pd=True return pd def undraw(self): self.cir.undraw() def close(self): self.cir.undraw() self.activate=False def move(self,pr): self.cir.move(pr.getX()-self.p1.getX(),pr.getY()-self.p1.getY()) self.p1.move(pr.getX()-self.p1.getX(),pr.getY()-self.p1.getY())
def find_the_circle(): """ 10. [harder] Write a find_the_circle() function to play a simple game. This should start by displaying a graphics window, and creating (but not displaying) a circle of radius 30 at a random position (use the randint function from the random module). The user should then have 10 attempts at locating the circle (by clicking on the graphics window). Each time (except the first) the user misses the circle, a "getting closer" or "getting further away" message should be displayed (depending on the position of the current and previous clicks). If the user manages to find the circle (by clicking within its circumference), then the circle should be displayed and the user given some points: 10 points for finding it with the first click, down to 1 point for finding it with the 10th click. The game then restarts. However, each time the game restarts the circle should be given a new random position and its radius reduced by 10%. The game ends when the user fails to find the circle within 10 clicks. The total number of points scored should then be displayed. Note: game is too hard with radius 30 circle so I've upped it to 60. """ win = GraphWin("Find the circle", 300, 440) score, radius, loss = 0, 60, False divider = Rectangle(Point(-1, 300), Point(300, 360)) divider.setFill("gray") divider.draw(win) warning_text = Text(Point(150, 330), "Click in the space\nabove this grey " "rectangle") warning_text.setSize(12) warning_text.setStyle("bold") warning_text.draw(win) status_text = Text(Point(150, 385), "") status_text.setSize(12) status_text.setStyle("bold") status_text.draw(win) score_text = Text(Point(150, 415), "Score: 0") score_text.setSize(12) score_text.setStyle("bold") score_text.draw(win) points_text = Text(Point(150, 25), "") points_text.setSize(14) points_text.setStyle("bold") points_text.draw(win) while True: centre_x, centre_y = random.randint(0, 300), random.randint(0, 300) centre = Point(centre_x, centre_y) hidden_circle = Circle(centre, radius) hidden_circle.setFill("red") status_text.setText("Find the circle!") distances = [] for i in range(1, 11): while True: cursor = win.getMouse() cursor_y = cursor.getY() if cursor_y <= 300: break distance = distance_between_points(centre, cursor) distances.append(distance) if distance <= radius: hidden_circle.draw(win) divider.undraw() divider.draw(win) points_text.undraw() points_text.draw(win) if i == 1: points_text.setText("You get 10 points!") status_text.setText("Click 1: you got it first try") else: status_text.setText(f"Click {i}: you got it") if i == 10: points_text.setText("You get 1 point!") else: points_text.setText(f"You get {11-i} points!") radius *= 0.9 score += 11 - i time.sleep(4) hidden_circle.undraw() status_text.setText("") points_text.setText("") score_text.setText(("Score:", score)) break if i == 1: status_text.setText("Click 1: missed") elif distances[i - 1] > distances[i - 2]: status_text.setText(f"Click {i}: further away") elif distances[i - 1] == distances[i - 2]: status_text.setText(f"Click {i}: same distance") else: status_text.setText(f"Click {i}: closer") if i == 10: loss = True break if loss: score_text.setText("Game over") hidden_circle.draw(win) divider.undraw() divider.draw(win) points_text.undraw() points_text.draw(win) break points_text.setText(f"Your score is {score}") time.sleep(4) win.close()
class Player: def __init__(self, pos, r, vel=1.0, c=color_rgb(255, 255, 255)): self.pos = pos #position self.radius = r #radius self.vel = vel #velocity self.colour = c self.object = Circle(self.pos, r) self.object.setFill(c) self.object.setOutline('black') self.bullets = [] #bullet list self.score = 0 print(self.object.getRadius()) def modifyScore(self, value): self.score += value #print("Score:"+str(self.score)) def getScore(self): return self.score def update(self, dt, win): x = 0.0 y = 0.0 if k.kUp(): #If key up is pressed y -= self.vel * dt if k.kDown(): #If key down is pressed y += self.vel * dt if k.kLeft(): #If key left is pressed x -= self.vel * dt if k.kRight(): #If key right is pressed x += self.vel * dt #Check if mouse has been clicked mouse = k.kMouseLeft(win) if mouse != None: #print(mouse) self.bullets.append( Bullet(self.pos, win, 10, Point(mouse.getX(), mouse.getY()), 1000, self.colour)) #Update bullets and check if out of bounds for bullet in self.bullets: bullet.update(dt, win) if bullet.getAliveFlag() != True: bullet.undraw() self.bullets.remove(bullet) self.object.move(x, y) self.pos = self.object.getCenter() def getBullets(self): return self.bullets def getObject(self): return self.object def getRadius(self): return self.object.getRadius() def getPos(self): return self.pos def draw(self, win): self.object.draw(win) #undraw object def undraw(self): self.object.undraw()
class Ball: def __init__(self, window: GraphWin, initial_position: Vector, radius: float, speed: float, initial_direction: Vector, score: Score, lose_life_function): self.window = window self.initial_direction = initial_direction self.initial_position = initial_position self.radius = radius self.initial_speed = speed self.current_speed = speed self.score = score self.is_alive = True self.still_ball = True self.position = self.initial_position self.velocity = self.initial_direction * speed self.sprite = Circle(self.position.to_point(), self.radius) self.damage = lose_life_function def draw(self, window: GraphWin): self.sprite.undraw() self.sprite = Circle(self.position.to_point(), self.radius) self.sprite.setFill("green") self.sprite.draw(window) def undraw(self): self.sprite.undraw() def reset(self): self.move_ball(self.initial_position) self.velocity = self.initial_direction * self.initial_speed self.is_alive = True self.still_ball = True self.current_speed = self.initial_speed def release(self): self.still_ball = False def update(self, elapsed_time: float, player: Player, squares: Sequence[PointSquare]): if self.still_ball: self.move_ball(player.position + Vector(0, -(self.radius + 10))) return self.world_collide() potential_position = self.position + (self.velocity * elapsed_time) new_potential_position, collided = self.collide_player( player, potential_position) if collided: potential_position = new_potential_position else: for square in squares: if not square.is_active: continue new_potential_position, collided = self.collide_player( square, potential_position) if collided: square.hit() potential_position = new_potential_position break self.move_ball(potential_position) def move_ball(self, new_position: Vector): delta = new_position - self.position self.sprite.move(delta.x, delta.y) self.position = new_position def world_collide(self): if self.position.x - self.radius <= 0 or self.position.x + self.radius >= self.window.width: self.velocity = Vector(-self.velocity.x, self.velocity.y) if self.position.x > self.window.width / 2: self.move_ball( Vector(self.window.width - (self.radius + 5), self.position.y)) else: self.move_ball(Vector(0 + (self.radius + 5), self.position.y)) if self.position.y - self.radius <= 0 or self.position.y + self.radius >= self.window.height: self.velocity = Vector(self.velocity.x, -self.velocity.y) if self.position.y > self.window.height / 2: self.damage() else: self.move_ball(Vector(self.position.x, 0 + self.radius + 5)) def collide_player(self, box_collider: Box, potential_position): # potential_position = self.position + self.velocity collided = False if type(box_collider) is Player: is_player = True else: is_player = False nearest_point = Vector(0, 0) nearest_point.x = max( box_collider.position.x - box_collider.half_size.x, min(potential_position.x, box_collider.position.x + box_collider.half_size.x)) nearest_point.y = max( box_collider.position.y - box_collider.half_size.y, min(potential_position.y, box_collider.position.y + box_collider.half_size.y)) ray_to_nearest = nearest_point - potential_position overlap = self.radius - ray_to_nearest.mag() if ray_to_nearest.mag() == 0: overlap = 0 if overlap > 0: potential_position = potential_position - ( ray_to_nearest.normalized() * overlap) right = (Vector(1, 0)).cos_angle(potential_position - box_collider.position) right_rec = (Vector(1, 0)).cos_angle( Vector(box_collider.half_size.x, -box_collider.half_size.y)) left_rec = (Vector(1, 0)).cos_angle( Vector(-box_collider.half_size.x, -box_collider.half_size.y)) collided_top = left_rec <= right <= right_rec top = (Vector(0, 1)).cos_angle(potential_position - box_collider.position) down_rec = (Vector(0, 1)).cos_angle( Vector(-box_collider.half_size.x, box_collider.half_size.y)) top_rec = (Vector(0, 1)).cos_angle( Vector(-box_collider.half_size.x, -box_collider.half_size.y)) collided_right = top_rec <= top <= down_rec if collided_top: if not is_player: self.velocity = Vector(self.velocity.x, -self.velocity.y) else: self.velocity = (potential_position - box_collider.position ).normalized() * self.current_speed if collided_right: self.velocity = Vector(-self.velocity.x, self.velocity.y) if not is_player: self.score.add_score() self.current_speed += 10 self.velocity = self.velocity.normalized() * self.current_speed collided = True return potential_position, collided
class Dado(object): def __init__(self, v, centro, ancho, alto): x, y = centro.getX(), centro.getY() w, h = ancho /2, alto /2 self.ventana = v self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.dado = Rectangle(p1, p2) self.dado.draw(v) self.dado.setFill('#FF0000') '''pintamos los circulos del dado con las posiciones reescalables de los puntos pos1 pos5 pos2 pos4 pos6 pos3 pos7 ''' self.pos1 = Point(self.xmin + w /2, self.ymin + h /2) self.pos2 = Point(self.xmin + w / 2, self.ymin + h) self.pos3 = Point(self.xmin + w / 2, self.ymin + h * 1.5) self.pos4 = Point(self.xmin + w, self.ymin + h) self.pos5 = Point(self.xmin + w * 1.5, self.ymin + h /2) self.pos6 = Point(self.xmin + w * 1.5, self.ymin + h) self.pos7 = Point(self.xmin + w * 1.5, self.ymin + h * 1.5) self.c1 = Circle(self.pos1, 4) self.c1.setOutline('#fff') self.c1.setFill('#fff') self.c1.draw(self.ventana) self.c2 = Circle(self.pos2, 4) self.c2.setOutline('#fff') self.c2.setFill('#fff') self.c2.draw(self.ventana) self.c3 = Circle(self.pos3, 4) self.c3.setOutline('#fff') self.c3.setFill('#fff') self.c3.draw(self.ventana) self.c4 = Circle(self.pos4, 4) self.c4.setOutline('#fff') self.c4.setFill('#fff') self.c4.draw(self.ventana) self.c5 = Circle(self.pos5, 4) self.c5.setOutline('#fff') self.c5.setFill('#fff') self.c5.draw(self.ventana) self.c6 = Circle(self.pos6, 4) self.c6.setOutline('#fff') self.c6.setFill('#fff') self.c6.draw(self.ventana) self.c7 = Circle(self.pos7, 4) self.c7.setOutline('#fff') self.c7.setFill('#fff') self.c7.draw(self.ventana) self.limpiar() def colorDado(self, c):#personalizar el dado self.dado.setFill(c) def colorPunto(self, c):#personalizar los puntos del dado self.c1.setOutline(c) self.c1.setFill(c) self.c2.setOutline(c) self.c2.setFill(c) self.c3.setOutline(c) self.c3.setFill(c) self.c4.setOutline(c) self.c4.setFill(c) self.c5.setOutline(c) self.c5.setFill(c) self.c6.setOutline(c) self.c6.setFill(c) self.c7.setOutline(c) self.c7.setFill(c) def pulsado(self, p):#funcion para saber si se ha pulsado if p.getX() in range(self.xmin, self.xmax) and p.getY() in range(self.ymin, self.ymax): return True else: return False def ponValor(self, valor=1):#funcion para establecer la cara visible del dado self.limpiar() if valor == 1: self.c4.draw(self.ventana) elif valor == 2: self.c1.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 3: self.c1.draw(self.ventana) self.c4.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 4: self.c1.draw(self.ventana) self.c3.draw(self.ventana) self.c5.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 5: self.c1.draw(self.ventana) self.c3.draw(self.ventana) self.c4.draw(self.ventana) self.c5.draw(self.ventana) self.c7.draw(self.ventana) elif valor == 6: self.c1.draw(self.ventana) self.c2.draw(self.ventana) self.c3.draw(self.ventana) self.c5.draw(self.ventana) self.c6.draw(self.ventana) self.c7.draw(self.ventana) def limpiar(self):#limpiar el dado antes de pintar self.c1.undraw() self.c2.undraw() self.c3.undraw() self.c4.undraw() self.c5.undraw() self.c6.undraw() self.c7.undraw() def tirarDado(self):#funcion para visualizar aleatoriamente una cara posibles = [1, 2, 3, 4, 5, 6] num = choice(posibles) self.ponValor(num) return num
num_snowballs = 75 for i in range(num_snowballs): x = randint(0, 199) y = randint(0, 199) p = Point(x, y) snowball = Circle(p, randint(2, 3)) snowball.setFill("white") snowball.draw(win) snowballs.append(snowball) # Freeze between frames in seconds animation_delay = 0.1 frames_to_render = 20 for i in range(frames_to_render): for snowball in snowballs: snowball.move(0, randint(1, 3)) if snowball.getCenter().getY() > 199: snowball.undraw() snowball.move(0, -200) snowball.draw(win) # time.sleep(animation_delay) label = Text(Point(99, 99), "Happy SNOW DAY!") label.setFill("white") label.setSize(18) label.draw(win) win.getMouse() win.close()
def drawCircle(): #draws the red circle radius = 5 circle1 = Circle(Point(25, 25), radius) circle1.setFill("red") circle1.draw(win) #good job text goodJob = Text(Point(25, 35), "Good Job!") goodJob.setFace("times roman") goodJob.draw(win) sleep(2) goodJob.undraw() #grow text grow = Text(Point(25,35), "Now we will grow our circle.") grow.setFace("times roman") grow.draw(win) sleep(1.5) grow.undraw() sleep(.5) #click on the window text clickToGrow = Text(Point(25,35), "Click on the window") clickToGrow.setFace("times roman") clickToGrow.draw(win) win.getMouse() circle1.undraw() clickToGrow.undraw() #grow your circle for i in range(10): radius = radius + 1 circle2 = Circle(Point(25, 25), radius) circle2.setFill("red") circle2.draw(win) circle2.undraw() circle2.draw(win) #WOW text for growing your circle wow = Text(Point(25, 43), "WOW!") wow.setSize(30) wow.setFace("times roman") wow.draw(win) sleep(2) wow.undraw() #Shrink the circle prompt thenText = Text(Point(25, 43), "Now, click to shrink it back down!") thenText.setFace("times roman") thenText.setSize(25) thenText.draw(win) win.getMouse() thenText.undraw() circle2.undraw() #Shrink your circle Function for i in range(10): radius = radius - 1 circle3 = Circle(Point(25, 25), radius) circle3.setFill("red") circle3.draw(win) circle3.undraw() circle3.draw(win) #small wow wow2 = Text(Point(25, 33), "wow") wow2.setFace("times roman") wow2.draw(win) sleep(.35) wow2.undraw() wow2 = Text(Point(25, 33), "wow.") wow2.setFace("times roman") wow2.draw(win) sleep(.5) wow2.undraw() wow2 = Text(Point(25, 33), "wow..") wow2.setFace("times roman") wow2.draw(win) sleep(.5) wow2.undraw() wow2 = Text(Point(25, 33), "wow...") wow2.setFace("times roman") wow2.draw(win) sleep(2) wow2.undraw() circle3.undraw() #call to next function moveCircle()
cir1.setFill('red') cir2.setFill('orange') cir3.setFill('yellow') cir4.setFill('green') cir5.setFill('blue') ones = [1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56] twos = [2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57] threes = [3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58] fours = [4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59] fives = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] while True: if int(strftime('%S')) in ones: cir1.undraw() cir1.draw(win) cir2.undraw() cir3.undraw() cir4.undraw() cir5.undraw() if int(strftime('%S')) in twos: cir2.draw(win) if int(strftime('%S')) in threes: cir3.draw(win) if int(strftime('%S')) in fours: cir4.draw(win)
class Robot: NORTE = 0 ESTE = 1 SUR = 2 OESTE = 3 def __init__(self, x, y): self.col = x self.row = y self.circle = Circle(Point(x * 50 + 25, y * 50 + 50 + 25), 20) self.facing = self.NORTE self.face = self._getFace() self.attached = False self.s = [False, False, False, False, False, False, False, False] self.x = [False, False, False, False] def move(self, x, y): self.col = x self.row = y self.circle.undraw() self.face.undraw() self.circle = Circle(Point(x * 50 + 25, y * 50 + 50 + 25), 20) self.face = self._getFace() def draw(self, win): self.circle.setFill(color_rgb(0, 0, 0)) self.face.setFill(color_rgb(0, 0, 0)) self.circle.undraw() self.face.undraw() self.circle.draw(win) self.face.draw(win) def undraw(self): self.circle.undraw() self.face.undraw() def getCol(self): return self.col def getRow(self): return self.row def detach(self): self.attached = False def actualizarPercepciones(self, perc): self.s = perc self.x[0] = perc[1] or perc[2] self.x[1] = perc[3] or perc[4] self.x[2] = perc[5] or perc[6] self.x[3] = perc[7] or perc[0] def desplaza(self, dir): x = self.col y = self.row self.facing = dir if (dir == self.NORTE): self.move(x, y - 1) elif (dir == self.ESTE): self.move(x + 1, y) elif (dir == self.SUR): self.move(x, y + 1) elif (dir == self.OESTE): self.move(x - 1, y) def razona(self): x = self.x s = self.s if (not (x[0] or x[1] or x[2] or x[3])): self.desplaza(self.NORTE) elif not self.attached: if x[0] and not s[3]: self.desplaza(self.ESTE) elif x[1] and not s[5]: self.desplaza(self.SUR) elif x[2] and not s[7]: self.desplaza(self.OESTE) elif x[3] and not s[1]: self.desplaza(self.NORTE) self.attached = True elif self.facing == self.SUR: if not s[3]: self.desplaza(self.ESTE) elif not s[5]: self.desplaza(self.SUR) elif not s[7]: self.desplaza(self.OESTE) elif not s[1]: self.desplaza(self.NORTE) elif self.facing == self.NORTE: if not s[7]: self.desplaza(self.OESTE) elif not s[1]: self.desplaza(self.NORTE) elif not s[3]: self.desplaza(self.ESTE) elif not s[5]: self.desplaza(self.SUR) elif self.facing == self.ESTE: if not s[1]: self.desplaza(self.NORTE) elif not s[3]: self.desplaza(self.ESTE) elif not s[5]: self.desplaza(self.SUR) elif not s[7]: self.desplaza(self.OESTE) elif self.facing == self.OESTE: if not s[5]: self.desplaza(self.SUR) elif not s[7]: self.desplaza(self.OESTE) elif not s[1]: self.desplaza(self.NORTE) elif not s[3]: self.desplaza(self.ESTE) def _getFace(self): x1 = 0 y1 = 0 x2 = 0 y2 = 0 if (self.facing == self.NORTE): x1 = self.col * 50 + 20 y1 = self.row * 50 + 50 x2 = x1 + 10 y2 = y1 + 10 elif (self.facing == self.ESTE): x1 = self.col * 50 + 40 y1 = self.row * 50 + 20 + 50 x2 = x1 + 10 y2 = y1 + 10 elif (self.facing == self.SUR): x1 = self.col * 50 + 20 y1 = self.row * 50 + 40 + 50 x2 = x1 + 10 y2 = y1 + 10 elif (self.facing == self.OESTE): x1 = self.col * 50 y1 = self.row * 50 + 20 + 50 x2 = x1 + 10 y2 = y1 + 10 return Rectangle(Point(x1, y1), Point(x2, y2))
class Fighter(): def __init__(self, team, points, win): self.win = win self.radius = 0.1 self.position = points[0] self.looking = points[1] self.fire_line = self.get_fire() self.color = "red" if team == 0 else "blue" self.view_left = self.get_left() self.view_left.setFill(self.color) self.view_right = self.get_right() self.view_right.setFill(self.color) self.firing = True self.view = False self.body = Circle(self.position, self.radius) self.body.setFill(self.color) def draw(self): self.body.draw(self.win) self.toggle_fire() self.toggle_view() def die(self): self.fire_line.undraw() self.view_left.undraw() self.view_right.undraw() self.body.setFill("orange") def undraw(self): self.body.undraw() def update(self, points): self.update_position(points[0]) self.update_view(points[1]) def update_position(self, position): self.body.move(position.x - self.position.x, position.y - self.position.y) self.position = position def update_view(self, looking): self.fire_line.undraw() self.view_left.undraw() self.view_right.undraw() self.looking = looking self.fire_line = self.get_fire() self.view_left = self.get_left() self.view_right = self.get_right() if self.firing: self.fire_line.draw(self.win) if self.looking: self.view_left.draw(self.win) self.view_right.draw(self.win) def toggle_fire(self): self.firing = not self.firing if self.firing: self.fire_line.draw(self.win) else: self.fire_line.undraw() def toggle_view(self): self.view = not self.view if self.view: self.view_left.draw(self.win) self.view_right.draw(self.win) else: self.view_left.undraw() self.view_right.undraw() def get_left(self): x = self.position.x + (self.looking.x - self.position.x) * math.cos( math.pi / 3) - (self.looking.y - self.position.y) * math.sin( math.pi / 3) y = self.position.y + (self.looking.x - self.position.x) * math.sin( math.pi / 3) + (self.looking.y - self.position.y) * math.cos( math.pi / 3) l = Line(self.position, Point(x, y)) l.setFill(self.color) return l def get_right(self): x = self.position.x + (self.looking.x - self.position.x) * math.cos( -math.pi / 3) - (self.looking.y - self.position.y) * math.sin( -math.pi / 3) y = self.position.y + (self.looking.x - self.position.x) * math.sin( -math.pi / 3) + (self.looking.y - self.position.y) * math.cos( -math.pi / 3) l = Line(self.position, Point(x, y)) l.setFill(self.color) return l def get_fire(self): l = Line(self.position, self.looking) l.setFill("orange") return l def __repr__(self): return "Fighter({}, {})".format(str(self.position), str(self.radius))