Sphere on the board ! (Labyrinth game base)
in
Share your Work
•
2 years ago
This is base of the game I created as my first year project. I though it might be useful for people who are looking for similar codes !
Tilt the board by clicking and moving the mouse pointer on the screen, the sphere moves on the board, it might look a bit complicated to create such things but as you can see I have used very simple methods to implement it!
- import processing.opengl.*;
- float rotationX;
- float rotationY;
- float velocityX;
- float velocityY;
- float newvely;
- float newvelx;
- PVector pos;
- PVector vel;
- PVector grav;
- void setup() {
- size(800, 600, OPENGL);
- fill(255, 255);
- velocityX = 0;
- velocityY = 0;
- rotationX = 180;
- rotationY = 105;
- pos = new PVector (0, 0, 0);
- vel = new PVector (0, 0, 0);
- grav = new PVector (0,0,-0.8);
- }
- void draw() {
- background(0);
- lights();
- translate(width/2, height-230, 0);
- rotateX( radians(-rotationY) );
- rotateY( radians(rotationX) );
- rotationX -= velocityX;
- rotationY -= velocityY;
- velocityX *= 0.98;
- velocityY *= 0.98;
- if (mousePressed) {
- this.velocityX += (mouseX-pmouseX) * 0.007;
- this.velocityY -= (mouseY-pmouseY) * 0.007;
- }
- if (rotationX < 180) {
- newvelx = map(rotationX,180,160,0,-4.5);
- vel.x = newvelx;
- }
- else if (rotationX > 180) {
- newvelx = map(rotationX,180,200,0,4.5);
- vel.x = newvelx;
- }
- if (rotationY < 105) {
- newvely = map(rotationY,105,85,0,4.5);
- vel.y = newvely;
- }
- else if (rotationY > 105) {
- newvely = map(rotationY,105,125,0,-4.5);
- vel.y = newvely;
- }
- if ((pos.x > 215 || pos.x < -215) || (pos.y > 215 || pos.y < -215)) {
- vel.add(grav);
- if (pos.z < -1500) {
- rotationY = 105;
- rotationX = 180;
- velocityX = 0;
- velocityY = 0;
- vel.x = 0;
- vel.y = 0;
- vel.z = 0;
- pos.x = 0;
- pos.y = 0;
- pos.z = 0;
- pos.add(vel);
- }
- }
- pushMatrix();
- pos.add(vel);
- translate(pos.x, pos.y, pos.z+20);
- fill(255,0,0);
- noStroke();
- sphere(20);
- popMatrix();
- pushMatrix();
- fill(240);
- stroke(100);
- scale(210);
- beginShape(QUADS);
- vertex(-1, 1, 0);
- vertex( 1, 1, 0);
- vertex( 1, -1, 0);
- vertex(-1, -1, 0);
- vertex(-1, 1, -0.03);
- vertex( 1, 1, -0.03);
- vertex( 1, -1, -0.03);
- vertex(-1, -1, -0.03);
- vertex(-1, -1, 0);
- vertex( 1, -1, 0);
- vertex( 1, -1, -0.03);
- vertex(-1, -1, -0.03);
- vertex(-1, 1, -0.03);
- vertex( 1, 1, -0.03);
- vertex( 1, 1, 0);
- vertex(-1, 1, 0);
- vertex( -1, 1, 0);
- vertex( -1, -1, 0);
- vertex( -1, -1, -0.03);
- vertex( -1, 1, -0.03);
- vertex(1, -1, 0);
- vertex(1, 1, 0);
- vertex(1, 1, -0.03);
- vertex(1, -1, -0.03);
- endShape();
- popMatrix();
- }
If you are interested, the actual game can be downloaded from here
Screenshot !
Game play !
1