赛车_e35555564

#include<iostream>
#include<conio.h>
#include<dos.h> 
#include <windows.h>
#include <time.h>

#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70 

using namespace std; 

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

int enemyY[3];
int enemyX[3];
int enemyFlag[3];
char car[4][4] = { ' ','±','±',' ', 
                    '±','±','±','±', 
                    ' ','±','±',' ',
                    '±','±','±','±' }; 

int carPos = WIN_WIDTH/2;
int score = 0; 

void gotoxy(int x, int y){
    CursorPosition.X = x;
    CursorPosition.Y = y;
    SetConsoleCursorPosition(console, CursorPosition);
}
void setcursor(bool visible, DWORD size) {
    if(size == 0)
        size = 20;  

    CONSOLE_CURSOR_INFO lpCursor;   
    lpCursor.bVisible = visible;
    lpCursor.dwSize = size;
    SetConsoleCursorInfo(console,&lpCursor);
}
void drawBorder(){  
    for(int i=0; i<SCREEN_HEIGHT; i++){
        for(int j=0; j<17; j++){
            gotoxy(0+j,i); cout<<"±";
            gotoxy(WIN_WIDTH-j,i); cout<<"±";
        }
    } 
    for(int i=0; i<SCREEN_HEIGHT; i++){
        gotoxy(SCREEN_WIDTH,i); cout<<"±";
    } 
}
void genEnemy(int ind){
    enemyX[ind] = 17 + rand()%(33);  
}
void drawEnemy(int ind){
    if( enemyFlag[ind] == true ){
        gotoxy(enemyX[ind], enemyY[ind]);   cout<<"****";  
        gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ** "; 
        gotoxy(enemyX[ind], enemyY[ind]+2); cout<<"****"; 
        gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ** ";  
    } 
}
void eraseEnemy(int ind){
    if( enemyFlag[ind] == true ){
        gotoxy(enemyX[ind], enemyY[ind]); cout<<"    ";  
        gotoxy(enemyX[ind], enemyY[ind]+1); cout<<"    "; 
        gotoxy(enemyX[ind], enemyY[ind]+2); cout<<"    "; 
        gotoxy(enemyX[ind], enemyY[ind]+3); cout<<"    "; 
    } 
}
void resetEnemy(int ind){
    eraseEnemy(ind);
    enemyY[ind] = 1;
    genEnemy(ind);
}

void drawCar(){
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            gotoxy(j+carPos, i+22); cout<<car[i][j];
        }
    }
}
void eraseCar(){
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            gotoxy(j+carPos, i+22); cout<<" ";
        }
    }
}

int collision(){
    if( enemyY[0]+4 >= 23 ){
        if( enemyX[0] + 4 - carPos >= 0 && enemyX[0] + 4 - carPos < 9  ){
            return 1;
        }
    }
    return 0;
} 
void gameover(){
    system("cls");
    cout<<endl;
    cout<<"\t\t--------------------------"<<endl;
    cout<<"\t\t-------- Game Over -------"<<endl;
    cout<<"\t\t--------------------------"<<endl<<endl;
    cout<<"\t\tPress any key to go back to menu.";
    getch();
}
void updateScore(){
    gotoxy(WIN_WIDTH + 7, 5);cout<<"Score: "<<score<<endl;
}

void instructions(){

    system("cls");
    cout<<"Instructions";
    cout<<"\n----------------";
    cout<<"\n Avoid Cars by moving left or right. ";
    cout<<"\n\n Press 'a' to move left";
    cout<<"\n Press 'd' to move right";
    cout<<"\n Press 'escape' to exit";
    cout<<"\n\nPress any key to go back to menu";
    getch();
}

