Need some help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need some help

Okay so im trying to make a flappy bird like game but i cant get the background to change to black if theres any other errors please tell me how to fix those too thank you heres the code import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import javax.swing.JFrame; public class Flappy <BufferStrategy> extends Canvas implements Runnable{ public static final int WIDTH = 640,HEIGHT = 480; private boolean running = false; private Thread thread; public Flappy(){ Dimension d = new Dimension(Flappy.WIDTH,Flappy.HEIGHT); setPreferredSize(d); } public synchronized void start() { if(running) return; running = true; thread = new Thread(this); thread.start(); } public synchronized void stop() { if (!running) return; running = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args){ JFrame frame = new JFrame("Flappy"); Flappy flappy = new Flappy(); frame.add(flappy); frame.setResizable(false); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); flappy.start(); } @Override public void run() { int fps = 0; double timer = System.currentTimeMillis(); long lastTime = System.nanoTime(); double ns = 100000000 / 60; double delta = 0; while(running){ long now = System.nanoTime(); delta+= (now - lastTime)/ ns; lastTime = now; while(delta >= 1){ update(); render(); fps++; delta--; } if(System.currentTimeMillis() - timer >= 1000){ System.out.println("FPS: " + fps); fps = 0; timer+=1000; } } stop(); } private void render() { Object bs = getBufferStrategy(); if(bs == null) { createBufferStrategy(3); return; } Graphics g = ((j

6th Aug 2018, 1:29 AM
SarahDiamondFox
SarahDiamondFox - avatar
1 Answer
+ 1
heres the secconed part ava.awt.image.BufferStrategy) bs).getDrawGraphics(); g.setColor(Color.black); g.fillRect(0, 0, Flappy.WIDTH, Flappy.HEIGHT); g.dispose(); } private void update() { } }
6th Aug 2018, 1:29 AM
SarahDiamondFox
SarahDiamondFox - avatar