boris-vlasenko

pygame__2

May 18th, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Apr 20 08:16:32 2016
  4.  
  5. @author: User
  6. """
  7. import random
  8. import pygame
  9. from pygame.locals import *
  10. import sys
  11.  
  12. SIZE = (640, 760)
  13. WHITE = (255, 255, 255)
  14. RED = (255, 0, 0)
  15. BLUE = (40, 40, 200)
  16. BLACK = (0, 0, 0)
  17. TRANSPARENT = (0, 0, 0, 0)
  18. FPS = 600
  19. pygame.init()
  20. surface = pygame.display.set_mode(SIZE)
  21. screen = pygame.Surface(SIZE)
  22. clock = pygame.time.Clock()
  23.  
  24.  
  25. class Circle():
  26.    
  27.     def __init__(self, color, position, radius):
  28.         self.surface = pygame.Surface((2*radius, 2*radius)).convert_alpha()
  29.         self.surface.fill(TRANSPARENT)
  30.         self.circle = pygame.draw.circle(self.surface, color,
  31.                                          (radius, radius), radius)
  32.         self.x, self.y = position
  33.         self.radius = radius
  34.         self.attached = False
  35.         self.vx = 1.2
  36.         self.vy = 0.8
  37.    
  38.     def distanceTo(self,obj):
  39.         return ((obj.x-self.x)**2+(obj.y-self.y)**2)**0.5
  40.  
  41.     def move(self):
  42.         if not self.attached:
  43.             self.x += self.vx
  44.             self.y += self.vy
  45.            
  46.             if self.x > 500:
  47.                 self.x = 500
  48.                 self.vx = -self.vx
  49.  
  50.             if self.x < 40:
  51.                 self.x = 40
  52.                 self.vx = -self.vx
  53.  
  54.             if self.y < 40:
  55.                 self.y = 40
  56.                 self.vy = -self.vy
  57.                
  58.             if self.y > 620:
  59.                 self.y = 620
  60.                 self.vy = -self.vy
  61.        
  62.         for c in circles:
  63.             if c != self:
  64.                 if self.distanceTo(c) < self.radius + c.radius:
  65.                     self.vx = (self.x-c.x)/100
  66.                     self.vy = (self.y-c.y)/100
  67.                     c.vx = -self.vx
  68.                     c.vy = -self.vy
  69.  
  70.     def draw(self, surface):
  71.         self.rect = surface.blit(self.surface, (self.x, self.y))
  72.    
  73.     def point_in(self, point):
  74.         return self.rect.collidepoint(point)
  75.  
  76.     def change_color(self):
  77.         color = (int(random.random()*256),
  78.                  int(random.random()*256),
  79.                  int(random.random()*256))
  80.         self.circle = pygame.draw.circle(self.surface, color,
  81.                                         (self.radius, self.radius),
  82.                                          self.radius)                                      
  83.  
  84.  
  85.  
  86. class Platform:
  87.     def __init__(self):
  88.         self.img = pygame.image.load('e.jpg')      
  89.        
  90.        
  91. def make_level(level, platform):
  92.     a = 0
  93.     b = 0
  94.     for row in level:
  95.         for col in row:
  96.             if col == '-':
  97.                 screen.blit(platform.img, (a, b))  
  98.             a += 20
  99.         b += 20
  100.         a = 0
  101.  
  102. level = [
  103.     '-----------          -----------',
  104.     '-                              -',
  105.     '-                              -',
  106.     '-                              -',
  107.     '-                              -',
  108.     '-                              -',
  109.     '-                              -',
  110.     '-                              -',
  111.     '-                              -',
  112.     '-                              -',
  113.     '-                              -',
  114.     '-                              -',
  115.     '-                              -',
  116.     '-                              -',
  117.     '-                              -',
  118.     '-                              -',
  119.     '-                              -',
  120.     '-                              -',
  121.     '-                              -',
  122.     '-                              -',
  123.     '-                              -',
  124.     '-                              -',
  125.     '-                              -',
  126.     '-                              -',
  127.     '-                              -',
  128.     '-                              -',
  129.     '-                              -',
  130.     '-                              -',
  131.     '-                              -',
  132.     '-                              -',
  133.     '-                              -',
  134.     '-                              -',
  135.     '-                              -',
  136.     '-                              -',
  137.     '-                              -',
  138.     '-                              -',
  139.     '-                              -',
  140.     '-----------          -----------']
  141.  
  142.  
  143. circles = [Circle(WHITE, (300, 600), 50),Circle(BLUE, (320, 380), 40),Circle(RED, (320, 100), 50)]
  144. circles.append(Circle(WHITE, (200, 600), 20))
  145. circles.append(Circle(WHITE, (300, 500), 20))
  146.  
  147.  
  148. pl = Platform()
  149.  
  150. while True:
  151.     for event in pygame.event.get():
  152.         if event.type == QUIT:
  153.             pygame.quit()
  154.             sys.exit()
  155.  
  156.         elif event.type == KEYDOWN and event.key == K_SPACE:  
  157.             for c in circles:
  158.                 c.vx = 0
  159.                 c.vy = 0
  160.            
  161.         elif event.type == MOUSEBUTTONDOWN:
  162.             for c in circles:
  163.                 if c.point_in(event.pos):
  164.                     c.attached = True
  165.                
  166.         elif event.type == MOUSEBUTTONUP:
  167.             for c in circles:
  168.                 c.attached = False
  169.            
  170.         elif event.type == MOUSEMOTION:
  171.             for c in circles:
  172.                 if c.attached:
  173.                     c.vx,c.vy = event.rel
  174.    
  175.  
  176.     surface.fill(BLACK)
  177.     make_level(level, pl)
  178.     surface.blit(screen,(0, 0))
  179.  
  180.     for c in circles:
  181.         c.move()
  182.         c.draw(surface)
  183.        
  184.     pygame.display.update()
  185.     clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment