Advertisement
furas

Python - Turtle - Pong - (Stackoverflow)

Jun 27th, 2024
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. # date: 2024.06.28
  5. # [Second instance of a class suppresses/deletes the first one in my Python program - Stack Overflow](https://stackoverflow.com/questions/78677249/second-instance-of-a-class-suppresses-deletes-the-first-one-in-my-python-program?noredirect=1#comment138718516_78677249)
  6. """
  7.  
  8. from turtle import Turtle
  9. from turtle import Screen
  10.  
  11. # --- constants ---  # PEP8: `UPPER_CASE_NAMES`
  12.  
  13. # BOX = 30
  14.  
  15. # --- classes ---  
  16.  
  17. class Paddle:
  18.  
  19.     def __init__(self, side, key_left, key_right, x, color):
  20.    
  21.         self.key_left = key_left
  22.         self.key_right = key_right
  23.  
  24.         self.paddle = Turtle(shape="square")
  25.         self.paddle.color(color)
  26.         self.paddle.penup()
  27.         self.paddle.setx(x)
  28.         self.paddle.sety(0)
  29.  
  30.         self.pressed_up = False
  31.         self.pressed_down = False
  32.        
  33.         self.paddle.screen.onkeypress(key=self.key_left,  fun=self.move_up)
  34.         self.paddle.screen.onkeypress(key=self.key_right, fun=self.move_down)
  35.  
  36.         self.paddle.screen.onkeyrelease(key=self.key_left,  fun=self.move_up_stop)
  37.         self.paddle.screen.onkeyrelease(key=self.key_right, fun=self.move_down_stop)
  38.  
  39.         half_height = screen.canvheight // 2
  40.        
  41.         self.range_top    =  half_height - 10
  42.         self.range_bottom = -half_height + 10
  43.         #print('[DEBUG]', self.range_top, self.range_bottom)
  44.  
  45.         self.speed = 0
  46.        
  47.     def update(self):
  48.        
  49.         if self.pressed_up and self.pressed_down:  # both keys are presse then don't move
  50.             self.speed = 0
  51.         elif self.pressed_up:    # only one is pressed
  52.             self.speed = 5  
  53.         elif self.pressed_down:  # only one is pressed
  54.             self.speed = -5
  55.         else:  # both keys are unpressed
  56.             self.speed = 0
  57.    
  58.         if self.speed != 0:
  59.             current_y = self.paddle.ycor()
  60.             new_y = current_y + self.speed
  61.            
  62.             if self.range_top > new_y > self.range_bottom:
  63.                 self.paddle.sety(new_y)
  64.  
  65.     # I don't do calculation here (and don't execute `update()`
  66.     # because key repeates presses slow and it slows all movement
  67.     def move_up(self):
  68.         self.pressed_up = True
  69.  
  70.     def move_down(self):
  71.         self.pressed_down = True
  72.  
  73.     def move_up_stop(self):
  74.         self.pressed_up = False
  75.  
  76.     def move_down_stop(self):
  77.         self.pressed_down = False
  78.  
  79. class Ball:
  80.  
  81.     def __init__(self, x=0, y=0, color='black'):
  82.         self.ball = Turtle(shape="square")
  83.         self.ball.color(color)
  84.         self.ball.penup()
  85.         self.ball.setx(x)
  86.         self.ball.sety(y)
  87.         self.speed_x = 3
  88.         self.speed_y = 3
  89.        
  90.         half_width = screen.canvwidth // 2
  91.         half_height = screen.canvheight // 2
  92.        
  93.         self.range_top    =  half_height - 10
  94.         self.range_bottom = -half_height + 10
  95.         self.range_left   =  half_width - 10
  96.         self.range_right  = -half_width + 10
  97.        
  98.     def update(self):
  99.         current_x = self.ball.xcor()
  100.         current_y = self.ball.ycor()
  101.         new_x = current_x + self.speed_x
  102.         new_y = current_y + self.speed_y
  103.        
  104.         if new_x < self.range_right or new_x > self.range_left:
  105.             self.speed_x = -self.speed_x
  106.         if new_y > self.range_top   or new_y < self.range_bottom:
  107.             self.speed_y = -self.speed_y
  108.        
  109.         self.ball.setx(new_x)
  110.         self.ball.sety(new_y)
  111.        
  112. # --- functions ----
  113.  
  114. def mainloop():
  115.     left_pad.update()
  116.     right_pad.update()
  117.     ball.update()
  118.    
  119.     screen.update()
  120.  
  121.     # update after ? ms
  122.     screen.ontimer(mainloop, 5)
  123.  
  124. # --- main ---
  125.  
  126. screen = Screen()
  127. screen.bgcolor("beige")
  128. screen.title("Pong!")
  129. screen.setup(width=820, height=620)
  130. screen.screensize(canvwidth=800, canvheight=600)
  131. screen.tracer(0)
  132. screen.listen()
  133.  
  134. half_width = screen.canvwidth // 2
  135.  
  136. x = -half_width + 20
  137. left_pad  = Paddle("left", "w", "s", x, 'red')
  138.  
  139. x =  half_width - 20
  140. right_pad = Paddle("right", "Up", "Down", x, 'green')
  141.  
  142. ball = Ball()
  143.  
  144. mainloop()
  145.  
  146. screen.exitonclick()
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement