Java

покажите полный код самой малой игры на java

import java.util.Random;
import java.util.Scanner;
class number {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
Random r = new Random();
int x = r.nextInt(100) + 1, z, counter = 0;
System.out.print("Угадайте число! \n");
do {
System.out.print("Введите число, попыток - " + (7-counter) + ": ");
z = scn.nextInt();
if (z > x)
System.out.print("Много\n");
if (z < x)
System.out.print("Мало\n");
counter++;
}
while ((z != x) && (counter < 7));
if (z == x)
System.out.print("Вы выйграли! ");
else
System.out.print("Вы проиграли! ");
}
}
Николай Чугай
Николай Чугай
8 005
Giorgi Gogua спасибо
package org.tides.tutorial;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;

private boolean running;

public static int WIDTH = 400;
public static int HEIGHT = 300;
public static String NAME = "TUTORIAL 1";

public static Sprite hero;

public void start() {
running = true;
new Thread(this).start();
}

public void run() {
long lastTime = System.currentTimeMillis();
long delta;

init();

while(running) {
delta = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
render();
update(delta);
}
}

public void init() {
hero = getSprite("man.png");
}

public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
requestFocus();
return;
}

Graphics g = bs.getDrawGraphics(); //получаем Graphics из созданной нами BufferStrategy
g.setColor(Color.black); //выбрать цвет
g.fillRect(0, 0, getWidth(), getHeight()); //заполнить прямоугольник
hero.draw(g, 20, 20);
g.dispose();
bs.show(); //показать
}

public void update(long delta) {

}

public Sprite getSprite(String path) {
BufferedImage sourceImage = null;

try {
URL url = this.getClass().getClassLoader().getResource(path);
sourceImage = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}

Sprite sprite = new Sprite(Toolkit.getDefaultToolkit().createImage(sourceImage.getSource()));

return sprite;
}

public static void main(String[] args) {
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
JFrame frame = new JFrame(Game.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
}
FIELD_SIZE = 20;
SNAKE_SPEED = 100;

window.onload = startGame();
window.addEventListener("keydown", changeDirection, false);
window.addEventListener("touchstart", touchCatched, false);
window.addEventListener("touchmove", touchEnded, false);

function touchCatched(event) {
touch = event;
}

function touchEnded(event) {
var offsetX = touch.changedTouches[0].clientX - event.changedTouches[0].clientX;
var offsetY = touch.changedTouches[0].clientY - event.changedTouches[0].clientY;

var rule = new Object();

if (Math.abs(offsetX) > Math.abs(offsetY)) {
if (offsetX < 0) {
rule.keyCode = 39;
} else {
rule.keyCode = 37;
}
} else {
if (offsetY < 0) {
rule.keyCode = 40;
} else {
rule.keyCode = 38;
}
}
changeDirection(rule);
}

function spawnSnake() {
for (var i = FIELD_SIZE - 3; i < FIELD_SIZE; i++) {
field.children[FIELD_SIZE - 1].children[i].className = "snake-cell";
}

snake = field.children[FIELD_SIZE - 1].children[FIELD_SIZE - 3];
snake.tail = field.children[FIELD_SIZE - 1].children[FIELD_SIZE - 2];
snake.tail.tail = field.children[FIELD_SIZE - 1].children[FIELD_SIZE - 1];

snake.direction = "left";
return snake;
}

function Cell() {
var cell = document.createElement("div");
cell.className = "cell";
return cell;
}

function Row(length) {
if(!length) {
alert("Length of row is null!"); /* DEBUG */
}

var row = document.createElement("div");
row.className = "row";

for (var i = 0; i < length; i++) {
row.appendChild(new Cell());
}
return row;
}

function Field(size) {
if(!size) {
alert("Size of field is null!"); /* DEBUG */
}

var field = document.createElement("div");
field.className = "field";
field.spawnSnake = spawnSnake;

for (var i = 0; i < size; i++) {
field.appendChild(new Row(size));
}

for (var i = 0; i < FIELD_SIZE; i++) {
for (var j = 0; j < FIELD_SIZE; j++) {
cell = field.children[i].children[j];
if (i == 0) {
cell.up = null;
} else {
cell.up = field.children[i-1].children[j];
}
if (j == 0) {
cell.left = null;
} else {
cell.left = field.children[i].children[j-1];
}
if (j == FIELD_SIZE - 1) {
cell.right = null;
} else {
cell.right = field.children[i].children[j+1];
}
if (i == FIELD_SIZE - 1) {
cell.down = null;
} else {
cell.down = field.children[i+1].children[j];
}
}
}
return field;
}

function startGame() {
field = Field(FIELD_SIZE);

var div = document.getElementsByTagName("div")[1];
div.appendChild(field);

snake = field.spawnSnake();

points = document.getElementsByTagName("div")[0];
points.textContent = 0;

spawnFood();
intervalID = window.setInterval(snakeMotion, SNAKE_SPEED);
}

/* up: 38; down:40; left: 37; right: 39*/
function changeDirection(event) {
if (event.keyCode == 38) {
if (snake.direction == "left" || snake.direction == "right") {
snake.direction = "up";
}
}
if (event.keyCode == 40) {
if (snake.direction == "left" || snake.direction == "right") {
snake.direction = "down";
}
}
if (event.keyCode == 37) {
if (snake.direction == "up" || snake.direction == "down") {
snake.direction = "left";
}
}
if (event.keyCode == 39) {
if (snake.direction == "up" || snake.direction == "down") {
snake.direction = "right";
}
}
}

function spawnFood() {
var food;
do {
food = field.children[Math.floor(Math.random() * FIELD_SIZE)].children[Math.floor(Math.random() * FIELD_SIZE)];
} while (food.className == "snake-cell");
food.eat = true;
food.style.background = "white";
}

function snakeMotion() {
var nextCell = snake[snake.direction];
if (nextCell && (nextCell.className != "snake-cell")) {
nextCell.direction = snake.direction;
nextCell.tail = snake;
if (nextCell.eat) {
spawnFood();
points.textContent++;
Mihhail Taranov Это JavaScript, а не Java.
import java.io.*;
// This is a simple program called HelloWorld.java
class HelloWorld
{
public static void main(String args[ ])
{
System.out.println( Welcome to the world of Java );
}
}
SA
Sherxon Ashurov
224
Дмитрий Ширяев Есть ошибка в строке
System.out.println( Welcome to the world of Java );
Надо поставить ковычки в скобках перед Welcome и после Java