overactive

Untitled

Aug 4th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 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. class Border():
  57.     def __init__(self, x, y, w, h):
  58.         self.x = x
  59.         self.y = y
  60.         self.w = w
  61.         self.h = h
  62.         self.rec = pygame.Rect(self.x, self.y, self.w, self.h)
  63.  
  64.     def show(self):
  65.         pygame.draw.rect(canvas, WHITE, self.rec, 0)
  66.  
  67. ### GAME SETTINGS
  68. OFFSET = 50
  69. BORDER_WIDTH = 10
  70.  
  71. BALL_COLOR = WHITE
  72. BACKGROUND_COLOR = BLACK
  73. HIGHLIGHT_COLOR = PINK
  74.  
  75. SPEED = list(range(-5, 5))
  76. SPEED.remove(0)
  77. FUN =  list(range(0, 5))
  78.  
  79. MIN_BALL_WIDTH = 20
  80. MAX_BALL_WIDTH = 40
  81.  
  82. BALL_DENSITY = 2 #random.randint(5, 20)
  83. ###
  84.  
  85. ballPool = []
  86. borders = {'left': Border(OFFSET, OFFSET, BORDER_WIDTH, HEIGHT - 2*OFFSET), 'right': Border(WIDTH - OFFSET, OFFSET, BORDER_WIDTH, HEIGHT - 2*OFFSET),
  87.             'up': Border(OFFSET, OFFSET, WIDTH - 2*OFFSET + BORDER_WIDTH, BORDER_WIDTH), 'down': Border(OFFSET, HEIGHT - OFFSET, WIDTH - 2*OFFSET + BORDER_WIDTH, BORDER_WIDTH)}
  88.  
  89. for i in range(BALL_DENSITY):
  90.     x, y, w, xspeed, yspeed, fun = randomizeBall()
  91.     ballPool.append(Ball(x, y, w, xspeed, yspeed, fun))
  92. ballRect = []
  93. counter = 1
  94. while True:
  95.     for event in pygame.event.get():
  96.         if event.type == pygame.QUIT:
  97.             quitGame()
  98.  
  99.     canvas.fill(BLACK)
  100.    
  101.     for border, value in borders.items():
  102.         value.show()
  103.    
  104.     for ball in ballPool:
  105.         ball.update(borders)
  106.         ball.show()
  107.         ballRect.append(ball.rec)
  108.     print(ballRect)
  109.  
  110.     for rect in ballRect:
  111.         ballRect.remove(rect)
  112.         col = rect.collidelist(ballRect)
  113.         print(ballRect, len(ballRect))
  114.         print('collision:', col)
  115.         print(rect, ballRect[col])
  116.    
  117.  
  118.  
  119.  
  120.  
  121.  
  122.     pygame.display.flip()  
  123.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment