boris-vlasenko

pygame__3

May 18th, 2016
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 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.         self.friction = 0.99
  38.    
  39.     def distanceTo(self,obj):
  40.         return ((obj.x-self.x)**2+(obj.y-self.y)**2)**0.5
  41.  
  42.     def move(self):
  43.         if not self.attached:
  44.             self.vx *= self.friction
  45.             self.vy *= self.friction
  46.             self.x += self.vx
  47.             self.y += self.vy
  48.            
  49.             if self.x > 500:
  50.                 self.x = 500
  51.                 self.vx = -self.vx
  52.  
  53.             if self.x < 40:
  54.                 self.x = 40
  55.                 self.vx = -self.vx
  56.  
  57.             if self.y < 40:
  58.                 self.y = 40
  59.                 self.vy = -self.vy
  60.                
  61.             if self.y > 620:
  62.                 self.y = 620
  63.                 self.vy = -self.vy
  64.        
  65.         for c in circles:
  66.             if c != self:
  67.                 if self.distanceTo(c) < self.radius + c.radius:
  68.                     self.vx += (self.x-c.x)/100
  69.                     self.vy += (self.y-c.y)/100
  70.                     #~ c.vx += -self.vx
  71.                     #~ c.vy += -self.vy
  72.  
  73.     def draw(self, surface):
  74.         self.rect = surface.blit(self.surface, (self.x, self.y))
  75.    
  76.     def point_in(self, point):
  77.         return self.rect.collidepoint(point)
  78.  
  79.     def change_color(self):
  80.         color = (int(random.random()*256),
  81.                  int(random.random()*256),
  82.                  int(random.random()*256))
  83.         self.circle = pygame.draw.circle(self.surface, color,
  84.                                         (self.radius, self.radius),
  85.                                          self.radius)                                      
  86.  
  87.  
  88.  
  89. class Platform:
  90.     def __init__(self):
  91.         self.img = pygame.image.load('e.jpg')      
  92.        
  93.        
  94. def make_level(level, platform):
  95.     a = 0
  96.     b = 0
  97.     for row in level:
  98.         for col in row:
  99.             if col == '-':
  100.                 screen.blit(platform.img, (a, b))  
  101.             a += 20
  102.         b += 20
  103.         a = 0
  104.  
  105. level = [
  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.     '-----------          -----------']
  144.  
  145.  
  146. circles = [Circle(WHITE, (300, 600), 50),Circle(BLUE, (320, 380), 40),Circle(RED, (320, 100), 50)]
  147. circles.append(Circle(WHITE, (200, 600), 20))
  148. circles.append(Circle(WHITE, (300, 500), 20))
  149. circles[0].friction = 1
  150.  
  151. pl = Platform()
  152.  
  153. while True:
  154.     for event in pygame.event.get():
  155.         if event.type == QUIT:
  156.             pygame.quit()
  157.             sys.exit()
  158.  
  159.         elif event.type == KEYDOWN and event.key == K_SPACE:  
  160.             for c in circles:
  161.                 c.vx = 0
  162.                 c.vy = 0
  163.            
  164.         elif event.type == MOUSEBUTTONDOWN:
  165.             for c in circles:
  166.                 if c.point_in(event.pos):
  167.                     c.attached = True
  168.                
  169.         elif event.type == MOUSEBUTTONUP:
  170.             for c in circles:
  171.                 c.attached = False
  172.            
  173.         elif event.type == MOUSEMOTION:
  174.             for c in circles:
  175.                 if c.attached:
  176.                     c.vx,c.vy = event.rel
  177.                     c.x = event.pos[0]-c.radius
  178.                     c.y = event.pos[1]-c.radius
  179.    
  180.  
  181.     surface.fill(BLACK)
  182.     make_level(level, pl)
  183.     surface.blit(screen,(0, 0))
  184.  
  185.     for c in circles:
  186.         c.move()
  187.         c.draw(surface)
  188.        
  189.     pygame.display.update()
  190.     clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment