Advertisement
shh_algo_PY

Richard

Oct 2nd, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. from turtle import *
  2. from random import randint
  3.  
  4. tsize = 20
  5. s_width = 200
  6. s_height = 180
  7.  
  8. class Sprite(Turtle):
  9.     def __init__(self, x, y, step=10, shape='circle', color='black'):
  10.         super().__init__()
  11.        
  12.         self.penup()
  13.         self.speed(0)
  14.         self.goto(x, y)
  15.         self.color(color)
  16.         self.shape(shape)
  17.         self.step = step
  18.         self.points = 0
  19.  
  20.     # KEYS USED TO MOVE
  21.     def move_up(self):
  22.         self.goto(self.xcor(), self.ycor() + self.step)
  23.     def move_down(self):
  24.         self.goto(self.xcor(), self.ycor() - self.step)
  25.     def move_left(self):
  26.         self.goto(self.xcor() - self.step, self.ycor())
  27.     def move_right(self):
  28.         self.goto(self.xcor() + self.step, self.ycor())
  29.  
  30.     # ADD THE COLLISION DEFINITION #
  31.  
  32.     def is_collide(self, sprite):
  33.         dist = self.distance(sprite.xcor(), sprite.ycor())
  34.         if dist < 30:
  35.             return True
  36.         else:
  37.             return False
  38.  
  39.     # ADD ENEMY MOVEMENT #
  40.  
  41.     def set_move(self, x_start, y_start, x_end, y_end):
  42.         self.x_start = x_start
  43.         self.y_start = y_start      
  44.         self.x_end = x_end
  45.         self.y_end = y_end
  46.         self.goto(x_start, y_start)
  47.         self.setheading(self.towards(x_end, y_end)) #direction
  48.    
  49.     # ENEMY MOVEMENT - STEPS #
  50.  
  51.     def make_step(self):
  52.         self.forward(self.step)
  53.  
  54.         if self.distance(self.x_end, self.y_end) < self.step:
  55.             self.set_move(self.x_end, self.y_end, self.x_start, self.y_start)
  56.            
  57. player = Sprite(0, -100, 10, 'circle', 'orange')
  58.  
  59. enemy1 = Sprite(-s_width, 0, 15, 'square', 'red')
  60. enemy1.set_move(-s_width, 0, s_width, 0)
  61.  
  62. enemy2 = Sprite(s_width, 70, 15, 'square', 'red')
  63. enemy2.set_move(s_width, 70, -s_width, 70)
  64.  
  65. goal = Sprite(0, 120, 20, 'triangle', 'green')
  66. # goal.set_move(-s_width, 120, s_width, 0)  
  67.  
  68. total_score = 0
  69.  
  70. scr = player.getscreen()
  71.  
  72. scr.listen()
  73.  
  74. scr.onkey(player.move_up, 'Up')
  75. scr.onkey(player.move_left, 'Left')
  76. scr.onkey(player.move_right, 'Right')
  77. scr.onkey(player.move_down, 'Down')
  78.  
  79. # ADD THE SCORE!
  80.  
  81. while total_score < 3:
  82.     enemy1.make_step()
  83.     enemy2.make_step()
  84.     # goal.make_step()
  85.  
  86.     if player.is_collide(goal):
  87.         total_score += 1
  88.         player.goto(0, -100)
  89.        
  90.     if player.is_collide(enemy1) or player.is_collide(enemy2):
  91.         goal.hideturtle()
  92.         break
  93.  
  94. if total_score == 3:
  95.     enemy1.hideturtle()
  96.     enemy2.hideturtle()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement