shh_algo_PY

Hit It Game

Jun 11th, 2022 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 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.     def move_up(self):
  21.         self.goto(self.xcor(), self.ycor() + self.step)
  22.     def move_down(self):
  23.         self.goto(self.xcor(), self.ycor() - self.step)
  24.     def move_left(self):
  25.         self.goto(self.xcor() - self.step, self.ycor())
  26.     def move_right(self):
  27.         self.goto(self.xcor() + self.step, self.ycor())
  28.  
  29.     # ADD THE COLLISION DEFINITION #
  30.  
  31.     def is_collide(self, sprite):
  32.         dist = self.distance(sprite.xcor(), sprite.ycor())
  33.         if dist < 30:
  34.             return True
  35.         else:
  36.             return False
  37.  
  38.     # ADD ENEMY MOVEMENT #
  39.  
  40.     def set_move(self, x_start, y_start, x_end, y_end):
  41.         self.x_start = x_start
  42.         self.y_start = y_start      
  43.         self.x_end = x_end
  44.         self.y_end = y_end
  45.         self.goto(x_start, y_start)
  46.         self.setheading(self.towards(x_end, y_end)) #direction
  47.  
  48.     def make_step(self):
  49.         self.forward(self.step)
  50.  
  51.         if self.distance(self.x_end, self.y_end) < self.step: #if distance less than half step
  52.             self.set_move(self.x_end, self.y_end, self.x_start, self.y_start) #change direction
  53.  
  54. player = Sprite(0, -100, 10, 'circle', 'orange')
  55.  
  56. enemy1 = Sprite(-s_width, 0, 15, 'square', 'red')
  57. enemy1.set_move(-s_width, 0, s_width, 0)
  58.  
  59. enemy2 = Sprite(s_width, 70, 15, 'square', 'red')
  60. enemy2.set_move(s_width, 70, -s_width, 70)
  61.  
  62. goal = Sprite(0, 120, 20, 'triangle', 'green')
  63. # goal.set_move(-s_width, 120, s_width, 0)  
  64.  
  65. total_score = 0
  66.  
  67. scr = player.getscreen()
  68.  
  69. scr.listen()
  70.  
  71. scr.onkey(player.move_up, 'Up')
  72. scr.onkey(player.move_left, 'Left')
  73. scr.onkey(player.move_right, 'Right')
  74. scr.onkey(player.move_down, 'Down')
  75.  
  76.  
  77. # ADD THE SCORE!
  78.  
  79. while total_score < 3:
  80.     enemy1.make_step()
  81.     enemy2.make_step()
  82.     # goal.make_step()
  83.     if player.is_collide(goal):
  84.         total_score += 1
  85.         player.goto(0, -100)
  86.     if player.is_collide(enemy1) or player.is_collide(enemy2):
  87.         goal.hideturtle()
  88.         break
  89.  
  90. if total_score == 3:
  91.     enemy1.hideturtle()
  92.     enemy2.hideturtle()
  93.  
Add Comment
Please, Sign In to add comment