本帖最后由 dongyangzhang 于 2013-4-15 18:05 编辑
- 自己参考书上仿照着写的双人赛车游戏,刷新频率实在是太低了,屏幕闪烁的晃眼,求助怎么解决重绘游戏屏幕是闪烁的问题?
- package S1P9;
- //导入所需要的软件包
- import javax.swing.*;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.event.*;
- //创建的类S1P9继承于JFrame
- public class S1P9 extends JFrame {
- /**
- * @param args
- */
- //通过调用构造方法运行该程序
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new S1P9();
- }
- //设置游戏屏幕大小
- final int WIDTH = 900, HEIGHT = 650;
- double p1Speed=.5,p2Speed=.5;
- Rectangle p1=new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);
- //下面的矩形代表第二辆赛车
- Rectangle p2=new Rectangle(((WIDTH/9)+(int)(((WIDTH/9)*1.5)/2)),(HEIGHT/2)+(HEIGHT/10),WIDTH/30,WIDTH/30);
- /*下面是待绘制的矩形*/
- /*
- *创建矩形的代码起初看起来有点复杂,但是各个矩形的创建大同小异,它们的大小都是取决于WIDTH和HEIGHT的属性值
- */
- //下面的矩形分别表示赛场的左右上下部分,以及赛场中央区域
- Rectangle left=new Rectangle(0,0,WIDTH/9,HEIGHT);
- Rectangle right=new Rectangle((WIDTH/9)*9,0,WIDTH/9,HEIGHT);
- Rectangle top=new Rectangle(0,0,WIDTH,HEIGHT/9);
- Rectangle bottom=new Rectangle(0,HEIGHT/9*9,(WIDTH/9)*10,HEIGHT/9);
- Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*5),(int)((HEIGHT/9)*4));
- //下面的矩形用于绘制跑道上的障碍物,以增加赛车的难度
- Rectangle obstacle=new Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),WIDTH/10,HEIGHT/9);
- Rectangle obstacle2=new Rectangle(WIDTH/3,(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
- Rectangle obstacle3=new Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
- Rectangle obstacle4=new Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);
- Rectangle obstacle5=new Rectangle(WIDTH/2,(int)((HEIGHT/9*1.5)),WIDTH/30,HEIGHT/4);
- //下面的矩形用于绘制内外跑道的终点线
- Rectangle finish=new Rectangle(WIDTH/9,(HEIGHT/2)-(HEIGHT/9),(int)((WIDTH/9)*1.5),(HEIGHT/70));
- //下面的矩形用于绘制外跑道的起跑线
- Rectangle line0=new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
- //下面的矩形用于绘制内跑道的起跑线
- Rectangle line1=new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+(HEIGHT/10),(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
- //构造方法
- public S1P9(){
- //使用下列代码设置JFrame
- super("Radical Racing");
- setSize(WIDTH/9*10,HEIGHT/9*10);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- Move1 m1=new Move1();
- Move2 m2=new Move2();
- m1.start();
- m2.start();
- }
- private class Move1 extends Thread{
- public void run(){
- //通过永久循环语句让赛车1不断向前行驶
- while(true){
- //用try...catch语句捕捉异常,如果运行出错,退出循环
- try{
- //刷新游戏屏幕
- repaint(0);
- //赛车行驶中加速
- if(p1Speed<=5){
- p1Speed+=.2;
- }
- p1.y-=p1Speed;
- //设置每次加速的间隔时间
- Thread.sleep(75);
- }catch(Exception e){
- //如果程序运行出错,那么退出循环
- break;
- }
- }
- }
- }
- private class Move2 extends Thread{
- public void run(){
- //通过永久循环语句让赛车2不断向前行驶
- while(true){
- //用try...catch语句捕捉异常,如果运行出错,退出循环
- try{
- //刷新游戏屏幕
- repaint(0);
- //赛车行驶中加速
- if(p2Speed<=5){
- p2Speed+=.2;
- }
- p2.y-=p2Speed;
- //设置每次加速的间隔时间
- Thread.sleep(75);
- }catch(Exception e){
- //如果程序运行出错,那么退出循环
- break;
- }
- }
- }
- }
- //该方法用于绘制赛车和赛场
- public void paint(Graphics g){
- super.paint(g);
- //绘制赛场背景
- g.setColor(Color.DARK_GRAY);
- g.fillRect(0, 0, WIDTH, HEIGHT);
- //绘制时,先将场地设置为绿色
- g.setColor(Color.GREEN);
- //现在绘制各个矩形
- g.fillRect(left.x, left.y, left.width, left.height);
- g.fillRect(right.x, right.y, right.width, right.height);
- g.fillRect(top.x, top.y, top.width, top.height);
- g.fillRect(bottom.x, bottom.y, bottom.width, bottom.height);
- g.fillRect(center.x, center.y, center.width, center.height);
- g.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
- g.fillRect(obstacle2.x, obstacle2.y, obstacle2.width, obstacle2.height);
- g.fillRect(obstacle3.x, obstacle3.y, obstacle3.width, obstacle3.height);
- g.fillRect(obstacle4.x, obstacle4.y, obstacle4.width, obstacle4.height);
- g.fillRect(obstacle5.x, obstacle5.y, obstacle5.width, obstacle5.height);
- //设置起跑线为白色
- g.setColor(Color.WHITE);
- //绘制起跑线
- g.fillRect(line0.x, line0.y, line0.width, line0.height);
- g.fillRect(line1.x, line1.y, line1.width, line1.height);
- //设置终点线为黄色
- g.setColor(Color.YELLOW);
- //绘制终点线
- g.fillRect(finish.x, finish.y, finish.width, finish.height);
-
-
- g.setColor(Color.RED);
- //绘制赛车1
- g.fill3DRect(p1.x, p1.y, p1.width, p1.height,true);
- //设置赛车2为黑色
- g.setColor(Color.BLACK);
- //绘制赛车2
- g.fill3DRect(p2.x, p2.y, p2.width, p2.height, true);
- }
- }
- <p> </p>
复制代码 |