Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import javax.swing.JFrame;
- public class Game { // ãëàâíûé êëàññ
- public static void main(String[] args) throws IOException {
- JFrame frame = new JFrame("GetFruit");
- frame.setLayout(null);
- int standX = 250;
- int standY = 350;
- Stand stand = new Stand();
- stand.setBounds(standX,standY,100,100);
- int potatoX = (int)(Math.random() * 486); // ãåíåðèðóåì ñëó÷àéíûé x
- int potatoY = 100;
- Potato potato = new Potato();
- potato.setBounds(potatoX,potatoY,50,50);
- frame.add(stand).setVisible(true);
- frame.add(potato).setVisible(true);
- KeyboardObserver observer = new KeyboardObserver(stand);
- frame.addKeyListener(observer);
- frame.setSize(700,500);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setVisible(true);
- }
- }
- =================================================
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- public class KeyboardObserver extends KeyAdapter { // Îòñëåæèâàåò íàæàòèÿ êëàâèø
- private Stand stand;
- public KeyboardObserver(Stand stand) {
- this.stand = stand;
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_LEFT){
- stand.setDirection(StandDirection.LEFT);
- stand.setLocation(stand.getX()-10, stand.getY());
- }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- stand.setDirection(StandDirection.RIGHT);
- stand.setLocation(stand.getX()+10, stand.getY());
- }
- }
- }
- =================================================
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JPanel;
- public class Potato extends JPanel{ // Êàðòîøêà
- private BufferedImage image;
- public Potato(){
- try {
- image = ImageIO.read(new File("src\\images\\potato.jpg"));
- } catch (IOException e) {
- System.out.println("Íåâåðíûé ïóòü ê èçîáðàæåíèþ.");
- }
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(image,0,0,null);
- }
- }
- =================================================
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JPanel;
- public class Stand extends JPanel { // Ïîäñòàâêà
- private StandDirection direction;
- private BufferedImage image;
- public Stand(){
- try {
- image = ImageIO.read(new File("src\\images\\p.jpg"));
- } catch (IOException e) {
- System.out.println("Íåâåðíûé ïóòü ê èçîáðàæåíèþ.");
- }
- }
- public void setDirection(StandDirection direction) {
- this.direction = direction;
- }
- public StandDirection getDirection() {
- return direction;
- }
- // public void draw(){
- // if (this.getDirection() == StandDirection.LEFT) {
- // x -= 25;
- // if(this.x <0 ){
- // x+= 25;
- // }
- // } else if (this.getDirection() == StandDirection.RIGHT) {
- // x += 25;
- // if(this.x > 495){
- // x -= 25;
- // }
- // }
- // }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- // draw();
- g.drawImage(image, 0, 0, null);
- }
- }
- =================================================
- public enum StandDirection { // enum ñî çíà÷åíèÿìè ïîäñòàâêè
- LEFT,
- RIGHT
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement