Trouble with the visual output in draw()
in
Programming Questions
•
1 year ago
Hi,
My english isnt that fine, sorry for that ;)
Im trying to program a little game with processing. Its my first experience in programing, so my code is kind of ugly.
We havnt learned to use classes and stuff so far, so i tried to code it just by using the draw function.
The game starts with a countdown, counting from 3 to a random number. First part of the game is about reacting. After that the faster player have to aim at the opponent. If he hits, player 2 should loose a life and the countdown shall start again. So i thought, i could just write in the "hitOrNot" if-function, that the countdown shall start again (start=false). But it doesnt work.. the timer is working fine, but there is no visual output. Its working if i write println("1") instead of text("3", 400, 300); but thats not what i want... is there a way to do it, without changing all of my code? I know that its really confusing .. :S I hope u get what i mean =(
My english isnt that fine, sorry for that ;)
Im trying to program a little game with processing. Its my first experience in programing, so my code is kind of ugly.
We havnt learned to use classes and stuff so far, so i tried to code it just by using the draw function.
The game starts with a countdown, counting from 3 to a random number. First part of the game is about reacting. After that the faster player have to aim at the opponent. If he hits, player 2 should loose a life and the countdown shall start again. So i thought, i could just write in the "hitOrNot" if-function, that the countdown shall start again (start=false). But it doesnt work.. the timer is working fine, but there is no visual output. Its working if i write println("1") instead of text("3", 400, 300); but thats not what i want... is there a way to do it, without changing all of my code? I know that its really confusing .. :S I hope u get what i mean =(
int sec ;
int msec;
int cdLastNumber = int(random(7, 22));
int winner = 0;
boolean aiming = true;
boolean start;
boolean active = true;
boolean hitOrNot = true;
boolean horizontal = false;
boolean vertical = true;
float life1 = 3;
float life2 = 3;
float timey = 0;
float timex = 0;
float speedx = 1000;
float speedy = 1000;
float xmove;
float ymove;
float taste;
float savedTime = 0;
PImage ball;
float ballx = 52;
float bally = 46;
void setup() {
size(800, 600);
textSize(100);
textAlign(CENTER);
background(255);
ball = loadImage("ball.png");
}
void draw() {
background(255);
if (!start) {
sec = floor((millis()-msec)*.001-savedTime);
if (sec == cdLastNumber) start = true;
}
else
{
}
if (sec==1) {
fill(0);
text("3", 400, 300);
}
if (sec==2) {
text("2", 400, 300);
}
if (sec==3 && sec<cdLastNumber) {
text("1", 400, 300);
}
if (sec>=4 && sec<cdLastNumber) {
text("GET READY", 400, 300);
}
if (sec>=cdLastNumber) {
text("FEUER FREI", 400, 300);
active = false;
}
if (winner == 1) {
background(255);
active = true;
}
if (winner == 2) {
background(255);
active = true;
}
if (!aiming) {
rectMode(CORNERS);
rect(600, 50, 750, 250);
rect(50, 350, 200, 550);
if (!horizontal) {
timex = timex+speedx/frameRate;
xmove = timex;
fill(133);
image(ball, xmove, height/2);
if (xmove>=width-ballx)
{
speedx = -speedx;
}
if (xmove<=0)
{
speedx = -speedx;
}
}
if (!vertical) {
ymove=height/2;
timey = timey+speedy/frameRate;
ymove = timey;
fill(133);
image(ball, xmove, ymove);
if (ymove>=height-bally)
{
speedy = -speedy;
}
if (ymove<=0)
{
speedy = -speedy;
}
}
}
if (!hitOrNot) {
if (winner==1 && aiming== false && xmove>=600-ballx/2 && xmove<=750+ballx/2 && ymove>=50-bally/2 && ymove<=250+bally/2 )
{
life2 = life2-1;
text(life2,400,300);
aiming=true;
savedTime = floor((millis()-msec)*.001-savedTime);
start=false;
}
if (winner==2 && xmove>=50-ballx/2 && xmove<=200+ballx/2 && ymove>=350-bally/2 && ymove<=550+bally/2 )
{
life1 = life1-1;
println(life1);
aiming=true;
savedTime = floor((millis()-msec)*.001-savedTime);
start=false;
}
}
}
void keyPressed() {
if ((keyCode==taste) && vertical==false )
{
speedy=0;
hitOrNot = false;
}
if ((keyCode==taste) && aiming==false)
{
vertical=false;
horizontal=true;
background(255);
}
if (keyCode==UP && !active)
{
winner = 1;
aiming = false;
}
else if (keyCode==DOWN && !active)
{
winner = 2;
aiming = false;
}
if (winner==1) {
taste=UP;
}
if (winner==2) {
taste=DOWN;
}
}
1