Jasteroids

This game is my interpretation of the all-time classic arcade game Asteroids. Although it plays a little differently, it has all the components of the original game. The rocks are stationary, but – if destroyed – split into two smaller pieces. You can either destroy the rocks via missles or by just flying right into them.

Controls

  • Left arrow key tilts the space craft to the left
  • Right arrow key tilts the space craft to the right
  • Up arrow key propels the space craft
  • Space launches a missle

The first example The second example The third example


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of August, 2015

# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

# player class
class player():
	# init
	def __init__(self):
		self.reset()
		self.color = [255, 255, 255]
		self.angle = 0
		self.size = 20
	
	# reset the player
	def reset(self):
		self.pos = main.CENTER[:]
		self.velocity = 0
	
	# render the player (and check for rocks)
	def render(self, _surface):
		# calculate positions
		p0 = getCirclePos(self.pos, self.size, self.angle)
		p1 = getCirclePos(self.pos, self.size, self.angle + 150)
		p2 = getCirclePos(self.pos, self.size, self.angle - 150)
		
		# draw player
		pygame.draw.line(_surface, self.color, p0, p1)
		pygame.draw.line(_surface, self.color, p1, self.pos)
		pygame.draw.line(_surface, self.color, self.pos, p2)
		pygame.draw.line(_surface, self.color, p2, p0)
		
		# check rocks
		for _ in main.ROCKS:
			if posDistance(_.pos, self.pos) < _.size or posDistance(_.pos, p0) < _.size or posDistance(_.pos, p1) < _.size or posDistance(_.pos, p2) < _.size:
				self.reset()
				_.split()
	
	# tick the player
	def tick(self):
		# key handling
		if pygame.K_UP in main.KEYSDOWN:
			self.velocity += .1
		if pygame.K_LEFT in main.KEYSDOWN:
			self.angle -= 1
		elif pygame.K_RIGHT in main.KEYSDOWN:
			self.angle += 1
		
		# velocity cap
		n = 10
		if self.velocity < -n:
			self.velocity = -n
		elif self.velocity > n:
			self.velocity = n
		
		# friction
		n = .05
		if self.velocity > n:
			self.velocity -= n
		elif self.velocity < -n:
			self.velocity += n
		else:
			self.velocity = 0
		
		# check screen bounds
		for _ in range(0, 2):
			if self.pos[_] < 0:
				self.pos[_] = main.SIZE[_]
				#self.angle += 180
			if self.pos[_] > main.SIZE[_]:
				self.pos[_] = 0
				#self.angle += 180
		
		# move player
		self.pos = getCirclePos(self.pos, self.velocity, self.angle)
	
	# hanlde player
	def handle(self, event):
		if event.type == pygame.KEYDOWN:
			# space launches a missle
			if event.key == pygame.K_SPACE:
				main.SHOTS.append(shot(getCirclePos(self.pos[:], self.size, self.angle), self.angle))

# shot class
class shot():
	# init
	def __init__(self, _pos, _angle):
		self.pos = _pos
		self.angle = _angle
		self.lifetime = 100
	
	# tick shots
	def tick(self):
		# decraese lifetime
		self.lifetime -= 1
		
		# update pos
		self.pos = getCirclePos(self.pos, 5, self.angle)
		
		# check bounds
		for _ in range(0, 2):
			if self.pos[_] < 0:
				self.pos[_] = main.SIZE[_]
			if self.pos[_] > main.SIZE[_]:
				self.pos[_] = 0
		
		# die if too old
		if self.lifetime <= 0:
			return True
		
		# die and split rock if hitting a rock
		for _ in main.ROCKS:
			if posDistance(self.pos, _.pos) < _.size:
				_.split()
				return True
	
	# render shot
	def render(self, _surface):
		_surface.set_at(intpos(self.pos), [255, 255, 255])

# rock class
class rock():
	# init
	def __init__(self, _pos = None, _dest = None, _size = 40):
		self.dest = _dest
		self.pos = _pos
		self.size = _size
		
		if self.dest == None:
			self.dest = [random.randint(0, main.WIDTH), random.randint(0, main.HEIGHT)]
		if self.pos == None:
			self.pos = self.dest[:]
		
		self.dead = False
		
		# calculate render positions
		self.p = []
		angle = 0
		while angle < 360:
			self.p.append(angle)
			angle += random.randint(50, 100)
	
	# tick
	def tick(self):
		# approach destination
		for _ in range(0, 2):
			
			dif = self.dest[_] - self.pos[_]
			self.pos[_] += dif / 50.
		
		# check bounds
		for _ in range(0, 2):
			if self.pos[_] < 0:
				self.pos[_] = main.SIZE[_]
				self.dest[_] = self.dest[_] % main.SIZE[_]
			if self.pos[_] > main.SIZE[_]:
				self.pos[_] = 0
				self.dest[_] = self.dest[_] % main.SIZE[_]
		
	
	# render
	def render(self, _surface):
		last = self.p[-1]
		for _ in self.p:
			pygame.draw.line(_surface, [255, 255, 255], getCirclePos(self.pos, self.size, last), getCirclePos(self.pos, self.size, _))
			last = _
	
	# split (die and release two other asteroids)
	def split(self):
		self.dead = True
		
		if self.size > 5:
			a = random.randint(0, 180)
			main.ROCKS.append(rock(self.pos[:], getCirclePos(self.pos, 100, a), self.size / 2))
			main.ROCKS.append(rock(self.pos[:], getCirclePos(self.pos, 100, a+180), self.size / 2))

""" FUNCTIONS """
# gets the position on a circle
# circle center                           : '_pos'
# circle radius                           : '_radius'
# angle from center to point on the circle: '_angle'
def getCirclePos(_pos, _radius, _angle):
	return [
				_pos[0] + _radius * math.cos(math.radians(_angle)),
				_pos[1] + _radius * math.sin(math.radians(_angle))
			]

# returns an integer version of given positon
def intpos(_pos):
	return [int(_pos[0]), int(_pos[1])]

# basic vector functions
def vecConvert(p1, p2):
	return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
	return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
	return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
	return [point[0] + vec[0], point[1] + vec[1]]
def vecAdd(vec1, vec2):
	return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# calculates distance between given positions
def posDistance(p1, p2):
	return math.sqrt( (p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 )

# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()
		
		# keyup
		if event.type == pygame.KEYUP:
			# handle 'main.KEYSDOWN'
			if event.key in main.KEYSDOWN:
				main.KEYSDOWN.remove(event.key)
		
		# keydown
		if event.type == pygame.KEYDOWN:
			# handle 'main.KEYSDOWN'
			if event.key not in main.KEYSDOWN:
				main.KEYSDOWN.append(event.key)
		
		# player event handler
		main.PLAYER.handle(event)
	
	# tick player, shots and rocks
	main.PLAYER.tick()
	
	for _ in main.SHOTS:
		if _.tick():
			main.SHOTS.remove(_)
	
	for _ in main.ROCKS:
		_.tick()
		if _.dead:
			main.ROCKS.remove(_)

# render function
def render():
	# fill
	main.SURF.fill(main.COLOR)
	
	# render plaer, shots and rocks
	main.PLAYER.render(main.SURF)
	
	for _ in main.SHOTS:
		_.render(main.SURF)
	
	for _ in main.ROCKS:
		_.render(main.SURF)
	
	# blit and flip
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	main.WIDTH, main.HEIGHT = 1080, 720
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.CENTER = [main.WIDTH / 2., main.HEIGHT / 2.]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Jasteroids"
	main.COLOR = [0, 0, 0]
	main.TICKS = 0
	main.KEYSDOWN = []
	
	main.PLAYER = player()
	main.SHOTS = []
	main.ROCKS = []
	for _ in range(0, 10):
		main.ROCKS.append(rock())
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s