Beispiel #1
0
class BasicBaddie(pygame.sprite.Sprite):
	def __init__(self, x, y, dx, dy):
		super(BasicBaddie,self).__init__()
		self.image = pygame.Surface((30,30))
		self.image.fill((0,0,255))
		self.rect = self.image.get_rect()
		self.buff = SubPixelSurface(self.image)
		
		self.x, self.y, self.dx, self.dy = x, y, dx, dy
		self.rect.topleft = (x, y)
		
	
	def update(self):
		self.x += self.dx
		self.y += self.dy
		
		self.rect.topleft = (self.x, self.y)
		bound = self.rect.clamp(bounds)
		
		if bound.left != self.rect.left:
			self.dx = -self.dx
			self.x = bound.left
		if bound.top != self.rect.top:
			self.dy = -self.dy
			self.y = bound.top
			
		self.rect = bound

		self.image = self.buff.at(self.x,self.y)
		
	def die(self):
		self.kill()

	@classmethod
	def spawn(cls):
		to_close = True
		x,y = -1,-1
		
		while True:
			x,y = randint(0,screen.get_width()), randint(0,screen.get_height())
			
			clear = True
			for player in playersprites.sprites():
				clear = clear and (abs(player.rect.centerx-x)>60 or abs(player.rect.centery-y)>60)
			if clear: break	
			
		
		speed = 3
		angle = 2*math.pi*random()
		vx,vy = speed*math.sin(angle), speed*math.cos(angle)
		baddies.add(BasicBaddie(x,y,vx,vy))
Beispiel #2
0
class Player(pygame.sprite.Sprite):
	def __init__(self):
		super(Player,self).__init__()
		self.image = pygame.Surface((40,40))
		self.rect = self.image.get_rect()
		self.image.fill((255,255,255))
		pygame.draw.circle(self.image, (255,0,0), (20,20), 20)
		self.buff = SubPixelSurface(self.image)
		
		self.firerate    = 7
		self.timer       = 0
		self.bulletspeed = 12

	def update(self):
		x,y = self.rect.topleft
		self.image = self.buff.at(x,y)
		
	def fire(self):
		self.timer -= 1
		if self.timer>0:
			return
		self.timer = self.firerate
		
		# get the x/y velocities by normalizing 
		ax,ay = joystick.get_axis(2), joystick.get_axis(3)
		t = abs(ax)+abs(ay)
		if t==0:
			return
			
		ax/=t
		ay/=t
		r = math.sqrt((ax*ax)+(ay*ay))
		v = self.bulletspeed
		vx,vy = v*ax, v*ay
		
		
		# get starting point
		x, y = self.rect.center
		dx, dy = 21*ax, 21*ay/r
		x += dx
		y += dy
		
		bullets.add(Bullet(x,y,vx,vy))
Beispiel #3
0
class Bullet(pygame.sprite.Sprite):
	def __init__(self, x, y, dx, dy):
		super(Bullet,self).__init__()
		self.x, self.y, self.dx, self.dy = x, y, dx, dy
		
		self.image = pygame.Surface((10,10))
		self.rect = pygame.Rect(x-5,y-5,10,10)
		self.image.fill((0,0,0))
		self.buff = SubPixelSurface(self.image)
		
	def update(self):
		self.x += self.dx
		self.y += self.dy
		self.rect.center = (self.x, self.y)
		
		# destroy bullet if if is off the screen
		if not self.rect.colliderect(bounds):
			self.kill()
		
		self.image = self.buff.at(self.x, self.y)
Beispiel #4
0
class Player(pygame.sprite.Sprite):
	def __init__(self):
		super(Player,self).__init__()
		self.image = pygame.Surface((40,40))
		self.rect = self.image.get_rect()
		self.image.fill((255,255,255))
		pygame.draw.circle(self.image, (255,0,0), (20,20), 20)
		self.buff = SubPixelSurface(self.image)
		
		self.firerate    = 7
		self.timer       = 0
		self.bulletspeed = 12

	def update(self):
		x,y = self.rect.topleft
		self.image = self.buff.at(x,y)
		
	def fire(self):
		self.timer -= 1
		if self.timer>0:
			return
		self.timer = self.firerate
		
		# get the x/y velocities by normalizing 
		mx,my = pygame.mouse.get_pos()
		px,py = self.rect.center
		mx -= px
		my -= py
		r = math.sqrt((mx*mx)+(my*my))
		v = self.bulletspeed
		vx,vy = v*mx/r, v*my/r
		
		
		# get starting point
		x, y = self.rect.center
		dx, dy = 21*mx/r, 21*my/r
		x += dx
		y += dy
		
		bullets.add(Bullet(x,y,vx,vy))