Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
396a769
added project proposal
Mar 5, 2018
f0bcf8e
early experiments & tutorials
Mar 7, 2018
fd13fa8
early experiments and learning
Mar 9, 2018
d983436
Added explanation for mid project checkin
Mar 9, 2018
8104ce9
Additional materials
ASHSWAN1999 Mar 9, 2018
ef9abee
Merge branch 'master' of https://github.com/JonahSpicher/InteractiveP…
ASHSWAN1999 Mar 9, 2018
afd6bd4
Separated for organization
Mar 9, 2018
2f56819
sound stuff
Mar 11, 2018
86eb003
Got pyaudio working
Mar 12, 2018
0214d07
record function working
Mar 13, 2018
25a45e6
Frequency test code
ASHSWAN1999 Mar 13, 2018
c40a952
Merge branch 'master' of https://github.com/JonahSpicher/InteractiveP…
ASHSWAN1999 Mar 13, 2018
a636717
Frequency testing
ASHSWAN1999 Mar 14, 2018
afc5b29
I guess I had to do this
Mar 14, 2018
00d853a
First implementation of frequency movement
Mar 14, 2018
300de75
Broken mic upsdate
Mar 15, 2018
8676add
Working Code
ASHSWAN1999 Mar 16, 2018
33b2819
removed a couple things
ASHSWAN1999 Mar 16, 2018
594b778
Finished documentation
ASHSWAN1999 Mar 16, 2018
50b607f
Delete README.md
NoraSpicher Mar 16, 2018
d316cf4
Delete jabberwocky.png
NoraSpicher Mar 16, 2018
1dfba83
Delete johan.jpg
NoraSpicher Mar 16, 2018
1de1e20
Delete Proposal.md
NoraSpicher Mar 16, 2018
c022c42
Delete classes.py
NoraSpicher Mar 16, 2018
3a697e3
Delete round_one.py
NoraSpicher Mar 16, 2018
72d85d4
Delete jonah.jpg
NoraSpicher Mar 16, 2018
7d106b8
Delete sound_experimentation.py
NoraSpicher Mar 16, 2018
ea66291
Delete midproject_checkin.txt
NoraSpicher Mar 16, 2018
78cd42c
Delete game_tutorial.py
NoraSpicher Mar 16, 2018
c5b15bf
Delete freq_testing.py
NoraSpicher Mar 16, 2018
9b703d1
Delete freq_test.py
NoraSpicher Mar 16, 2018
304198d
Delete main.py
NoraSpicher Mar 16, 2018
99e093c
Delete record.py
NoraSpicher Mar 16, 2018
1d8b0ae
Delete stop.txt
NoraSpicher Mar 16, 2018
f3b13ed
Delete file.wav
NoraSpicher Mar 16, 2018
0e2fc22
Delete Jones-RapidGameDev.pdf
NoraSpicher Mar 16, 2018
2a0b4c5
Delete Mobydick.jpg
NoraSpicher Mar 16, 2018
f00bef6
Delete freq_test.py
NoraSpicher Mar 16, 2018
ae0c364
Delete freq_testing.py
NoraSpicher Mar 16, 2018
b42619f
Delete functions.py
NoraSpicher Mar 16, 2018
1e4186d
Update README.md
NoraSpicher Mar 16, 2018
de18ca7
Delete game_tutorial.py
NoraSpicher Mar 16, 2018
20c30e4
Delete jabberwocky.png
NoraSpicher Mar 16, 2018
92ce16f
Delete johan.jpg
NoraSpicher Mar 16, 2018
abb7d92
Delete jonah.jpg
NoraSpicher Mar 16, 2018
1973521
Delete record2.py
NoraSpicher Mar 16, 2018
e829b37
Delete round_one.py
NoraSpicher Mar 16, 2018
233234b
Delete sound_experimentation.py
NoraSpicher Mar 16, 2018
be62cd5
Delete functions.py
NoraSpicher Mar 16, 2018
5569241
Actually finished documentation, for real this time
Mar 16, 2018
758daa3
Merge branch 'master' of https://github.com/JonahSpicher/InteractiveP…
Mar 16, 2018
0add9cf
Test for merge conflicts
Mar 16, 2018
ff8a304
Another merge test
Mar 16, 2018
3ea170c
Add files via upload
ASHSWAN1999 Mar 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Important Stuff/MiniProject4.pdf
Binary file not shown.
38 changes: 38 additions & 0 deletions Important Stuff/Proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Project Proposal
Jonah Spicher & Ashley Swanson

