Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.147.48.212] |
|
Сообщ.
#1
,
|
|
|
Ja izuchaju Java po knigi i ushus v DIT.
Java Program Tabs.java //Another well-adjusted printing public class Tabs { public static void main(String[] args) { System.out.print("The are our top sellers:\n"); System.out.print("\tComputer games\n\tCoffee\n"); System.out.println("\tAspirin"); } } Добавлено Variable.java public class Variable { public static void main(String[] args) { int value; value = 5; System.out.print("The value is"); System.out.println(value); } } |
Сообщ.
#2
,
|
|
|
Budem delat igru
AIShip.java package ie.dit; import java.util.ArrayList; import processing.core.PApplet; import processing.core.PVector; public class AIShip extends GameObject { float size; private int health = 10; private ArrayList<PVector> waypoints = new ArrayList<PVector>(); public AIShip(YASC yasc, float x, float y, float speed, float size) { super(yasc, x, y, 0, 5); this.size = size; for(int i = 0 ; i < 5 ; i ++) { waypoints.add(new PVector(yasc.random(0, yasc.width), yasc.random(0, yasc.height))); } } public void render() { yasc.pushMatrix(); yasc.translate(pos.x, pos.y); yasc.rotate(rotation); yasc.stroke(0, 0, 255); float halfSize = size / 2; yasc.line(- halfSize, halfSize, 0, - halfSize); yasc.line(0, - halfSize , halfSize, halfSize); yasc.line(halfSize, halfSize ,0, 0); yasc.line(0, 0, -halfSize, halfSize); yasc.popMatrix(); for(int i = 1 ; i <= waypoints.size() ; i ++) { PVector a = waypoints.get(i - 1); PVector b = waypoints.get(i % waypoints.size()); yasc.stroke(255, 0, 0); yasc.line(a.x, a.y, b.x, b.y); } yasc.fill(0); yasc.text("Health:" + health, pos.x + 25, pos.y); } int current = 0; public void update() { PVector toNext = PVector.sub(waypoints.get(current), pos); float dist = toNext.mag(); toNext.normalize(); pos.add(toNext); rotation = (float) Math.atan2(toNext.y, toNext.x) + PApplet.HALF_PI; if (dist < 1) { current = (current + 1) % waypoints.size(); } } /** * @return the size */ public float getSize() { return size; } /** * @param size the size to set */ public void setSize(float size) { this.size = size; } /** * @return the health */ public int getHealth() { return health; } /** * @param health the health to set */ public void setHealth(int health) { this.health = health; } /** * @return the waypoints */ public ArrayList<PVector> getWaypoints() { return waypoints; } /** * @param waypoints the waypoints to set */ public void setWaypoints(ArrayList<PVector> waypoints) { this.waypoints = waypoints; } /** * @return the current */ public int getCurrent() { return current; } /** * @param current the current to set */ public void setCurrent(int current) { this.current = current; } } AmmoPowerup.java package ie.dit; import processing.core.PVector; public class AmmoPowerup extends GameObject implements Powerup { public AmmoPowerup(YASC yasc) { super(yasc, yasc.random(0, yasc.width) ,yasc.random(0, yasc.height) , 0, 2); forward.x = yasc.random(-1, 1); forward.y = yasc.random(-1, 1); forward.normalize(); } @Override public void update() { //pos =+ forward * speed; pos.add(PVector.mult(forward, speed)); rotation += 0.01f; if (pos.x < 0) { pos.x = yasc.width; } if (pos.x > yasc.width) { pos.x = 0; } if (pos.y < 0) { pos.y = yasc.height; } if (pos.y > yasc.height) { pos.y = 0; } } int size = 20; @Override public void render() { yasc.pushMatrix(); yasc.translate(pos.x, pos.y); yasc.rotate(rotation); yasc.rect(-size / 2, -size / 2, size, size); yasc.popMatrix(); } @Override public void applyTo(Ship s) { s.setAmmo(s.getAmmo() + 10); } } Bullet.java package ie.dit; import processing.core.PVector; public class Bullet extends GameObject { public Bullet(YASC yasc, float x, float y, float rotation) { super(yasc, x, y, rotation, 5); } public void render() { yasc.pushMatrix(); yasc.translate(pos.x, pos.y); yasc.rotate(rotation); yasc.line(0, -5, 0, 5); yasc.popMatrix(); } public void checkCollisions() { float dist = PVector.dist(yasc.aiShip.getPos(), pos); if (dist < yasc.aiShip.size / 2) { yasc.aiShip.setHealth(yasc.aiShip.getHealth() - 1); yasc.gameObjects.remove(this); } } public void update() { // static methods on the Math class forward.x = (float)Math.sin(rotation); forward.y = - (float)Math.cos(rotation); // pos += forward * speed pos.add(PVector.mult(forward, speed)); if (pos.x < 0) { pos.x = yasc.width; } if (pos.x > yasc.width) { pos.x = 0; } if (pos.y < 0) { pos.y = yasc.height; } if (pos.y > yasc.height) { pos.y = 0; } alive += yasc.timeDelta; if (alive >= 5.0) { yasc.gameObjects.remove(this); } checkCollisions(); } float alive; } GameObject.java package ie.dit; import processing.core.PVector; public abstract class GameObject { protected PVector pos; protected PVector forward; protected float rotation; protected float speed; protected YASC yasc; public GameObject(YASC yasc, float x, float y, float rotation, float speed) { this.yasc = yasc; pos = new PVector(x, y); forward = new PVector(0, -1); this.rotation = rotation; this.speed = speed; } public abstract void update(); public abstract void render(); /** * @return the pos */ public PVector getPos() { return pos; } /** * @param pos the pos to set */ public void setPos(PVector pos) { this.pos = pos; } /** * @return the forward */ public PVector getForward() { return forward; } /** * @param forward the forward to set */ public void setForward(PVector forward) { this.forward = forward; } /** * @return the rotation */ public float getRotation() { return rotation; } /** * @param rotation the rotation to set */ public void setRotation(float rotation) { this.rotation = rotation; } /** * @return the speed */ public float getSpeed() { return speed; } /** * @param speed the speed to set */ public void setSpeed(float speed) { this.speed = speed; } /** * @return the yasc */ public YASC getYasc() { return yasc; } /** * @param yasc the yasc to set */ public void setYasc(YASC yasc) { this.yasc = yasc; } } Mian.java package ie.dit; import processing.core.PVector; public class Main { public void yasc() { String[] a = {"MAIN"}; processing.core.PApplet.runSketch( a, new YASC()); } public static void main(String[] args) { PVector a = new PVector(0, 5); PVector b = new PVector(10, -2); PVector c = new PVector(); // a += b; a.add(b); // c = a + b; c = PVector.add(a, b); // static method call, on the class not the instance c.normalize(); c.mag(); Main main = new Main(); main.yasc(); } } powerup.java package ie.dit; public interface Powerup { public void applyTo(Ship s); } Ship.java package ie.dit; import processing.core.PVector; public class Ship extends GameObject { private float size; public int fireRate; private float toPass; private float ellapsed; private int ammo; public Ship(YASC yasc, float x, float y, float speed, float size) { super(yasc, x, y, 0, speed); this.size = size; fireRate = 20; toPass = 1 / (float) fireRate; ammo = 10; } public void render() { yasc.pushMatrix(); yasc.translate(pos.x, pos.y); yasc.rotate(rotation); float halfSize = size / 2; yasc.stroke(0); yasc.line(- halfSize, halfSize, 0, - halfSize); yasc.line(0, - halfSize , halfSize, halfSize); yasc.line(halfSize, halfSize ,0, 0); yasc.line(0, 0, -halfSize, halfSize); yasc.popMatrix(); yasc.text("Ammo: " + ammo, pos.x + 20, pos.y); } public void update() { forward.x = (float) Math.sin(rotation); forward.y = - (float) Math.cos(rotation); if (yasc.checkKey('w')) { pos.add(forward); } if (yasc.checkKey('s')) { pos.x -= forward.x; pos.y -= forward.y; } if (yasc.checkKey('a')) { rotation -= 0.1f; } if (yasc.checkKey('d')) { rotation += 0.1f; } if (yasc.checkKey(' ') && ellapsed >= toPass && ammo > 0) { PVector spawnPoint = PVector.add(pos, PVector.mult(forward, 25)); Bullet b = new Bullet(yasc, spawnPoint.x, spawnPoint.y, rotation + yasc.random(-0.1f, 0.1f)); yasc.gameObjects.add(b); ellapsed = 0; ammo --; } ellapsed += yasc.timeDelta; yasc.text("Ellapsed: "+ ellapsed, 10, 200); } public void setSize(float size) { this.size = size; } /** * @return the size */ public float getSize() { return size; } /** * @return the fireRate */ public int getFireRate() { return fireRate; } /** * @param fireRate the fireRate to set */ public void setFireRate(int fireRate) { this.fireRate = fireRate; } /** * @return the toPass */ public float getToPass() { return toPass; } /** * @param toPass the toPass to set */ public void setToPass(float toPass) { this.toPass = toPass; } /** * @return the ellapsed */ public float getEllapsed() { return ellapsed; } /** * @param ellapsed the ellapsed to set */ public void setEllapsed(float ellapsed) { this.ellapsed = ellapsed; } /** * @return the ammo */ public int getAmmo() { return ammo; } /** * @param ammo the ammo to set */ public void setAmmo(int ammo) { this.ammo = ammo; } } Yack.java package ie.dit; import java.util.ArrayList; import processing.core.PApplet; public class YASC extends PApplet { boolean[] keys = new boolean[1024]; public ArrayList<GameObject> gameObjects = new ArrayList<GameObject>(); AIShip aiShip; Ship ship; public void keyPressed() { keys[keyCode] = true; } public void keyReleased() { keys[keyCode] = false; } public boolean checkKey(int c) { return keys[c] || keys [Character.toUpperCase(c)]; } public void settings() { size(500, 500); } public void setup() { ship = new Ship(this, width / 2, height / 2, 5, 50); gameObjects.add(ship); aiShip = new AIShip(this, 100, 100, 5, 50); gameObjects.add(aiShip); gameObjects.add(new AmmoPowerup(this)); gameObjects.add(new AmmoPowerup(this)); } public float timeDelta; private float last; public void draw() { float now = millis(); timeDelta = (now - last) / 1000.0f; last = now; background(255); fill(0); text("GameObjects: " + gameObjects.size(), 50, 100); for(int i= gameObjects.size() - 1; i >= 0; i--) { GameObject b = gameObjects.get(i); b.render(); b.update(); if (b instanceof Powerup) { if (dist(b.pos.x, b.pos.y, ship.pos.x, ship.pos.y) < 50) { ((Powerup)b).applyTo(ship); gameObjects.remove(b); } } } } } |