CRASH 一个赌博小游戏的编写

初步实现了本人从面向过程到面向对象编程的转变 :)

程序功能:可以玩N回合(bout),每回合(bout)只要未分胜负就可以继续玩下去,或中途退出。

crashGame.h

/***************************

* 一個簡單的賭博遊戲,遊戲規則如下:

* 玩家擲兩個骰子,點數為1到6,

* 如果第一次點數和為7或11,則玩家勝,

* 如果點數和為2、3或12,則玩家輸,

* 如果和為其它點數,則記錄第一次的點數和,

* 然後繼續擲骰,直至點數和等於第一次擲出的點數和,則玩家勝,

* 如果在這之前擲出了點數和為7,則玩家輸。

*

* Write by Tayii, 20080620

*

****************************/





#include <iostream>

using std::cin ;

using std::cout ;

using std::endl ;





class

CrashGame

{

public:

            CrashGame() ;    // constructor

   void     crashMain() ;    // main 

   void     crash() ;       // one total running

   void     rollFirst() ;   // roll first time

   void     rollAgain() ;  // roll after first time

   void     showResult();   // after current bout over, show the total result

   unsigned gainPoint() ;

   void     queryPlayAgain() ;  // query & set playAgain

//   void     setGameOver( bool ) ;

//   bool     getGameOver() ;  // get gameOver

//   char     getPlayAgain() ; // get playAgain

   

private:

   unsigned winCount ;    // win times in toal bout times

   unsigned lossCount ;   // loss times in toal bout times

   unsigned myPoint ;     // gameplayer's point after one turn

   char     playAgain ;   //  query, indicate to call roolAgain() or not

   bool     gameOver ;     // flag, result of program running, lead to call queryPlayAgain() or not

   unsigned rollTimes ;    // loop times  i.e. roll times of this bout

   unsigned boutTimes ;   // total bout times

};
crashGame.cpp

#include <iostream>

using std::cin ;

using std::cout ;

using std::endl ;



#include <ctime>

using std::time ;



#include <cstdlib>

using std::rand ;

using std::srand ;



#include <iomanip>

using std::setw ;



#include "crashGame.h" ;





CrashGame::CrashGame()  // constructor

{

   winCount  = 0 ;

   lossCount = 0 ;

   gameOver  = 0 ;

   rollTimes = 1 ;  //beacuse the argument is used in rollAgain()

   boutTimes = 0 ;

}



// main running

void 

CrashGame::crashMain()

{

   char      answer ;

      

   do

   {

      cout << "Do you wanna playing CrashGame ?  (Y/N)  " ;

      cin >> answer ;

   }   while ( ( 'y' != answer ) && ( 'Y' != answer ) 

              && ( 'n' != answer ) && ( 'N' != answer ) ) ;

   cout << endl ;

   

   while ( ( 'y' == answer ) || ( 'Y' == answer ) )

   {

      ++boutTimes ;

      

      cout << setw( 40 ) << "Play bout:  " << boutTimes << endl ;

      

      crash() ;  // run Crash() one time

      

      showResult() ;

      

      do

      {

         cout << "/nDo you wanna playing CrashGame again ?  (Y/N)  " ;

         cin >> answer ;

      }   while ( ( 'y' != answer ) && ( 'Y' != answer ) 

                 && ( 'n' != answer ) && ( 'N' != answer ) ) ;

      cout << endl ;

   }

   

   cout << "GAME OVER!" << endl ;

   

}



//runnig crashGame once whole

void

CrashGame::crash()

{

   srand ( time( 0 ) ) ;

   

   rollFirst() ;

   

   while ( !gameOver )

   {

      queryPlayAgain() ;

      

      if ( !gameOver )

         rollAgain() ;

   }

   

   gameOver = 0 ;

}



// roll first

void

CrashGame::rollFirst()

{      

   myPoint = gainPoint() ;   // first time sum of 2 points

   

   if ( ( 7 == myPoint ) || ( 11 == myPoint ) )

   {

      ++winCount ;

      cout << "You win this bout in the 1st time ! " << endl ; 

      gameOver = 1 ; 

   }

   else if ( ( 2 == myPoint ) || ( 3 == myPoint )  || ( 12 == myPoint ) )

   {

      ++lossCount ;

      cout << "You loss this bout in the 1st time ! " << endl ;

      gameOver = 1 ;  

   }

   else

      cout << "No win or loss!" << endl ;

}



void

CrashGame::rollAgain()

{

   unsigned sum_point ;    // 2+ times sum of 2 points

   

   ++rollTimes ;         // loop times add

   

   sum_point = gainPoint() ;

         

   if ( 7 == sum_point )

   {

      ++lossCount ;

      cout << "You loss this bout in the " << rollTimes << "th time ! " << endl ;  

      gameOver = 1 ;  

   }

   else if ( myPoint == sum_point )

   {

      ++winCount ;

      cout << "You win this bout in the " << rollTimes << "th time ! " << endl ;

      gameOver = 1 ;   

   }   

   else

      cout << "No win or loss!" << endl ;

}



void 

CrashGame::showResult()

{

   cout << "/nIt's the rout " << boutTimes << " : /n/n"

        << setw( 15 ) << "Win" << setw( 15 ) << "Loss/n"

        << setw( 15 ) << winCount << setw( 15 ) << lossCount 

        << endl ;

}





// gain sum of 2 dices

unsigned 

CrashGame::gainPoint()

{

   unsigned dice1, dice2 ;

   unsigned sumPoint ;

   

   dice1 = ( 1 + rand() % 6 ) ;

   dice2 = ( 1 + rand() % 6 ) ;

   

   sumPoint = dice1 + dice2 ;

   

   cout << "Your current Point = " << sumPoint << " ,   ( " << dice1 << " + " << dice2 << " )." << endl ;

        

   return sumPoint ;

}





void

CrashGame::queryPlayAgain()

{

   do

   {

      cout << "Continue playing ? (Y/N)  " ;

      cin >> playAgain ;

   } while ( ( 'y' != playAgain ) && ( 'Y' != playAgain ) 

             && ( 'n' != playAgain ) && ( 'N' != playAgain ) ) ;

   cout << endl ;

   

   if ( ( 'n' == playAgain ) || ( 'N' == playAgain ) )

   {

      cout << "You stop playing." << endl ;

      gameOver = 1 ;

   }

}

/*

void

CrashGame::setGameOver( bool aa )

{

   gameOver = aa ;

}



bool

CrashGame::getGameOver()

{

   return  gameOver ;

}



char

CrashGame::getPlayAgain()

{

   return  playAgain ;

}

*/

crashGame_Main.cpp
 
 
#include  <iostream>

using  std::cout ;

using  std::endl ;

using  std::cin  ;



#include "crashGame.h"



int 

main()

{

   int wait ;

   

   CrashGame  crash1 ;

   

   crash1.crashMain() ;

   

   cin >> wait ;

   

   return 0 ;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的飞机大战游戏,你可以在 Python 中运行它: ```python import pygame import random # 初始化 Pygame pygame.init() # 设置屏幕宽高和标题 screen_width = 600 screen_height = 800 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("飞机大战") # 加载背景图片 background = pygame.image.load("background.png").convert() # 加载飞机图片 player = pygame.image.load("player.png").convert_alpha() # 加载敌机图片 enemy = pygame.image.load("enemy.png").convert_alpha() # 加载子弹图片 bullet = pygame.image.load("bullet.png").convert_alpha() # 设置游戏字体 font = pygame.font.SysFont(None, 36) # 定义函数:显示分数 def show_score(score): score_text = font.render("得分: " + str(score), True, (255, 255, 255)) screen.blit(score_text, (10, 10)) # 定义函数:显示游戏结束 def show_gameover(): gameover_text = font.render("游戏结束", True, (255, 0, 0)) screen.blit(gameover_text, (screen_width / 2 - gameover_text.get_width() / 2, screen_height / 2 - gameover_text.get_height() / 2)) # 定义函数:创建敌机 def create_enemy(): enemy_speed = random.randint(1, 5) enemy_x = random.randint(0, screen_width - enemy.get_width()) enemy_y = -enemy.get_height() return {"x": enemy_x, "y": enemy_y, "speed": enemy_speed} # 定义函数:移动敌机 def move_enemy(enemy_list): for enemy in enemy_list: enemy["y"] += enemy["speed"] # 定义函数:创建子弹 def create_bullet(player_x, player_y): bullet_x = player_x + player.get_width() / 2 - bullet.get_width() / 2 bullet_y = player_y - bullet.get_height() return {"x": bullet_x, "y": bullet_y} # 定义函数:移动子弹 def move_bullet(bullet_list): for bullet in bullet_list: bullet["y"] -= 5 # 定义函数:判断子弹是否打中敌机 def hit_enemy(bullet_list, enemy_list, score): for bullet in bullet_list: for enemy in enemy_list: if bullet["x"] + bullet.get_width() > enemy["x"] and bullet["x"] < enemy["x"] + enemy.get_width() and bullet["y"] < enemy["y"] + enemy.get_height(): bullet_list.remove(bullet) enemy_list.remove(enemy) score += 10 return score # 定义函数:判断是否撞击敌机 def crash_enemy(player_x, player_y, enemy_list): for enemy in enemy_list: if player_x + player.get_width() > enemy["x"] and player_x < enemy["x"] + enemy.get_width() and player_y < enemy["y"] + enemy.get_height() and player_y + player.get_height() > enemy["y"]: return True return False # 初始化玩家位置 player_x = screen_width / 2 - player.get_width() / 2 player_y = screen_height - player.get_height() # 初始化分数和游戏状态 score = 0 gameover = False # 初始化敌机列表和子弹列表 enemy_list = [] bullet_list = [] # 设置游戏时钟 clock = pygame.time.Clock() # 游戏循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bullet_list.append(create_bullet(player_x, player_y)) # 移动玩家 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x > 0: player_x -= 5 if keys[pygame.K_RIGHT] and player_x < screen_width - player.get_width(): player_x += 5 if keys[pygame.K_UP] and player_y > 0: player_y -= 5 if keys[pygame.K_DOWN] and player_y < screen_height - player.get_height(): player_y += 5 # 创建敌机 if random.randint(1, 10) == 1: enemy_list.append(create_enemy()) # 移动敌机 move_enemy(enemy_list) # 移动子弹 move_bullet(bullet_list) # 判断子弹是否打中敌机 score = hit_enemy(bullet_list, enemy_list, score) # 判断是否撞击敌机 if crash_enemy(player_x, player_y, enemy_list): gameover = True # 绘制游戏元素 screen.blit(background, (0, 0)) screen.blit(player, (player_x, player_y)) for enemy in enemy_list: screen.blit(enemy, (enemy["x"], enemy["y"])) for bullet in bullet_list: screen.blit(bullet, (bullet["x"], bullet["y"])) show_score(score) if gameover: show_gameover() # 刷新屏幕 pygame.display.update() # 控制游戏帧率 clock.tick(60) ``` 在运行代码之前需要准备四张 png 图片作为背景、玩家、敌机和子弹的素材,分别保存为 background.png、player.png、enemy.png 和 bullet.png。 运行游戏的方式是在 Python 命令行中输入 `python 文件名.py`,其中文件名是你保存代码的文件名。游戏开始后,你可以通过方向键控制玩家移动,按下空格键发射子弹,撞击敌机或者被敌机撞击都会结束游戏。玩家每打中一架敌机得分增加 10 分。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值