-1
the code
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
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 javax.swing.JFrame;
public class Game extends Canvas implements Runnable,KeyListener{
private static final long serialVersionUID = 1L;
public static int WIDTH = 240;
public static int HEIGHT = 120;
public static int SCALE = 3;
public BufferedImage layer = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
public Player player;
public Game(){
this.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
this.addKeyListener(this);
player = new Player(100,HEIGHT-10);
}
public static void main(String[] args){
Game game = new Game();
JFrame frame = new JFrame("Pong");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new Thread(game).start();
}
public void tick(){
player.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = layer.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
player.render(g);
g = bs.getDrawGraphics();
g.drawImage(layer,0,0,WIDTH*SCALE,HEIGHT*SCALE,null);
bs.show();
}
public void run() {
while(true){
try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
e.printStackTrace();
}
tick();
render(); }
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
player.right(true);
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT){
player.left(true);
}
}
public void keyReleased(KeyEvent e) {
}
}
and the player code:
import java.awt.Color;
import java.awt.Graphics;
public class Player {
public boolean right,left;
public int x,y;
public Player(int x,int y){
this.x = x;
this.y = y;
}
public void tick(){
if(right)
{
x++;
}
else if(left){
x--;
}
}
public void render(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(x,y,40,10);
}
}
the mistakes:
Exception in thread "AWT-Eventqueue-0" java.lang.Error: Unresolved Compilation problems: The method right(Boolean) is Undefined for the type Player The method left(Boolean) is Undefined for the type Player
at pong.Game.keyPressed(Game.java:79)
at java.desktop/java.awt.Component.processKeyEvent(Component.java:6590)
at java.desktop/java.awt.Component.processEvent(Component.java:6409)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:871)
at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1140)
at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1010)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:836)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
how do I proceed?
didn’t work :(
– nightmareNBA
Could you report the error? I copied your code and tested it here on mine and it worked perfectly. Just try to copy my two functions and replace them with yours.
– Danilo Lima
my friend I am a tapir thank you
– nightmareNBA
For nothing, I could mark it as the right answer?
– Danilo Lima