Guest User

Untitled

a guest
Jun 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.51 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. class pong:
  5.     def __init__(self):
  6.         self._running = True
  7.         self._display_surf = None
  8.         self.size = self.width, self.height = 640, 400
  9.         self.cursor_width = 30
  10.         self.cursor_height = 70
  11.         self.pos_user_cursor = self.height/2 - self.cursor_height/2
  12.         self.pos_ai_cursor = self.height/2 - self.cursor_height/2
  13.         self.pos_ball_x = self.width/2
  14.         self.pos_ball_y = self.height/2
  15.         self.ray_ball_x = self.pos_ball_x
  16.         self.ray_ball_y = self.pos_ball_y
  17.         self.ball_speed_x = -2
  18.         self.ball_speed_y = -2
  19.         self.ray_speed_x = self.ball_speed_x
  20.         self.ray_speed_y = self.ball_speed_y
  21.         self.radius_ball = 10
  22.         self.points_user = 0
  23.         self.points_ai = 0
  24.         self.key_z_pressed = False
  25.         self.key_s_pressed = False
  26.         self.clock = pygame.time.Clock()
  27.  
  28.     def on_init(self):
  29.         pygame.init()
  30.         self.score_font = pygame.font.SysFont("monospace", 15)
  31.         self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
  32.         self._running = True
  33.  
  34.     def on_event(self, event):
  35.         if event.type == pygame.QUIT:
  36.             self._running = False
  37.         if event.type == pygame.KEYDOWN:
  38.             if event.key == pygame.K_z:
  39.                 self.key_z_pressed = True
  40.             if event.key == pygame.K_s:
  41.                 self.key_s_pressed = True
  42.         if event.type == pygame.KEYUP:
  43.             if event.key == pygame.K_z:
  44.                 self.key_z_pressed = False
  45.             if event.key == pygame.K_s:
  46.                 self.key_s_pressed = False
  47.  
  48.     def raytracing(self):
  49.         self.ray_ball_x = self.pos_ball_x
  50.         self.ray_ball_y = self.pos_ball_y
  51.         self.ray_speed_x = self.ball_speed_x
  52.         self.ray_speed_y = self.ball_speed_y
  53.        
  54.         if self.ball_speed_x > 0:
  55.             while int(self.ray_ball_x) + self.radius_ball < self.width - self.cursor_width:
  56.                 self.ray_ball_x += self.ray_speed_x
  57.                 self.ray_ball_y += self.ray_speed_y
  58.                 if int(self.ray_ball_y) + self.radius_ball > self.height:
  59.                     self.ray_speed_y = self.ray_speed_y * -1
  60.                 if int(self.ray_ball_y) - self.radius_ball < 0:
  61.                     self.ray_speed_y = self.ray_speed_y * -1
  62.  
  63.             self.pos_ai_cursor = self.ray_ball_y
  64.  
  65.  
  66.     def on_loop(self):
  67.         self._display_surf.fill((0,0,0))
  68.  
  69.         if self.key_z_pressed:
  70.             self.pos_user_cursor = self.pos_user_cursor-5
  71.             if self.pos_user_cursor < 0:
  72.                 self.pos_user_cursor = 0
  73.  
  74.         if self.key_s_pressed:
  75.             self.pos_user_cursor = self.pos_user_cursor+5
  76.             if self.pos_user_cursor > self.height - self.cursor_height:
  77.                 self.pos_user_cursor = self.height - self.cursor_height
  78.  
  79.         self.raytracing()
  80.  
  81.         self.pos_ball_x += self.ball_speed_x
  82.         self.pos_ball_y += self.ball_speed_y
  83.         if int(self.pos_ball_x) - self.radius_ball < 0:
  84.             self.pos_ball_x = self.width/2
  85.             self.pos_ball_y = self.height/2
  86.             self.points_ai += 1;
  87.         if int(self.pos_ball_x) + self.radius_ball > self.width:
  88.             self.pos_ball_x = self.width/2
  89.             self.pos_ball_y = self.height/2
  90.             self.points_user += 1;
  91.         if int(self.pos_ball_y) + self.radius_ball > self.height:
  92.             self.ball_speed_y = self.ball_speed_y * -1
  93.         if int(self.pos_ball_y) - self.radius_ball < 0:
  94.             self.ball_speed_y = self.ball_speed_y * -1
  95.         if (int(self.pos_ball_x) - self.radius_ball < self.cursor_width) and ((int(self.pos_ball_y) + self.radius_ball > self.pos_user_cursor) and (int(self.pos_ball_y) - self.radius_ball < self.pos_user_cursor + self.cursor_height)):
  96.             self.ball_speed_x = self.ball_speed_x * -1
  97.             self.ball_speed_y = self.ball_speed_y * -1
  98.         if (int(self.pos_ball_x) + self.radius_ball > self.width - self.cursor_width) and ((int(self.pos_ball_y) + self.radius_ball > self.pos_ai_cursor) and (int(self.pos_ball_y) - self.radius_ball < self.pos_ai_cursor + self.cursor_height)):
  99.             self.ball_speed_x = self.ball_speed_x * -1
  100.             self.ball_speed_y = self.ball_speed_y * -1
  101.        
  102.         pygame.draw.circle(self._display_surf, (255,255,255), (int(self.pos_ball_x), int(self.pos_ball_y)), self.radius_ball, 0)
  103.         pygame.draw.rect(self._display_surf, (255,255,255), (self.width - self.cursor_width,self.pos_ai_cursor,self.cursor_width,self.cursor_height), 0)
  104.         pygame.draw.rect(self._display_surf, (255,255,255), (0,self.pos_user_cursor,self.cursor_width,self.cursor_height), 0)
  105.  
  106.     def on_render(self):
  107.         self.clock.tick(60)
  108.         self._display_surf.blit(self.score_font.render("User score: "+str(self.points_user), 1, (255,255,255)), (50, 20))
  109.         self._display_surf.blit(self.score_font.render("AI score: "+str(self.points_ai), 1, (255,255,255)), (self.width - 150, 20))
  110.         pygame.display.flip()
  111.         pygame.display.update()
  112.  
  113.     def on_cleanup(self):
  114.         pygame.quit()
  115.  
  116.     def on_execute(self):
  117.         if self.on_init() == False:
  118.             self._running = False
  119.  
  120.         while( self._running ):
  121.             for event in pygame.event.get():
  122.                 self.on_event(event)
  123.             self.on_loop()
  124.             self.on_render()
  125.         self.on_cleanup()
  126.  
  127. if __name__ == "__main__" :
  128.     pongInstance = pong()
  129.     pongInstance.on_execute()
Add Comment
Please, Sign In to add comment