overactive

Untitled

Aug 5th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import pygame
  2. import time
  3. import sys
  4. import random
  5.  
  6. WIDTH, HEIGHT = 1000, 600
  7. WHITE = (255, 255, 255)
  8. BLUE = (0, 100, 200)
  9. BLACK = (0, 0, 0)
  10. PINK = (255, 0, 120)
  11.  
  12. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  13. pygame.display.set_caption('Balls')
  14. clock = pygame.time.Clock()
  15. pygame.init()
  16.  
  17. def quitGame():
  18.     pygame.quit()
  19.     sys.exit(0)
  20.  
  21. def randomizeBall():
  22.         x = random.randint(60, WIDTH - 60)
  23.         y = random.randint(60, HEIGHT - 60)
  24.         w = random.randint(MIN_BALL_WIDTH, MAX_BALL_WIDTH)
  25.         xspeed = yspeed =  random.choice(SPEED)
  26.         fun = random.choice(FUN)
  27.        
  28.         return x, y, w, xspeed, yspeed, fun
  29.  
  30. class Ball():
  31.     def __init__(self, x, y, w, xspeed, yspeed, fun):
  32.         self.x = x
  33.         self.y = y
  34.         self.w = w
  35.         self.xspeed = xspeed
  36.         self.yspeed = yspeed
  37.         self.fun = fun
  38.         self.rec = pygame.Rect(self.x, self.y, self.w, self.w)
  39.  
  40.     def update(self, borders):
  41.         self.rec.x += self.xspeed
  42.         self.rec.y += self.yspeed
  43.        
  44.         for border, value in borders.items():
  45.             if self.rec.colliderect(value.rec):
  46.                 if border == 'left' or border == 'right':
  47.                     self.xspeed *= -1
  48.                     self.xspeed += random.randint(-self.fun, self.fun)
  49.                 if border == 'up' or border == 'down':
  50.                     self.yspeed *= -1
  51.                     self.xspeed += random.randint(-self.fun, self.fun)
  52.  
  53.     def show(self):
  54.         pygame.draw.rect(canvas, WHITE, self.rec, 0)
  55.  
  56.     def highlight(self, ball):
  57.         pygame.draw.rect(canvas, PINK, ball, 0)
  58.  
  59. class Border():
  60.     def __init__(self, x, y, w, h):
  61.         self.x = x
  62.         self.y = y
  63.         self.w = w
  64.         self.h = h
  65.         self.rec = pygame.Rect(self.x, self.y, self.w, self.h)
  66.  
  67.     def show(self):
  68.         pygame.draw.rect(canvas, WHITE, self.rec, 0)
  69.  
  70. ### GAME SETTINGS
  71. OFFSET = 50
  72. BORDER_WIDTH = 10
  73.  
  74. BALL_COLOR = WHITE
  75. BACKGROUND_COLOR = BLACK
  76. HIGHLIGHT_COLOR = PINK
  77.  
  78. SPEED = list(range(-2, 2))
  79. SPEED.remove(0)
  80. FUN =  list(range(0, 2))
  81.  
  82. MIN_BALL_WIDTH = 30
  83. MAX_BALL_WIDTH = 50
  84.  
  85. BALL_DENSITY = random.randint(5, 20)
  86. ###
  87.  
  88. ballPool = []
  89. borders = {'left': Border(OFFSET, OFFSET, BORDER_WIDTH, HEIGHT - 2*OFFSET), 'right': Border(WIDTH - OFFSET, OFFSET, BORDER_WIDTH, HEIGHT - 2*OFFSET),
  90.             'up': Border(OFFSET, OFFSET, WIDTH - 2*OFFSET + BORDER_WIDTH, BORDER_WIDTH), 'down': Border(OFFSET, HEIGHT - OFFSET, WIDTH - 2*OFFSET + BORDER_WIDTH, BORDER_WIDTH)}
  91.  
  92. for i in range(BALL_DENSITY):
  93.     x, y, w, xspeed, yspeed, fun = randomizeBall()
  94.     ballPool.append(Ball(x, y, w, xspeed, yspeed, fun))
  95.  
  96. while True:
  97.     for event in pygame.event.get():
  98.         if event.type == pygame.QUIT:
  99.             quitGame()
  100.     canvas.fill(BLACK)
  101.    
  102.     for border, value in borders.items():
  103.         value.show()
  104.  
  105.     ballRect = []
  106.     for ball in ballPool:
  107.         ball.update(borders)
  108.         ball.show()
  109.         ballRect.append(ball.rec)
  110.    
  111.     for rectBall in ballRect:
  112.         ballRect.remove(rectBall)
  113.         collision = rectBall.collidelist(ballRect)
  114.         if collision != -1:
  115.             ball.highlight(rectBall)
  116.             ball.highlight(ballRect[collision])
  117.  
  118.     pygame.display.flip()  
  119.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment