boris-vlasenko

pygame__

May 18th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 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.         print(self.vx)
  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 self.distanceTo(c) < self.radius + c.radius:
  64.                 self.vx = (self.x-c.x)/50
  65.                 self.vy = (self.y-c.y)/50
  66.                 c.vx = -self.vx
  67.                 c.vy = -self.vy
  68.  
  69.     def draw(self, surface):
  70.         self.rect = surface.blit(self.surface, (self.x, self.y))
  71.    
  72.     def point_in(self, point):
  73.         return self.rect.collidepoint(point)
  74. #        self.rect.collide..
  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 Intersect(x1, x2, y1, y2):
  92.     if (x1 > x2-20) and (x1 < x2+20) and (y1 > y2-20) and (y1 < y2+20):
  93.         return 1
  94.     else:
  95.         return 0
  96.        
  97.        
  98. def make_level(level, platform):
  99.     a = 0
  100.     b = 0
  101.     for row in level:
  102.         for col in row:
  103.             if col == '-':
  104.                 screen.blit(platform.img, (a, b))  
  105.             a += 20
  106.         b += 20
  107.         a = 0
  108.  
  109. level = [
  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.     '-                              -',
  147.     '-----------          -----------']
  148.  
  149. circles = [Circle(BLUE, (320, 380), 40),Circle(RED, (320, 100), 50)]
  150. circle = Circle(WHITE, (300, 600), 50)
  151.  
  152. pl = Platform()
  153. circle.up = True
  154. circle.dn = True
  155.  
  156. while True:
  157.     for event in pygame.event.get():
  158.         if event.type == QUIT:
  159.             pygame.quit()
  160.             sys.exit()
  161.  
  162.         elif event.type == KEYDOWN and event.key == K_SPACE:  
  163.             circle.vx = 0
  164.             circle.vy = 0
  165.            
  166.         elif event.type == MOUSEBUTTONDOWN:
  167.             if circles[0].point_in(event.pos):
  168.                 circles[0].change_color()
  169.             if circle.point_in(event.pos):
  170.                 circle.attached = True
  171.                
  172.         elif event.type == MOUSEBUTTONUP:
  173.                 circle.attached = False
  174.         elif event.type == MOUSEMOTION:
  175.             if circle.attached:
  176.                 circle.vx,circle.vy = event.rel
  177.                 circle.move()
  178.    
  179.  
  180.     if not circle.attached:
  181.         circle.move()
  182.        
  183.        
  184.     surface.fill(BLACK)
  185.     make_level(level, pl)
  186.     surface.blit(screen,(0, 0))
  187.     circle.draw(surface)
  188.     for c in circles:
  189.         c.move()
  190.         c.draw(surface)
  191.        
  192.     pygame.display.update()
  193.     clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment