如何使对象看起来与Ruby on Rails中的海报不同?

时间:2016-03-02 00:24:58

标签: ruby-on-rails ruby

我正在开发一个项目,允许人们创建群组,然后将任务发送给所有与该群体不同的人。当匹配与任务附加值相同的user_id的人时,我希望任务显示不同(其上有不同的文本和其他一些东西),当您单击它时,它会显示该任务的分析。我只需要知道如何为发布它的人提取对象的更改版本。谢谢你的帮助。我可能在这里太模糊了,我对Rails还是很新。

1 个答案:

答案 0 :(得分:2)

如果某个任务属于某个用户,并且您可以访问current_user(如果您正在使用它内置的设计,w / bcrypt,则必须自己编写),在视图中执行此操作的简单方法是可以这样说:

package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;

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

public class Game extends Canvas implements Runnable, KeyListener {

    //declare values
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final String TITLE = "Space Shooter";

    private boolean running = false;
    private Thread thread;

    private Player player;

    private BufferedImage playerImage;
    private BufferedImage bulletImage;
    private BufferedImage enemyImage;

    int playerx;
    int playery;

    int round = 1;

    public Game() {
        //
        player = new Player((WIDTH/2)-32, HEIGHT-200); 

        //allocates all file resources
        try {
            playerImage = ImageIO.read(this.getClass().getResourceAsStream("/resources/player.png"));
            bulletImage = ImageIO.read(this.getClass().getResourceAsStream("/resources/bullet.png"));
            enemyImage = ImageIO.read(this.getClass().getResourceAsStream("/resources/enemy.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        addKeyListener(this);
        setFocusable(true);
        requestFocusInWindow();
    }

    //starts thread
    private synchronized void start() {
        if (running)
            return;

        running = true;
        thread = new Thread(this);
        thread.start();
    }

    //stops thread
    private synchronized void stop() {
        if (!running)
            return;

        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.exit(1);
    }

    @Override
    //game loop
    public void run() {     
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60.0;
        double ns  = 1000000000 / amountOfTicks;
        double delta = 0;
        int updates = 0;
        int frames = 0;
        long timer = System.currentTimeMillis();
        while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if (delta > 1) {
                tick();
                updates++;
                delta--;
            }
            Shoot.updateBullets();
            Enemy.updateEnemies();
            render();
            frames++;

            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println(updates + " TICKS, " + frames + " FPS");
                updates = 0;
                frames = 0;
            }
        }
        stop(); 
    }

    //updates sprite locations
    public void tick() {
        playerx = player.getX();
        playery = player.getY(); 
    }

    //renders sprites
    public void render() {
        //setting up triple-buffering
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(3);
            return;
        }
        Graphics g  = bs.getDrawGraphics();

        //////////////////////////////////

        g.setColor(Color.BLACK); g.fillRect(0,0,getWidth(), getHeight());
        g.drawImage(playerImage, playerx, playery, this); 

        if (Shoot.allBullets.size() != 0) {
            for (int i = 0; i < Shoot.allBullets.size(); i++) {
                int bulletx = (int) Shoot.allBullets.get(i).getX();
                int bullety = (int) Shoot.allBullets.get(i).getY(); 
                g.drawImage(bulletImage, bulletx + 21, bullety, this);
            }
        }

        if (Enemy.allEnemies.size() != 0) {
            for (int i = 0; i < Enemy.allEnemies.size(); i++) {
                int enemyx = (int) Enemy.allEnemies.get(i).getX();
                int enemyy = (int) Enemy.allEnemies.get(i).getY();
                g.drawImage(enemyImage, enemyx, enemyy, this);
            }
        } else {
            Enemy.createEnemies(round);
            round++;
        }

        //////////////////////////////////

        g.dispose();
        bs.show();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_SPACE) {
            Shoot.addBullet(player.getX(), player.getY());
        }
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_UP) {
            player.setY(playery -= 20);
        } else if (key == KeyEvent.VK_DOWN) {
            player.setY(playery += 20);
        } else if (key == KeyEvent.VK_RIGHT) {
            player.setX(playerx += 20);
        } else if (key == KeyEvent.VK_LEFT) {
            player.setX(playerx -= 20);
        } 
    }

    public void keyTyped(KeyEvent e) {}

    public static void main(String[] args) {

        Game game = new Game();
        JFrame frame = new JFrame(TITLE);

        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(game);
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setVisible(true); 

        game.start();

    }

}

假设你正在使用erb。但是你得到了要点。