The aim of the 3 week assignment is to understand reasons to use classes as a part of object-oriented programming.
Inspiration
During the pandemic we all have to stay at home, but fortunately I travelled to campus and this inspired me to create a game, which is connected with sunsets, clouds and flights. Looking on the map we can really understand how big this world is and especially planes can help us to skip hundreds of kilometers in a short amount of time to see people from all over the world.
I have created a game, where you can manage plane trying to avoid clouds and not crash plane, at the same time you can collect coins. You can manage plane movement with 4 keys: “w” is to direct to the up, “a” to the left, “d” to right and “s” to the bottom. Keep in mind that plane is a fast moving object, so you should always control its position.
The game is not ideal, because physic collision doesn’t really work.
Code
Plane, clouds, coins are separated classes
The main part
Coin coins; Plane plane; Cloud clouds []; int Y_AXIS = 1; int X_AXIS = 2; color b1, b2, c1, c2; void setup(){ size(1280, 720); b1 = color(255); b2 = color(0); c1 = color(149, 187, 241); c2 = color(217, 136, 159); coins = new Coin(); plane = new Plane(); clouds = new Cloud[9]; for (int i=0; i<clouds.length; i++){ clouds[i] = new Cloud(); } } int posBallX=1150; int posBallY=300; void draw() { setGradient(0, 0, width, height, c1, c2, Y_AXIS); coins.runCoin(); plane.runPlane(); for (int i = 0; i<clouds.length; i++){ clouds[i].run(); } } void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) { noFill(); if (axis == Y_AXIS) { // Top to bottom gradient for (int i = y; i <= y+h; i++) { float inter = map(i, y, y+h, 0, 1); color c = lerpColor(c1, c2, inter); stroke(c); line(x, i, x+w, i); } } }
class Cloud { float posX, posY; color CColor; float acceleration; Cloud() { posX = random(width); posY = random(height); CColor = color (30, 35, 100) ; acceleration = random(2, 6); } void drawClouds() { fill(CColor); noStroke(); rectMode(CENTER); rect(posX+20, posY-1, 135, 70); fill(CColor); ellipse(posX-31, posY-10, 120, 90); ellipse(posX+90, posY-7, 90, 80); ellipse(posX+60, posY-17, 90, 100); ellipse(posX, posY-50, 80, 70); } void moveClouds() { posX += acceleration; } void checkEdge() { if (posX > width) { posX = 0; posY = random(0, height); } } void run() { moveClouds(); drawClouds(); checkEdge(); } }
Class for coin
class Coin { float sizeH; float posXC, posYC; float acceleration; Coin(){ posXC = 0; posYC = random(height); acceleration = 2; } void drawCoin(){ fill(255, 229, 15); stroke(255, 190, 0); ellipse(posXC, posYC, 40, 40); fill(255, 190, 0); ellipse (posXC, posYC, 25, 25); } void moveCoin() { posXC += acceleration; } void checkCoin() { if (posXC > width) { posXC = 0; posYC = random(0, height); } } void runCoin() { moveCoin(); drawCoin(); checkCoin(); } }
I also decided to put plane into separate class
class Plane { float posPlaneX; float posPlaneY; Plane() { posPlaneX = 1115; posPlaneY = 300; } void drawPlane() { fill(255); noStroke(); triangle(posPlaneX-70, posPlaneY, posPlaneX+30, posPlaneY-70,posPlaneX-20,posPlaneY); triangle(posPlaneX-70, posPlaneY, posPlaneX+30, posPlaneY+70,posPlaneX-20,posPlaneY); triangle(posPlaneX-70, posPlaneY, posPlaneX+30, posPlaneY+70,posPlaneX-20,posPlaneY); triangle(posPlaneX+45, posPlaneY,posPlaneX+95,posPlaneY-50, posPlaneX+75, posPlaneY); triangle(posPlaneX+45, posPlaneY,posPlaneX+95,posPlaneY+50, posPlaneX+75, posPlaneY); ellipse (posPlaneX, posPlaneY, 200, 30); } void keyPressed(){ if (key == 'w') posPlaneY-=10; if (key == 'a') posPlaneX-=10; if (key == 's') posPlaneY+=10; if (key == 'd') posPlaneX+=10; } void runPlane(){ keyPressed(); drawPlane(); } }
New skills
This week I continue to experiment with colors, that is why I added gradient on the background to represent beautiful sunsets in Abu Dhabi
Beginning
The idea of navigation came to my mind after some attempts to create something. At the beginning I wanted to make airstrip to land the plane or something like this. By the way, ball here is a future plane.