Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- from .actor import *
- from .keyboard import *
- class Player(Actor):
- def __init__(self, image, pos, speed, keys):
- Actor.__init__(self, image, pos)
- self.speed = speed
- self.vel = [0, 0]
- self.keys = keys
- def update(self):
- self.vel = [0, 0]
- if len(self.keys) >= 1 and self.keys[0] and keyboard[keys[self.keys[0].upper()]]:
- self.vel[0] -= self.speed
- if len(self.keys) >= 2 and self.keys[1] and keyboard[keys[self.keys[1].upper()]]:
- self.vel[0] += self.speed
- if len(self.keys) >= 3 and self.keys[2] and keyboard[keys[self.keys[2].upper()]]:
- self.vel[1] -= self.speed
- if len(self.keys) >= 4 and self.keys[3] and keyboard[keys[self.keys[3].upper()]]:
- self.vel[1] += self.speed
- self.x += self.vel[0]
- self.y += self.vel[1]
- class Ball(Actor):
- def __init__(self, image, pos, speed):
- Actor.__init__(self, image, pos)
- self.speed = speed
- self.vel = [speed, speed]
- self.collisions = []
- def add_collision(self, objects, callback):
- self.collisions.append({ 'objs': objects, 'cb': callback })
- def update(self):
- self.x += self.vel[0]
- self.y += self.vel[1]
- edge_collide = False
- if self.right >= WIDTH or self.left <= 0:
- self.vel[0] = -self.vel[0]
- edge_collide = True
- if self.bottom >= HEIGHT or self.top <= 0:
- self.vel[1] = -self.vel[1]
- edge_collide = True
- if edge_collide:
- return
- for collision in self.collisions:
- if not len(collision['objs']):
- self.collisions.remove(collision)
- continue
- for obj in collision['objs']:
- if self.colliderect(obj):
- collision['cb'](self, obj)
- def set_size(width, height):
- global WIDTH
- global HEIGHT
- WIDTH = width
- HEIGHT = height
- return WIDTH, HEIGHT
- def spawnSeq(Obj, image, amount, spacing):
- objs = []
- actor = Actor(image)
- perRow = int(WIDTH / (actor.width + spacing)) - 1
- rows = int(amount / perRow) + 1
- offset = (WIDTH - perRow * (actor.width + spacing)) / 2
- spawned_amount = 0
- for y in range(rows):
- for x in range(perRow):
- if spawned_amount == amount:
- break
- spawned_amount += 1
- pos = x * (actor.width + spacing) + offset, y * (actor.height + spacing) + spacing
- objs.append(Obj(image, topleft = pos))
- return objs
- def spawnRand(Obj, image, amount, spacing):
- objs = []
- for obj in range(amount):
- collided = True
- attempts = 0
- actor = Actor(image)
- while collided:
- collided = False
- attempts += 1
- if attempts > 5000:
- print('Too many sprites!')
- quit()
- actor.pos = (randrange(int(0 + actor.width / 2), int(WIDTH - actor.width / 2)), randrange(int(0 + actor.height / 2), int(HEIGHT - actor.height / 2)))
- for other_obj in objs:
- if actor.colliderect(other_obj):
- collided = True
- break
- objs.append(Obj(image, actor.pos))
- return objs
Add Comment
Please, Sign In to add comment