GabeLinux

PyGame Negative

Feb 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. from random import randrange
  2. from .actor import *
  3. from .keyboard import *
  4.  
  5. class Player(Actor):
  6.     def __init__(self, image, pos, speed, keys):
  7.         Actor.__init__(self, image, pos)
  8.  
  9.         self.speed = speed
  10.         self.vel = [0, 0]
  11.         self.keys = keys
  12.  
  13.     def update(self):
  14.         self.vel = [0, 0]
  15.  
  16.         if len(self.keys) >= 1 and self.keys[0] and keyboard[keys[self.keys[0].upper()]]:
  17.             self.vel[0] -= self.speed
  18.  
  19.         if len(self.keys) >= 2 and self.keys[1] and keyboard[keys[self.keys[1].upper()]]:
  20.             self.vel[0] += self.speed
  21.  
  22.         if len(self.keys) >= 3 and self.keys[2] and keyboard[keys[self.keys[2].upper()]]:
  23.             self.vel[1] -= self.speed
  24.  
  25.         if len(self.keys) >= 4 and self.keys[3] and keyboard[keys[self.keys[3].upper()]]:
  26.             self.vel[1] += self.speed
  27.  
  28.         self.x += self.vel[0]
  29.         self.y += self.vel[1]
  30.  
  31. class Ball(Actor):
  32.     def __init__(self, image, pos, speed):
  33.         Actor.__init__(self, image, pos)
  34.  
  35.         self.speed = speed
  36.         self.vel = [speed, speed]
  37.         self.collisions = []
  38.  
  39.     def add_collision(self, objects, callback):
  40.         self.collisions.append({ 'objs': objects, 'cb': callback })
  41.  
  42.     def update(self):
  43.         self.x += self.vel[0]
  44.         self.y += self.vel[1]
  45.        
  46.         edge_collide = False
  47.         if self.right >= WIDTH or self.left <= 0:
  48.             self.vel[0] = -self.vel[0]
  49.             edge_collide = True
  50.         if self.bottom >= HEIGHT or self.top <= 0:
  51.             self.vel[1] = -self.vel[1]
  52.             edge_collide = True
  53.  
  54.         if edge_collide:
  55.             return
  56.  
  57.         for collision in self.collisions:
  58.             if not len(collision['objs']):
  59.                 self.collisions.remove(collision)
  60.                 continue
  61.  
  62.             for obj in collision['objs']:
  63.                 if self.colliderect(obj):
  64.                     collision['cb'](self, obj)
  65.  
  66. def set_size(width, height):
  67.     global WIDTH
  68.     global HEIGHT
  69.     WIDTH = width
  70.     HEIGHT = height
  71.  
  72.     return WIDTH, HEIGHT
  73.  
  74. def spawnSeq(Obj, image, amount, spacing):
  75.     objs = []
  76.  
  77.     actor = Actor(image)
  78.     perRow = int(WIDTH / (actor.width + spacing)) - 1
  79.     rows = int(amount / perRow) + 1
  80.     offset = (WIDTH - perRow * (actor.width + spacing)) / 2
  81.  
  82.     spawned_amount = 0
  83.  
  84.     for y in range(rows):
  85.         for x in range(perRow):
  86.             if spawned_amount == amount:
  87.                 break
  88.  
  89.             spawned_amount += 1
  90.             pos = x * (actor.width + spacing) + offset, y * (actor.height + spacing) + spacing
  91.             objs.append(Obj(image, topleft = pos))
  92.  
  93.     return objs
  94.  
  95. def spawnRand(Obj, image, amount, spacing):
  96.     objs = []
  97.  
  98.     for obj in range(amount):
  99.         collided = True
  100.         attempts = 0
  101.         actor = Actor(image)
  102.  
  103.         while collided:
  104.             collided = False
  105.             attempts += 1
  106.  
  107.             if attempts > 5000:
  108.                 print('Too many sprites!')
  109.                 quit()
  110.  
  111.             actor.pos = (randrange(int(0 + actor.width / 2), int(WIDTH - actor.width / 2)), randrange(int(0 + actor.height / 2), int(HEIGHT - actor.height / 2)))
  112.             for other_obj in objs:
  113.                 if actor.colliderect(other_obj):
  114.                     collided = True
  115.                     break
  116.  
  117.         objs.append(Obj(image, actor.pos))
  118.  
  119.     return objs
Add Comment
Please, Sign In to add comment