Our project will be a video game, focusing on alternative inputs to the traditional
mouse and keyboard style. Our game will consist of avoiding oncoming obstacles using
pitch to control the height of the little player guy. This nontraditional input will
allow us to explore interactive programming while learning about the tendencies of
foreign libraries such as pygame and OpenCV. As a stretch goal, we would also like
to explore electrical inputs, which would let us do really silly things like a
potentiometer controlled by the strength of your breath blowing on some apparatus.

As For Learning Goals:
Jonah:
I am excited to learn a little bit about pygame, I would definitely do more with
that in the future. Making video games sounds cool.
Ashley:
I am excited to learn about different inputs as I have very little concept of how
to work with inputs outside of typing something into a box when prompted. I am
also looking forwards to tackling the documentation involved in figuring out the
libraries as this is something I generally find intimidating and I would like to
become more comfortable with it.

And Now For Libraries:
We are going to use pygame for collisions and other video game features and OpenCV
to process our audio input. We may need some other ones but thats what we know at
the moment. Maybe like, math.

By our mid-project check-in:
We will definitely have installed OpenCV and pygame and made sure specifically
that OpenCV will work with audio. We will probably also have planned out mostly
how our code will be structured, and started working on some of that.

Potential Risks:
We might get so distracted by how hilarious it is that people are screaming at
our laptop that we never do any work. If you were not aware we are dating so
its pretty hilarious that we are partners. We are both totally fine with this and
work together well (we have worked on projects together before), but if we
break up over this project we will sue.
41 changes: 41 additions & 0 deletions Important Stuff/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2018 at Olin College.

To run:

In terminal, type:

$python record.py & python main.py

Necessary packages:

PyAudio

To install:

In terminal, type:

$ sudo git clone http://people.csail.mit.edu/hubert/git/pyaudio.git

$ sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev

$ sudo apt-get install python3-dev

$ pip install pyaudio

$ cd pyaudio

$ sudo python setup.py install

pygame

To install:

In terminal, type:

$sudo apt-get install python3-pygame


Also, sorry for the awkwardly organized files, that is the result of git confusion.

Test for merge conflict Ashley style
11 changes: 11 additions & 0 deletions Important Stuff/midproject_checkin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
So, just to explain where we are as of right now, we have implemented a very
basic version of our game, which was how we learned pygame. It makes
obstacles move to the left and you dodge them by moving up and down. It's very
bare bones right now, the plan is to make it nicer and then change the control
scheme to audio, as we discussed in the proposal.

Speaking of audio, OpenCV will definitely not work. However, we learned that
we can use Pyaudio to record and then Librosa to analyze, and that should do
exactly what we want to do. We will record very small intervals, use Librosa
to find the pitch of the previous interval, and then map the location of the
player sprite to the pitch.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repo: ff5f763093769902dac5f101b33a9704a790606a
node: ba1a82994f56e80237171f5e48864562ad789434
branch: default
tag: drawing1.0
2 changes: 2 additions & 0 deletions Important Stuff/przemoli-pygametutorial-ba1a82994f56/.hgtags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cc13abeb84a8d72cfe0225cf3cc772e92a25beeb basic0.5
540433c50ffc681822bf3d82afb35cb5ca91c900 basic1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# PyGame Tutorials
This repo contain PyGame Tutorials source code!

Tutorials can be found at:
pygametutorials.wikidot.com

# License
This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
41 changes: 41 additions & 0 deletions Important Stuff/przemoli-pygametutorial-ba1a82994f56/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pygame
from pygame.locals import *

class App:
def __init__(self):
self._running = True
self._display_surf = None
self._image_surf = None

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode((350,350), pygame.HWSURFACE)
self._running = True
self._image_surf = pygame.image.load("myimage.jpg").convert()

def on_event(self, event):
if event.type == QUIT:
self._running = False
def on_loop(self):
pass
def on_render(self):
self._display_surf.blit(self._image_surf,(0,0))
pygame.display.flip()

def on_cleanup(self):
pygame.quit()

def on_execute(self):
if self.on_init() == False:
self._running = False

while( self._running ):
for event in pygame.event.get():
self.on_event(event)
self.on_loop()
self.on_render()
self.on_cleanup()

if __name__ == "__main__" :
theApp = App()
theApp.on_execute()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

216 changes: 216 additions & 0 deletions classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
"""A collection of all of the classes and methods used to run the game, main
imports from here. Initially this was because we thought recording was going to
work differently than it had to. The way things ended up, main looks kind of silly.
"""
import pygame, sys, random
from pygame.locals import *

import pyaudio
from pyaudio import *
import wave
import time

import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile
from numpy import arange

import pickle
from pickle import dump, load

SCREENWIDTH = 1500
SCREENHEIGHT = 1000

class Block(pygame.sprite.Sprite):
"""These are the obstacles that move towards you. They inherit from pygame's
sprite class.
"""
def __init__(self, screen, color, width, height):
"""Gives a size, color, and speed.
"""
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.screen = screen
self.rect = self.image.get_rect()
self.speedx = 10

def update(self):
"""Moves the block according to its speed.
"""
self.rect.left -= self.speedx
if self.rect.right < 0:
x_displacement = random.randint(0, SCREENWIDTH*2)
self.rect.x = (SCREENWIDTH + x_displacement)
self.rect.y = random.randrange(SCREENHEIGHT - 150)


class Player(pygame.sprite.Sprite):
"""This is also a sprite, but it is the block you control.
"""
def __init__(self, screen, color, size, increment):
"""Gives the player a color, size, and sets how quickly it moves. Also
initializes expected frequencies.
"""
super().__init__()
self.image = pygame.Surface((size, size))
self.size = size
self.image.fill(color)
self.screen = screen
self.rect = self.image.get_rect()
self.increment = increment
self.is_recording = False

#Initially, we wanted to calibrate the game to the user. This could be
#implemented somewhat easily, but as it is, we ran out of time. Also,
#we found these values worked for most people.
self.calibrated_high = 100
self.calibrated_low = 450

def collide(self, obstacles, game):
"""Checks to see if the player has hit an obstacle.
"""
if pygame.sprite.spritecollide(self, obstacles, False):
#First, collide edits stop.txt to tell record to stop recording.
f = open('stop.txt', 'wb')
insert = pickle.dumps('stop')
f.write(insert)
f.close()

#Then, it displays the score for a few seconds
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 300)
textsurface = myfont.render("Score=%d"%(int(game.score)), False, (255, 255, 255))
game.mainSurface.blit(textsurface,(275,400))
pygame.display.update()
time.sleep(2)

#Then it quits.
pygame.quit()
sys.exit()

def analyze_freq(self):
"""Analyze frequency uses a fast Fourier transform to get the frequency
of file.wav, which is consistently being updated by record. It returns
the frequency in Hz. FFT is somewhat imprecise with such short increments,
and gets better the longer we record for, but also the game feels laggy.
0.2 seconds is somewhat of a balance.
"""
#A try clause was necessary, because occasionally, it will try to read
#file.wav while record is trying to write to it, resulting in an error.
#Our except clause keeps the previous values stored in fie.wav
try:
fs, data = wavfile.read('file.wav')
except:
data = self.data
fs = self.fs

self.data = data
self.fs = fs

a = data.T[0]
b=[(ele/2**8.)*2-1 for ele in a]
c = fft(b)

d = len(c)//2

k = arange(len(data))
T = len(data)/fs
frqLabel = k/T

#Eccentricities in this algorithm make low frequencies appear very strongly,
#So we look after the first few entries in the frequency list to get a more
#accurate idea of what's going on.
val = max(abs(c[2:(d-1)]))
i = list(abs(c[2:(d-1)])).index(val)
return frqLabel[2:(d-1)][i]

def freq_movement(self, frequency):
"""Moves the player based on the detected frequency.
"""

#First sets the middle frequency based on the high and low established in
#init
mid_freq = (self.calibrated_low + self.calibrated_high)/2
if frequency < 70:
pass

elif frequency > mid_freq:
#Then moves the correct direction, assuming that moving would not
#move the player offscreen.
if self.rect.y < self.increment:
self.rect.y = 0
elif self.rect.y > 0:
self.rect.y -= self.increment

elif frequency < mid_freq:
if self.rect.y > SCREENHEIGHT-(self.size + self.increment):
self.rect.y = SCREENHEIGHT - self.size
elif self.rect.y < SCREENHEIGHT- self.size:
self.rect.y += self.increment





class Game:
"""This class organizes the player and several obstacles, then runs and renders
the game.
"""
def __init__(self, num_blocks, player_color=(255, 0, 0)):
"""Makes a given number of obstacle blocks, then a player. Also prepares
necessary pygame assets.
"""
pygame.init()


self.mainSurface = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), 0, 32)
pygame.display.set_caption("Collision Game")
self.blocksGroup = pygame.sprite.Group()
self.player_group = pygame.sprite.Group()
self.score = 0

for i in range(num_blocks):
height = random.randint(30, 150)
weight = random.randint(30, 150)
myBlock = Block(self.mainSurface, (255, 255, 255), weight, height)
x_displacement = random.randint(0, SCREENWIDTH)
myBlock.rect.x = (SCREENWIDTH + x_displacement)
myBlock.rect.y = random.randrange(SCREENHEIGHT - 150)
self.blocksGroup.add(myBlock)

self.player = Player(self.mainSurface, player_color, 50, 10)
self.player.rect.x = 200
self.player.rect.y = 0.5*SCREENHEIGHT
#The player has to be added to a group all by itself in order to be
#drawn later.
self.player_group.add(self.player)
#Ha, comic sans was in the example we got, it doesn't actually do comic
#sans, it just does some default font. This is fine.
self.myfont = pygame.font.SysFont('Comic Sans MS', 150)

def run(self):
"""Checks for the quit event, then moves everything as much as it needs
to be moved and checkes for a collision, then draws everything. Then
updates the score.
"""
while True:
for event in pygame.event.get():
if event.type == QUIT:
print(self.score)
pygame.quit()
sys.exit()

freq = self.player.analyze_freq()
self.player.freq_movement(freq)
self.player.collide(self.blocksGroup, self)

self.mainSurface.fill((0, 0, 0))
textsurface = self.myfont.render(str(int(self.score)), False, (255, 255, 255))
print(textsurface)
self.player_group.draw(self.mainSurface)
self.blocksGroup.update()
self.blocksGroup.draw(self.mainSurface)
self.mainSurface.blit(textsurface,(10,10))
pygame.display.update()
self.score += .1
Binary file added file.wav
Binary file not shown.
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
@author Ashley Swanson & Jonah Spicher
"""

from classes import Player, Block, Game

if __name__ == "__main__":
game = Game(num_blocks=10)
game.run()
4 changes: 4 additions & 0 deletions przemoli-pygametutorial-ba1a82994f56/.hg_archival.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repo: ff5f763093769902dac5f101b33a9704a790606a
node: ba1a82994f56e80237171f5e48864562ad789434
branch: default
tag: drawing1.0
2 changes: 2 additions & 0 deletions przemoli-pygametutorial-ba1a82994f56/.hgtags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cc13abeb84a8d72cfe0225cf3cc772e92a25beeb basic0.5
540433c50ffc681822bf3d82afb35cb5ca91c900 basic1.0
8 changes: 8 additions & 0 deletions przemoli-pygametutorial-ba1a82994f56/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# PyGame Tutorials
This repo contain PyGame Tutorials source code!

Tutorials can be found at:
pygametutorials.wikidot.com

# License
This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
Loading