【每天更新】新手做个java游戏历程
前几天学习了java基础课程,废话不说了,直接进入正题。代码如下:
package view;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GameStar extends Frame {
int bj_x=0;
//画图部分
Toolkit tk=Toolkit.getDefaultToolkit();
public void paint(Graphics g) {
//加载背景图片,
Image bj=tk.getImage(GameStar.class.getClassLoader().getResource("images/beijing005.gif"));
g.drawImage(bj,-bj_x,0,900,600,null);
g.drawImage(bj,900-bj_x,0,900,600,null);
bj_x+=10;
if (bj_x>900) {
bj_x=0;
}
}
public void Star(){
//窗口属性
this.setSize(900,600);
this.setTitle("o&o");
this.setVisible(true);
this.setResizable(false);
//窗口关闭
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//线程开启
MyThread mt=new MyThread();
mt.start();
}
public static void main(String[] args) {
GameStar gs=new GameStar();
gs.Star();
}
//多线程
class MyThread extends Thread{
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
PS:背景图片动态的,连续循环。问题是出现闪屏问题,有高手的话顺便给解决下