Create a gist now

Instantly share code, notes, and snippets.

Embed
Arduino Pong
# -*- coding: utf-8 -*-
# Taha Dogan Gunes
# tdgunes@gmail.com
# Got it from the internet from a long time ago
class _Getch(object):
"""Gets a single character from standard input.
Does not echo to the screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self):
return self.impl()
class _GetchUnix(object):
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows(object):
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
# -*- coding: utf-8 -*-
import obtainer
import serial
# Taha Dogan Gunes
# tdgunes@gmail.com
# change the serial configuration according to your setting and you can press q to exit
# use w and s keys to move the pluck up and down
ser = serial.Serial('/dev/tty.usbmodem1411', 9600)
while True:
character = obtainer.getch()
if character == "q": break
ser.write(character+"\n")
// Taha Dogan Gunes
// tdgunes@gmail.com
// Edited the example 'TFTDisplayText'
// So you can use the same diagram to connect the wires to screen
#include <TFT.h>
#include <SPI.h>
#define cs 10
#define dc 9
#define rst 8
TFT screen = TFT(cs, dc, rst);
char sensorPrintout[20];
String inputString = "";
boolean stringComplete = false;
String sensorVal = "waiting data!";
int cursorCount = 0;
char scores[10];
//w 160 px
//h 128 px
const int w = 160;
const int h = 128;
int block_width = 10; //px
int block_height = 30; //px
int ball_size = 10; //px
int playerOne[] = {0,0};
int playerTwo[] = {w-block_width,h-block_height};
int playerOneScore = 0;
int playerTwoScore = 0;
int get_random_direction(){
int v = random(0,10)-5;
if (v==0){
return 1;
}
else{
return v;
}
}
int ball[] = {screen.width()/2, screen.height()/2, get_random_direction(), get_random_direction()};
void setup() {
// Put this line at the beginning of every sketch that uses the GLCD:
screen.begin();
Serial.begin(9600);
inputString.reserve(200);
// clear the screen with a black background
screen.background(0, 0, 0);
screen.setTextSize(2);
randomSeed(analogRead(0));
}
void loop() {
// Read the value of the sensor on A0
screen.stroke(0,0,0);
screen.fill(0,0,0);
screen.circle(ball[0], ball[1], ball_size);
screen.rect(playerTwo[0], playerTwo[1], block_width, block_height);
//screen.rect(playerOne[0], playerOne[1], block_width, block_height);
delay(5);
//
// Serial.print("playerOneX:");
// Serial.println(playerOne[0]);
// Serial.print("playerOneY:");
// Serial.println(playerOne[1]);
// Serial.print("playerTwoX:");
// Serial.println(playerTwo[0]);
// Serial.print("playerTwoY:");
// Serial.println(playerTwo[1]);
// Serial.print("ballX:");
// Serial.println(ball[0]);
// Serial.print("ballY:");
// Serial.println(ball[1]);
if ( playerTwo[0] <= ball[0]+ball[2]+ball_size && ball[0]+ball[2]+ball_size <= playerTwo[0]+block_width &&
playerTwo[1] <= ball[1]+ball[2] && ball[1]+ball[2] <= playerTwo[1]+block_height){
ball[3] = -ball[3]*1.2;
ball[2] = -ball[2]*1.2;
}
if ( ball[0]+ball[2]-ball_size <= playerOne[0]+block_width &&
playerOne[1] <= ball[1]+ball[2]+ball_size/2 && ball[1]+ball[2]+ball_size/2 <= playerOne[1]+block_height ){
ball[3] = -ball[3]*1.2;
ball[2] = -ball[2]*1.2;
}
if (ball[0]+ball[2] < ball_size){
ball[0] = w/2;
ball[1] = h/2;
ball[2] = get_random_direction();
ball[3] = get_random_direction();
playerOneScore++;
screen.text(scores, w/2-15, 1);
}
if (ball[0]+ball[2] > w-ball_size){
ball[0] = w/2;
ball[1] = h/2;
ball[2] = get_random_direction();
ball[3] = get_random_direction();
playerTwoScore++;
screen.text(scores, w/2-15, 1);
}
if (ball[1]+ball[3] < ball_size){
ball[3] = -ball[3];
}
if (ball[1]+ball[3] > h-ball_size){
ball[3] = -ball[3];
}
ball[0] += ball[2];
ball[1] += ball[3];
playerTwo[1] = ball[1]-get_random_direction();
//playerOne[1] = ball[1]-get_random_direction();
if (stringComplete){
screen.stroke(0,0,0);
screen.fill(0,0,0);
screen.rect(playerOne[0], playerOne[1], block_width, block_height);
if (inputString == "w\n"){
// player one go up here
playerOne[1] -= 8;
}
else if (inputString == "s\n"){
// player one down up here
playerOne[1] += 8;
}
Serial.println(playerOne[0]);
Serial.println(playerOne[1]);
inputString = "";
stringComplete = false;
}
screen.stroke(255,255,255);
// set the fill color to grey
screen.fill(255,255,255);
//draw player one
screen.rect(playerOne[0], playerOne[1], block_width, block_height);
//draw player two
screen.rect(playerTwo[0], playerTwo[1], block_width, block_height);
//draw circle
screen.circle(ball[0], ball[1], ball_size);
//
screen.setTextSize(2);
String oneText = String(playerOneScore) + "-" + String(playerTwoScore);
oneText.toCharArray(scores,10);
screen.text(scores, w/2-15, 1);
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment