Need some help with a collision type problem in this very simple game!
in
Programming Questions
•
3 years ago
Simple Game, i want the ballto bounce of the rectangle, but for some reason(most likely obvious) it just floats pass :( here is the code and thank you for your help!!
Astrophysics!
Harrison.
- float x = 400; // postion of the player
- float y = 193; //postion of player
- float speed = 10; // movement of player speed
- int size = 20; // Width of the shape
- float xpos, ypos; // Starting position of shape
- float xspeed = 2.8; // Speed of the shape
- float yspeed = 2.2; // Speed of the shape
- int xdirection = 1; // Left or Right
- int ydirection = 1; // Top to Bottom
- int score = 50;
- void setup()
- {
- size(640, 200);
- noStroke();
- frameRate(30);
- smooth();
- // Set the starting position of the shape
- xpos = width/2;
- ypos = height/2;
- }
- void draw()
- {
- background(102);
- moveright();
- moveleft();
- display();
- collosions();
- // Update the position of the shape
- // xpos = xpos + ( xspeed * xdirection );
- // ypos = ypos + ( yspeed * ydirection );
- // Test to see if the shape exceeds the boundaries of the screen
- // If it does, reverse its direction by multiplying by -1
- if (xpos > width-size || xpos < 0) {
- xdirection *= -1;
- }
- if (ypos > height-size) {
- // ydirection *= -1;
- score += 1;
- xspeed += 0.1;
- yspeed += 0.1;
- }
- if (score == 100) {
- print("you win!");
- exit();
- }
- if ( ypos < 0) {
- ydirection *= -1;
- score -= 1;
- }
- if (score == 0){
- print("game over");
- exit();
- }
- // Draw the shape
- float col= random(255);
- float col2 = random(255);
- float col3 = random(255);
- fill(col, col2, col3);
- ellipse(xpos+size/2, ypos+size/2, size, size);
- print(score);
- }
- void moveright() {
- if (keyPressed) {
- if (key == 'd'){
- x = x+speed;
- }
- }
- if ((x + 20) < 0) {
- x = 600;
- }
- }
- void moveleft() {
- if (keyPressed){
- if (key == 'a'){
- x = x - speed;
- }
- }
- if ((x + 20) > 640){
- x = 40;
- }
- }
- void display() {
- fill(255);
- rect(x,y,40,7);
- }
- void collosions() {
- xpos = xpos + ( xspeed * xdirection );
- ypos = ypos + ( yspeed * ydirection );
- if(xpos == x){
- ydirection *= -1;
- }
- }
Astrophysics!
1