Bouncing

This simulation lets you spawn in circular entities which then bounce off the groung and fade into the background. You can toggle if they are moving, the screen gets refilled and if they have a black border around them. The three-dimensional illusion comes from the balls getting slowly smaller and blending to the background color. Eventually the balls will ‘die’ (be removed), when they have reached a minimum size.

Usage

  • Clicking on the screen spawns in a new ball
  • ‘b’ toggles if the black border is shown
  • ‘f’ toggles if screen gets refilled
  • ‘m’ toggles if balls are moving
  • ‘k’ kills all the balls

The First ExampleThe Second ExampleThe Third Example


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 16th of April, 2015
#         edited 17th of April, 2015

# gets the scaled mouse position
def getMousePos():
	return [pygame.mouse.get_pos()[0] / main.SCALE, pygame.mouse.get_pos()[1] / main.SCALE]

# tries to get from one color to the other one
def blend(_color, _toColor):
	w = -1
	for _ in _toColor:
		w += 1
		if _color[w] > _:
			_color[w] -= 1
		elif _color[w]  n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

# ball class
class newBall():
	# init
	def __init__(self, _pos):
		self.pos = _pos
		self.velocity = [0, 0]
		self.falling = True
		self.radius = 50
		self.color = [255, 0, 0]
		self.borderColor = [0, 0, 0]
		
		self.DEAD = False
	
	# tick function
	def tick(self):
		# move the ball
		if self.falling:
			self.velocity[1] += .1
		else:
			self.velocity[1] += .11
			if self.velocity[1] > 0:
				self.velocity[1] = 0
				self.falling = True
		
		dif = 0
		
		if self.pos[1] + self.velocity[1] = main.HEIGHT:
			self.pos[1] = main.HEIGHT
			self.falling = False
			self.velocity[1] = -self.velocity[1]
		
		# radius gets smaller
		self.radius -= .1
		if int(self.radius) = main.HEIGHT:
				self.DEAD = True
		
		# kill the ball if it has no movement anymore
		#if self.velocity[1] == 0 and self.pos[1] >= main.HEIGHT:
		#	self.DEAD = True
		
		# blend colors
		if main.TICKS % 3 == 0:
			blend(self.color, main.BACKGROUNDCOLOR)
		#if main.TICKS % 2 == 0:
		#	blend(self.borderColor, main.BACKGROUNDCOLOR)
	
	# render function
	def render(self, _surf):
		pygame.draw.circle(_surf, self.color, [int(self.pos[0]), int(self.pos[1])], int(self.radius))
		if main.BORDER:
			pygame.draw.circle(_surf, self.borderColor, [int(self.pos[0]), int(self.pos[1])], int(self.radius), 1)

# tick function
def tick():
	
	# handle events
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
				sys.exit()
		
		# handle keys
		if event.type == pygame.KEYDOWN:
			# screen refill
			if event.key == pygame.K_f:
				if main.FILL:
					main.FILL = False
				else:
					main.FILL = True
			
			# ball border draw
			if event.key == pygame.K_b:
				if main.BORDER:
					main.BORDER = False
				else:
					main.BORDER = True
			
			# ball moving
			if event.key == pygame.K_m:
				if main.MOVING:
					main.MOVING = False
				else:
					main.MOVING = True
			
			# kill all balls
			if event.key == pygame.K_k:
				main.BALLS= []
		
		# spawn in new balls
		if event.type == pygame.MOUSEBUTTONDOWN:
			main.BALLS.append(newBall(getMousePos()))
	
	# tick balls
	if main.MOVING:
		for _ in main.BALLS:
			_.tick()
			if _.DEAD:
				main.BALLS.remove(_)

# render function
def render():
	# fill
	main.SCREEN.fill([0, 0, 0])
	if main.FILL or main.TICKS = 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

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

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

# main dummy
main = dummy()
main.TICKS = 0

main.RESOLUTION = 1
main.WIDTH = 1080 / main.RESOLUTION
main.HEIGHT = 720 / main.RESOLUTION
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SURF = pygame.Surface(main.SIZE)

main.SCALE = 1 * main.RESOLUTION
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]

main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Bouncing")
main.BACKGROUNDCOLOR = [255, 170, 0]
main.FILL = True
main.BORDER = True
main.MOVING = True

# ball list
main.BALLS = []

# 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