Geburtstag
Happy, Happy, Happy war der beste. Meine bessere Hälfte hat sich tapfer geschlagen. Ich hab ein supper zukunfts gefühl.
#include <TVout.h> #include <fontALL.h> TVout TV; int bx = 0; int by = 0; int dx = 0; int dy = 0; int leftP = 0; int rightP =0; char gewertet = 0; int ballsize = 3; int paddle = 20; int paddle_cpu = 15; int btn = 500; void setup() { TV.begin(PAL,128,96); TV.select_font(font6x8); //bis ultraschall sensoren da pinMode(2, INPUT); pinMode(3, INPUT); pinMode(4, INPUT); xinit(); Serial.begin(9600); } void xinit(){ randomSeed(analogRead(5)); if(random(1,2)==2){dx=1;}else{dx=-1;} if(random(1,2)==2){dy=1;}else{dy=-1;} bx = TV.hres()/2; by = TV.vres()/2; TV.fill(BLACK); } void loop() { int p; int inPin; inPin = analogRead(0); // <-- read poti 0 - 5 volts //bis wieder sensor buttons if(digitalRead(2) == LOW){btn-=50;if(btn<0){btn=0;}} if(digitalRead(3) == LOW){btn+=50;if(btn>1000){btn=1000;}} if(digitalRead(4) == HIGH){btn = (by-1) * 10;} inPin= btn; //end for(p= 0; p<5; p++){ //Clear bars TV.draw_line(p,0,p,96,BLACK); TV.draw_line(127-p,0,127-p,96,BLACK); } int ps = paddle_cpu; int top = by -5; if(top + ps > 96){top = 95 - ps;} if(top < 1){top=0;} for(p= 0; p<5; p++){ //draw right bar TV.draw_line(127-p,top,127-p,top+ps,WHITE); } ps = paddle; top = (inPin/10); if(top + ps > 96){top = 96 - ps;} for(p= 0; p<5; p++){ //draw left bar TV.draw_line(p,top,p,top+ps,WHITE); } TV.draw_circle(bx,by,ballsize,BLACK,BLACK); //clear ball if(bx < ballsize+2){dx=1;} if(bx > 126 - ballsize){dx=-1;} if(by < ballsize){dy=1;} if(by > 96 - ballsize){dy=-1;} bx = bx + dx; by = by + dy; TV.draw_circle(bx,by,ballsize,WHITE,WHITE); // draw ball if(dx == 1 && bx > (123-ballsize) && gewertet == 0){ //right panel unsigned char f = TV.get_pixel(bx + ballsize + 1,by); gewertet = 1; if(f == 0){leftP = leftP + 1;xinit();} Serial.println(f); } if(bx > 60 && bx < 70){ //center reset gewertet = 0; } if(dx == -1 && bx < (ballsize+2) && gewertet == 0){ //left panel unsigned char f = TV.get_pixel(bx - ballsize - 1,by); gewertet = 1; if(f == 0){rightP = rightP + 1;xinit();} Serial.println(f); } TV.set_cursor(8,0); TV.print("LEFT: "); TV.print(leftP); TV.print(" RIGHT: "); TV.print(rightP); TV.delay(20); }
/********************************************* * Ping Pong by Steffen Flamm * Visit me at flamm.me *********************************************/ #include <TVout.h> #include <fontALL.h> #define BALLSIZE 3 #define PADDLESIZE 20 #define PADDLEWIDTH 5 TVout TV; int ballx, dirx; float diry, bally; char lostball = 0; int PaddleL, PaddleR; int halfpaddle = PADDLESIZE / 2; int scoreL, scoreR; int btn = 500; /* collision detection */ char _ce =0; void collision(){ //TV.hres() x //TV.vres() y if(ballx > TV.hres() - (PADDLEWIDTH + BALLSIZE) && _ce < PADDLEWIDTH - BALLSIZE){ //right _ce += 1; if(bally<(PaddleR + (halfpaddle / 2)) && bally>(PaddleR - (halfpaddle / 2))){//kleiner ausgangswinkel diry = (diry<=0.0)?0.5:-0.5; if(bally>(PaddleR - (halfpaddle / 2))){ diry *= -1; } dirx *= -1; return playSOUND(0); } if(bally<(PaddleR + (halfpaddle-2)) && bally>(PaddleR - (halfpaddle-2))){//grosser ausgangswinkel diry = (diry<=0.0)?0.75:-0.75; if(bally>(PaddleR - (halfpaddle-2))){ diry *= -1; } dirx *= -1; return playSOUND(0); } if(bally<(PaddleR + (halfpaddle+4)) && bally>(PaddleR - (halfpaddle+4))){//grosser ausgangswinkel diry = (diry<=0.0)?1.25:-1.25; if(bally>(PaddleR - (halfpaddle+3))){ diry *= -1; } dirx *= -1; return playSOUND(0); } if(_ce >= PADDLEWIDTH - BALLSIZE){ lostball = 2; return playSOUND(3); } } else if(ballx < (PADDLEWIDTH + BALLSIZE) && _ce < PADDLEWIDTH - BALLSIZE){ //left _ce += 1; //PaddleL if(bally<(PaddleL + (halfpaddle / 2)) && bally>(PaddleL - (halfpaddle / 2))){//kleiner ausgangswinkel diry = (diry<=0.0)?0.5:-0.5; if(bally>(PaddleL - (halfpaddle / 2))){ diry *= -1; } dirx *= -1; return playSOUND(0); } if(bally<(PaddleL + (halfpaddle-2)) && bally>(PaddleL - (halfpaddle-2))){//grosser ausgangswinkel diry = (diry<=0.0)?0.75:-0.75; if(bally>(PaddleL - (halfpaddle-2))){ diry *= -1; } dirx *= -1; return playSOUND(0); } if(bally<(PaddleL + (halfpaddle+4)) && bally>(PaddleL - (halfpaddle+4))){//grosser ausgangswinkel diry = (diry<=0.0)?1.25:-1.25; if(bally>(PaddleL - (halfpaddle+3))){ diry *= -1; } dirx *= -1; return playSOUND(0); } if(_ce >= PADDLEWIDTH - BALLSIZE){ lostball = 1; return playSOUND(3); } } else{ //idle _ce = 0; } } /* Paddle */ int _paddelhelper(int pos){ if(pos < 0){ return 0; } else if(pos < (TV.vres() - PADDLESIZE)){ return pos; } else{ return TV.vres() - PADDLESIZE; } } void drawpaddles(int l, int r){ int lpTop = _paddelhelper(l); int rpTop = _paddelhelper(r); /* clear ball */ TV.draw_circle(ballx,(int)bally,BALLSIZE,BLACK,BLACK); //clear ball /* draw bars */ for(int p= 0; p<=PADDLEWIDTH; p++){ //Clear bars TV.draw_line(p,0,p,TV.vres(),BLACK); //left bar TV.draw_line(p,lpTop,p,lpTop + PADDLESIZE,WHITE); TV.draw_line((TV.hres()-1)-p,0,(TV.hres()-1)-p,TV.vres(),BLACK); //right bar TV.draw_line((TV.hres()-1)-p,rpTop,(TV.hres()-1)-p,rpTop + PADDLESIZE,WHITE); } PaddleL = lpTop + halfpaddle; PaddleR = rpTop + halfpaddle; return; } /* Ball */ void drawBall(){ float nbally = bally + diry; if(nbally < BALLSIZE || nbally > TV.vres() - BALLSIZE){ diry = diry *-1; bally = nbally; playSOUND(1); } else{ bally = bally + diry; } ballx += dirx; collision(); TV.draw_circle(ballx,(int)bally,BALLSIZE,WHITE,WHITE); // draw ball } /* init */ void pingponginit(){ lostball = 0; ballx = TV.hres()/2; bally = TV.vres()/2; PaddleL = bally;// - halfpaddle; PaddleR = PaddleL; dirx = -1; diry = 0; switch (random(4)){ case 0: diry = 0; break; case 1: diry = 0.5; break; case 2: diry = 0.75; break; case 3: diry = 1.25; break; } dirx = (random(2)!=0)?-1:1; diry = (random(2)!=0)?diry*-1:diry; TV.fill(BLACK); } /* SOUNDS */ void playSOUND(int w){ switch (w){ case 0: //paddle TV.tone(222,50); break; case 1: // wall TV.tone(333,50); break; case 3: // loose TV.tone(111,200); break; } } /*LOST*/ void lostGame(){ if(lostball == 1){ scoreR++; } else{ scoreL++; } TV.delay(1000); TV.fill(BLACK); TV.set_cursor(0,44); TV.print("LEFT: "); TV.print(scoreL); TV.print(" RIGHT: "); TV.println(scoreR); TV.delay(2000); TV.set_cursor(0,66); TV.print("Weiter in 3... "); TV.delay(1000); TV.set_cursor(0,66); TV.print("Weiter in 2... "); TV.delay(1000); TV.set_cursor(0,66); TV.print("Weiter in 1... "); TV.delay(1000); TV.fill(BLACK); } /* main */ void setup(){ TV.begin(PAL,128,96); TV.select_font(font6x8); //bis ultraschall sensoren da pinMode(2, INPUT); pinMode(3, INPUT); pinMode(4, INPUT); randomSeed(analogRead(5)); pingponginit(); //debug Serial.begin(9600); } /* simple ki */ int movePaddle(int paddle){ int ball = bally - halfpaddle; int padd = paddle - halfpaddle; int half = halfpaddle - halfpaddle / 3; if((paddle + half ) > bally && (paddle - half) < bally){return padd;} if(random(9)>2){ //move if(padd<(ball)){return ++padd;}else{return --padd;} } return padd; } void loop(){ int cpuPaddleRight = bally - halfpaddle; int inPin; inPin = analogRead(0); // <-- read poti 0 - 5 volts 0 - 1023 //bis wieder sensor buttons if(digitalRead(2) == LOW){ btn-=50; if(btn<0){ btn=0; } } if(digitalRead(3) == LOW){ btn+=50; if(btn>1023){ btn=1023; } } inPin = (float)btn / 1023.0 * (float)TV.vres(); if(digitalRead(4) == HIGH){ inPin = cpuPaddleRight; } //end //drawpaddles(inPin, cpuPaddleRight); //(TV.vres() / 2) - halfpaddle); drawpaddles(movePaddle(PaddleL), movePaddle(PaddleR)); //rein KI drawBall(); if(lostball != 0){ lostGame(); pingponginit(); } //tone(frequency,duration); //TV.delay(20); /* if (Serial.available() > 0) { // read the incoming byte: int incomingByte = Serial.parseInt();//Serial.read(); // say what you got: Serial.print("I received: "); Serial.println(incomingByte, DEC); TV.tone(incomingByte,100); ///111 222 333 }*/ TV.delay_frame(1); }