void play(){
    carPos = -1 + WIN_WIDTH/2;
    score = 0;
    enemyFlag[0] = 1;
    enemyFlag[1] = 0;
    enemyY[0] = enemyY[1] = 1;

    system("cls"); 
    drawBorder(); 
    updateScore();
    genEnemy(0);
    genEnemy(1);

    gotoxy(WIN_WIDTH + 7, 2);cout<<"Car Game";
    gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
    gotoxy(WIN_WIDTH + 6, 6);cout<<"----------";
    gotoxy(WIN_WIDTH + 7, 12);cout<<"Control ";
    gotoxy(WIN_WIDTH + 7, 13);cout<<"-------- ";
    gotoxy(WIN_WIDTH + 2, 14);cout<<" A Key - Left";
    gotoxy(WIN_WIDTH + 2, 15);cout<<" D Key - Right"; 

    gotoxy(18, 5);cout<<"Press any key to start";
    getch();
    gotoxy(18, 5);cout<<"                      ";

    while(1){
        if(kbhit()){
            char ch = getch();
            if( ch=='a' || ch=='A' ){
                if( carPos > 18 )
                    carPos -= 4;
            }
            if( ch=='d' || ch=='D' ){
                if( carPos < 50 )
                    carPos += 4;
            } 
            if(ch==27){
                break;
            }
        } 

        drawCar(); 
        drawEnemy(0); 
        drawEnemy(1); 
        if( collision() == 1  ){
            gameover();
            return;
        } 
        Sleep(50);
        eraseCar();
        eraseEnemy(0);
        eraseEnemy(1);   

        if( enemyY[0] == 10 )
            if( enemyFlag[1] == 0 )
                enemyFlag[1] = 1;

        if( enemyFlag[0] == 1 )
            enemyY[0] += 1;

        if( enemyFlag[1] == 1 )
            enemyY[1] += 1;

        if( enemyY[0] > SCREEN_HEIGHT-4 ){
            resetEnemy(0);
            score++;
            updateScore();
        }
        if( enemyY[1] > SCREEN_HEIGHT-4 ){
            resetEnemy(1);
            score++;
            updateScore();
        }
    }
}

int main()
{
    setcursor(0,0); 
    srand( (unsigned)time(NULL)); 

    do{
        system("cls");
        gotoxy(10,5); cout<<" -------------------------- "; 
        gotoxy(10,6); cout<<" |        Car Game        | "; 
        gotoxy(10,7); cout<<" --------------------------";
        gotoxy(10,9); cout<<"1. Start Game";
        gotoxy(10,10); cout<<"2. Instructions";  
        gotoxy(10,11); cout<<"3. Quit";
        gotoxy(10,13); cout<<"Select option: ";
        char op = getche();

        if( op=='1') play();
        else if( op=='2') instructions();
        else if( op=='3') exit(0);

    }while(1);

    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 赛车游戏的源代码: ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RacingGame extends JFrame implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; private JPanel panel; private Timer timer; private int carX, carY, carSpeed, roadX, roadY; private boolean leftPressed, rightPressed, upPressed, downPressed; public RacingGame() { setTitle("Racing Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 600); setLocationRelativeTo(null); setResizable(false); panel = new JPanel() { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { super.paintComponent(g); // 绘制背景 g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.WHITE); // 绘制道路 g.fillRect(roadX, roadY, 100, getHeight()); // 绘制赛车 g.setColor(Color.RED); g.fillRect(carX, carY, 20, 40); } }; add(panel); timer = new Timer(10, this); timer.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); carX = 290; carY = 500; carSpeed = 0; roadX = 250; roadY = 0; } public void actionPerformed(ActionEvent e) { // 移动赛车 carX += carSpeed; if (carX < roadX + 20) { carX = roadX + 20; } if (carX > roadX + 60) { carX = roadX + 60; } // 移动道路 roadY += 5; if (roadY > getHeight()) { roadY = -getHeight(); } panel.repaint(); } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftPressed = true; carSpeed = -5; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightPressed = true; carSpeed = 5; } if (e.getKeyCode() == KeyEvent.VK_UP) { upPressed = true; carSpeed = -10; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { downPressed = true; carSpeed = 10; } } public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftPressed = false; if (rightPressed) { carSpeed = 5; } else { carSpeed = 0; } } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightPressed = false; if (leftPressed) { carSpeed = -5; } else { carSpeed = 0; } } if (e.getKeyCode() == KeyEvent.VK_UP) { upPressed = false; if (downPressed) { carSpeed = 10; } else { carSpeed = 0; } } if (e.getKeyCode() == KeyEvent.VK_DOWN) { downPressed = false; if (upPressed) { carSpeed = -10; } else { carSpeed = 0; } } } public void keyTyped(KeyEvent e) {} public static void main(String[] args) { RacingGame game = new RacingGame(); game.setVisible(true); } } ``` 这个小游戏使用了 Java Swing 库来创建 GUI 界面,实现了一个赛车在道路上运动的效果。玩家可以通过方向键控制赛车的移动,游戏会不断地更新赛车和道路的位置来模拟运动的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值