Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Wed Apr 20 08:16:32 2016
- @author: User
- """
- import random
- import pygame
- from pygame.locals import *
- import sys
- SIZE = (640, 760)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- BLUE = (40, 40, 200)
- BLACK = (0, 0, 0)
- TRANSPARENT = (0, 0, 0, 0)
- FPS = 600
- pygame.init()
- surface = pygame.display.set_mode(SIZE)
- screen = pygame.Surface(SIZE)
- clock = pygame.time.Clock()
- class Circle():
- def __init__(self, color, position, radius):
- self.surface = pygame.Surface((2*radius, 2*radius)).convert_alpha()
- self.surface.fill(TRANSPARENT)
- self.circle = pygame.draw.circle(self.surface, color,
- (radius, radius), radius)
- self.x, self.y = position
- self.radius = radius
- self.attached = False
- self.vx = 1.2
- self.vy = 0.8
- def distanceTo(self,obj):
- return ((obj.x-self.x)**2+(obj.y-self.y)**2)**0.5
- def move(self):
- print(self.vx)
- self.x += self.vx
- self.y += self.vy
- if self.x > 500:
- self.x = 500
- self.vx = -self.vx
- if self.x < 40:
- self.x = 40
- self.vx = -self.vx
- if self.y < 40:
- self.y = 40
- self.vy = -self.vy
- if self.y > 620:
- self.y = 620
- self.vy = -self.vy
- for c in circles:
- if self.distanceTo(c) < self.radius + c.radius:
- self.vx = (self.x-c.x)/50
- self.vy = (self.y-c.y)/50
- c.vx = -self.vx
- c.vy = -self.vy
- def draw(self, surface):
- self.rect = surface.blit(self.surface, (self.x, self.y))
- def point_in(self, point):
- return self.rect.collidepoint(point)
- # self.rect.collide..
- def change_color(self):
- color = (int(random.random()*256),
- int(random.random()*256),
- int(random.random()*256))
- self.circle = pygame.draw.circle(self.surface, color,
- (self.radius, self.radius),
- self.radius)
- class Platform:
- def __init__(self):
- self.img = pygame.image.load('e.jpg')
- def Intersect(x1, x2, y1, y2):
- if (x1 > x2-20) and (x1 < x2+20) and (y1 > y2-20) and (y1 < y2+20):
- return 1
- else:
- return 0
- def make_level(level, platform):
- a = 0
- b = 0
- for row in level:
- for col in row:
- if col == '-':
- screen.blit(platform.img, (a, b))
- a += 20
- b += 20
- a = 0
- level = [
- '----------- -----------',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '- -',
- '----------- -----------']
- circles = [Circle(BLUE, (320, 380), 40),Circle(RED, (320, 100), 50)]
- circle = Circle(WHITE, (300, 600), 50)
- pl = Platform()
- circle.up = True
- circle.dn = True
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == KEYDOWN and event.key == K_SPACE:
- circle.vx = 0
- circle.vy = 0
- elif event.type == MOUSEBUTTONDOWN:
- if circles[0].point_in(event.pos):
- circles[0].change_color()
- if circle.point_in(event.pos):
- circle.attached = True
- elif event.type == MOUSEBUTTONUP:
- circle.attached = False
- elif event.type == MOUSEMOTION:
- if circle.attached:
- circle.vx,circle.vy = event.rel
- circle.move()
- if not circle.attached:
- circle.move()
- surface.fill(BLACK)
- make_level(level, pl)
- surface.blit(screen,(0, 0))
- circle.draw(surface)
- for c in circles:
- c.move()
- c.draw(surface)
- pygame.display.update()
- clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment