prossesing is an environment where you can create design art. The language is based on Java, but it can be written in other languages such as Python. Also, the structure of sentences is easier to see than others, so I want to make programming that works easily! Recommended for those who say. Here, I will introduce a simple game that I, a beginner of prossesing, could make in one day. Please note that the code may be difficult to read (sweat)
This time, I would like to make a game that avoids obstacles that are approaching. As a function, it can be created by jump / collision detection. Here, I will introduce the code and the simple flow. If you have any questions, please comment and we will answer.
I will introduce only a simple flow. This time I will run a stickman. First, download the necessary images and store the ones to be displayed repeatedly in the list.
Next, devise an update formula for y to make the stickman jump. The moment you click it, it has an upward speed, and it is dropped by applying a force that looks like gravity. I am doing a simple physics calculation.
Also, assuming that the obstacles that are approaching will come toward us at a constant speed, we have written an update formula for the position, where v is the speed and x is the position.
Finally, collision detection uses the AND operator (determines that a collision occurs when two conditions are met). The judgment condition is to stop the loop when the lower part of the image comes to a position below the obstacle.
I'll put the code below. Since the image uses the image on my PC, it will not work with direct copy and paste. Eight images with the movement of stick figures shifted and one image of Minister Kono are required. Lol
prossesing-for-java
int numFrames = 8; // The number of frames in the animation
int currentFrame = 0;
PImage[] img = new PImage[numFrames];
PImage img2;
float y,vy,Fy,x,z,vx;
void setup() {
frameRate(24);
size(600, 600);
y = 0;
vy = 0;
Fy = 0;
x = 570;
z = 570;
vx = 5;
// The image file must be in the data folder of the current sketch
// to load successfully
img2 = loadImage("kouno-tarou.jpg ");
img[0] = loadImage("stick0.png "); // Load the image into the program
img[1] = loadImage("stick1.png ");
img[2] = loadImage("stick2.png ");
img[3] = loadImage("stick3.png ");
img[4] = loadImage("stick4.png ");
img[5] = loadImage("stick5.png ");
img[6] = loadImage("stick6.png ");
img[7] = loadImage("stick7.png ");
}
void draw() {
background(255);
vy = vy +Fy;
y = y + vy;
x = x - vx;
if(y > 0){
vy = 0;
Fy = 0;
y = 0;
}
if(x < 0){
x = 570;
}
currentFrame = (currentFrame+1) % numFrames;
image(img[currentFrame], 0, 450 + y);
fill(0);
rect(x,z,30,30);
if ( ((0<x) && (x<100)) && (600+y > 570)){
fill(253);
rect(0,0,600,600);
image(img2,0,0);
noLoop();
}
}
void mouseClicked(){
Fy =1.2;
vy = -20;
}
Recommended Posts