Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- class pong:
- def __init__(self):
- self._running = True
- self._display_surf = None
- self.size = self.width, self.height = 640, 400
- self.cursor_width = 30
- self.cursor_height = 70
- self.pos_user_cursor = self.height/2 - self.cursor_height/2
- self.pos_ai_cursor = self.height/2 - self.cursor_height/2
- self.pos_ball_x = self.width/2
- self.pos_ball_y = self.height/2
- self.ray_ball_x = self.pos_ball_x
- self.ray_ball_y = self.pos_ball_y
- self.ball_speed_x = -2
- self.ball_speed_y = -2
- self.ray_speed_x = self.ball_speed_x
- self.ray_speed_y = self.ball_speed_y
- self.radius_ball = 10
- self.points_user = 0
- self.points_ai = 0
- self.key_z_pressed = False
- self.key_s_pressed = False
- self.clock = pygame.time.Clock()
- def on_init(self):
- pygame.init()
- self.score_font = pygame.font.SysFont("monospace", 15)
- self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
- self._running = True
- def on_event(self, event):
- if event.type == pygame.QUIT:
- self._running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_z:
- self.key_z_pressed = True
- if event.key == pygame.K_s:
- self.key_s_pressed = True
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_z:
- self.key_z_pressed = False
- if event.key == pygame.K_s:
- self.key_s_pressed = False
- def raytracing(self):
- self.ray_ball_x = self.pos_ball_x
- self.ray_ball_y = self.pos_ball_y
- self.ray_speed_x = self.ball_speed_x
- self.ray_speed_y = self.ball_speed_y
- if self.ball_speed_x > 0:
- while int(self.ray_ball_x) + self.radius_ball < self.width - self.cursor_width:
- self.ray_ball_x += self.ray_speed_x
- self.ray_ball_y += self.ray_speed_y
- if int(self.ray_ball_y) + self.radius_ball > self.height:
- self.ray_speed_y = self.ray_speed_y * -1
- if int(self.ray_ball_y) - self.radius_ball < 0:
- self.ray_speed_y = self.ray_speed_y * -1
- self.pos_ai_cursor = self.ray_ball_y
- def on_loop(self):
- self._display_surf.fill((0,0,0))
- if self.key_z_pressed:
- self.pos_user_cursor = self.pos_user_cursor-5
- if self.pos_user_cursor < 0:
- self.pos_user_cursor = 0
- if self.key_s_pressed:
- self.pos_user_cursor = self.pos_user_cursor+5
- if self.pos_user_cursor > self.height - self.cursor_height:
- self.pos_user_cursor = self.height - self.cursor_height
- self.raytracing()
- self.pos_ball_x += self.ball_speed_x
- self.pos_ball_y += self.ball_speed_y
- if int(self.pos_ball_x) - self.radius_ball < 0:
- self.pos_ball_x = self.width/2
- self.pos_ball_y = self.height/2
- self.points_ai += 1;
- if int(self.pos_ball_x) + self.radius_ball > self.width:
- self.pos_ball_x = self.width/2
- self.pos_ball_y = self.height/2
- self.points_user += 1;
- if int(self.pos_ball_y) + self.radius_ball > self.height:
- self.ball_speed_y = self.ball_speed_y * -1
- if int(self.pos_ball_y) - self.radius_ball < 0:
- self.ball_speed_y = self.ball_speed_y * -1
- 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)):
- self.ball_speed_x = self.ball_speed_x * -1
- self.ball_speed_y = self.ball_speed_y * -1
- 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)):
- self.ball_speed_x = self.ball_speed_x * -1
- self.ball_speed_y = self.ball_speed_y * -1
- pygame.draw.circle(self._display_surf, (255,255,255), (int(self.pos_ball_x), int(self.pos_ball_y)), self.radius_ball, 0)
- 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)
- pygame.draw.rect(self._display_surf, (255,255,255), (0,self.pos_user_cursor,self.cursor_width,self.cursor_height), 0)
- def on_render(self):
- self.clock.tick(60)
- self._display_surf.blit(self.score_font.render("User score: "+str(self.points_user), 1, (255,255,255)), (50, 20))
- self._display_surf.blit(self.score_font.render("AI score: "+str(self.points_ai), 1, (255,255,255)), (self.width - 150, 20))
- pygame.display.flip()
- pygame.display.update()
- def on_cleanup(self):
- pygame.quit()
- def on_execute(self):
- if self.on_init() == False:
- self._running = False
- while( self._running ):
- for event in pygame.event.get():
- self.on_event(event)
- self.on_loop()
- self.on_render()
- self.on_cleanup()
- if __name__ == "__main__" :
- pongInstance = pong()
- pongInstance.on_execute()
Add Comment
Please, Sign In to add comment