Simple obstacle racing made with processing for Java

processing is recommended for programming beginners

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)

The game to make this time

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.

Code introduction

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

Simple obstacle racing made with processing for Java
Presentation slides made with Processing
Enable OpenCV with java8. (For myself)
Server processing with Java (Introduction part.1)
Run an application made with Java8 with Java6
Getting Started with Ruby for Java Engineers
Learn Java with "So What" [For beginners]
Interoperability tips with Kotlin for Java developers
Vending machine made with Java (domain driven)
I made a Diff tool for Java files
[Java] How to test for null with JUnit
Links & memos for getting started with Java (for myself)
Create a simple bulletin board with Java + MySQL
Ask for n business days later with JAVA
Use Java7 try-with-resources statement for Cursor close processing
A collection of simple questions for Java beginners
Docker Container Operations with Docker-Client API for Java
For JAVA learning (2018-03-16-01)
Java thread processing
2017 IDE for Java
Java string processing
[Java] Multi-thread processing
Christmas with Processing
Java for statement
[Java] Stream processing
java iterative processing
I made a simple calculation problem game in Java
A simple CRUD app made with Nuxt / Laravel (Docker)
List processing to understand with pictures --java8 stream / javaslang-
Make Calendar gadgets made with JavaFX compatible with Java SE 9
Java programming exercises for newcomers unpopular with active engineers
Implementation example of simple LISP processing system (Java version)
[Java basics] Let's make a triangle with a for statement
Get Azure App Service for Java Configuration with System.getEnv ()
Generate dummy data for various tests with Faker (java)
[Java] Environment construction procedure for developing struts 1.3 with Eclipse
I tried OCR processing a PDF file with Java
Try connecting to AzureCosmosDB Emulator for Docker with Java
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Face recognition app made with Amazon Rekognition and Java