欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python運用pygame庫實現(xiàn)雙人彈球小游戲

 更新時間:2019年11月25日 15:26:58   作者:Lvengda  
這篇文章主要為大家詳細介紹了python運用pygame庫實現(xiàn)雙人彈球小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用python pygame庫實現(xiàn)一個雙人彈球小游戲,兩人分別控制一個左右移動的擋板用來攔截小球,小球會在兩板間不停彈跳,攔截失敗的一方輸?shù)粲螒?,?guī)則類似于簡化版的乒乓球。

因為是第一次用pygame寫python小游戲并且只用了兩三個小時,所以有些粗糙,部分方面有些bug,比如板子可以移動出屏幕外,游戲結束后的提示顯示不全。

但是關鍵部分如小球的移動和基本功能等,還算比較完善。

代碼如下:

運行環(huán)境為python 3.7,需要安裝pygame庫

import pygame,sys,time,random
from pygame.locals import *
# 定義顏色變量
redColour = pygame.Color(255,0,0)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)

# 定義gameOver函數(shù)
def gameOver(playSurface,board):
 gameOverFont = pygame.font.Font('C:\Windows\Fonts\consola.ttf',72)
 if board[0][1]==0:
  gameOverSurf = gameOverFont.render('board_2 win!', True, greyColour)
 if board[0][1]==460:
  gameOverSurf = gameOverFont.render('board_1 win!', True, greyColour)
 gameOverRect = gameOverSurf.get_rect()
 gameOverRect.midtop = (320, 10)
 playSurface.blit(gameOverSurf, gameOverRect)
 
 againFont = pygame.font.Font('C:\Windows\Fonts\consola.ttf',24)
 againSurf = gameOverFont.render('Do you want to try again? y/n', True, whiteColour)
 againRect=againSurf.get_rect()
 againRect.midtop=(20,100)
 playSurface.blit(againSurf, againRect)
 pygame.display.flip()
 time.sleep(3)
 for event in pygame.event.get():
  if event.key == ord("y"):
   main()
  if event.key==ord("n"):
   pygame.quit()
   sys.exit()
 pygame.quit()
 sys.exit()

# 定義main函數(shù)
def main():
 # 初始化pygame
 pygame.init()
 fpsClock = pygame.time.Clock()
 # 創(chuàng)建pygame顯示層
 playSurface = pygame.display.set_mode((640,480))
 pygame.display.set_caption('ping pang ball')

 # 初始化變量
 #兩塊板子為5塊正方形組成的矩形,小球為1塊正方形,正方形大小為20x20
 board_1 = [[100,0],[120,0],[140,0],[160,0],[180,0]] 
 board_2 = [[100,460],[120,460],[140,460],[160,460],[180,460]]
 ball = [100,100]
 direction=3 #控制小球X軸的移動方向及速度 
 direction_x=0 #判斷小球沿X軸正向還是反向移動 0反向 1正向,2沒有速度
 direction_y=1 #控制小球Y軸的移動方向及速度 0反向,1正向
 # 檢測例如按鍵等pygame事件
 while True:
  for event in pygame.event.get():
   if event.type == QUIT:
    pygame.quit()
    sys.exit()
   elif event.type == KEYDOWN:
    # 判斷鍵盤事件控制板子移動
    if event.key == K_RIGHT:
     for i in board_1:
      i[0]+=20
    if event.key == K_LEFT:
     for i in board_1:
      i[0]-=20
    if event.key == ord("a"):
     for i in board_2:
      i[0]-=20
    if event.key == ord("d"):
     for i in board_2:
      i[0]+=20
    if event.key == K_ESCAPE:
     pygame.event.post(pygame.event.Event(QUIT))

  # 判斷小球擊中board_1的位置,范圍為板子的左角到右角
  if ball[1] == board_1[0][1]+20 and board_1[0][0]-20<=ball[0]<=board_1[4][0]+20:
   direction_y=1 #若擊中板子,則Y軸方向正向移動
   #判斷小球擊中板子左角的狀態(tài),如果小球擊中板子左角并且移動方向為正向,則:
   if ball[0]==board_1[0][0]-20 and direction_x==1:
    direction=0 #設此刻方向改為0
   #如果小球擊中板子左數(shù)第一塊,則:
   if ball[0]==board_1[0][0]:
    direction=1 #設此刻方向改為1
   #如果小球擊中板子左數(shù)第二塊,則:
   if ball[0]==board_1[1][0]:
    direction=2 #設此刻方向改為2
   #如果小球擊中板子正中間,則:
   if ball[0]==board_1[2][0]:
    direction=3 #設此刻方向改為3
   #如果小球擊中板子左數(shù)第四塊,則:
   if ball[0]==board_1[3][0]:
    direction=4 #設此刻方向改為4
   #如果小球擊中板子左數(shù)第五塊,則:
   if ball[0]==board_1[4][0]:
    direction=5 #設此刻方向改為5
   #如果小球擊中板子右角并且移動方向為反向:
   if ball[0]==board_1[4][0]+20 and direction_x==0:
    direction=6 #設此刻方向改為6
   #如果小球擊中板子兩角但是沒有速度,即豎直移動
   if direction_x==2 and (ball[0]==board_1[0][0]-20 or ball[0]==board_1[4][0]+20):
    direction_y=0 #設此刻Y軸方向改為0
  #判斷小球擊中board_2的位置,與擊中board_1時相比只改變Y軸的方向,X軸不變 
  if ball[1]==board_2[0][1]-20 and board_2[0][0]-20<=ball[0]<=board_2[4][0]+20:
   direction_y=0
   if ball[0]==board_2[0][0]-20 and direction_x==1:
    direction=0
   if ball[0]==board_2[0][0]:
    direction=1
   if ball[0]==board_2[1][0]:
    direction=2
   if ball[0]==board_2[2][0]:
    direction=3
   if ball[0]==board_2[3][0]:
    direction=4
   if ball[0]==board_2[4][0]:
    direction=5
   if ball[0]==board_2[4][0]+20 and direction_x==0:
    direction=6
   if direction_x==2 and (ball[0]==board_2[0][0]-20 or ball[0]==board_2[4][0]+20):
    direction_y=1
  if ball[0]<=0:
   direction=4
  if ball[0]>=620:
   direction=2
  #設置小球Y軸的移動速度
  if direction_y==0: 
   ball[1]-=20
  if direction_y==1:
   ball[1]+=20
  #設置小球X軸的移動速度,X,Y軸速度的改變形成角度
  if direction==0:
   ball[0]-=40
   direction_x=0
  if direction==1:
   ball[0]-=40
   direction_x=0
  if direction==2:
   ball[0]-=20
   direction_x=0
  if direction==3:
   direction_x=2
  if direction==4:
   ball[0]+=20
   direction_x=1
  if direction==5:
   ball[0]+=40
   direction_x=1 
  if direction==6:
   ball[0]+=40
   direction_x=1


  # 繪制pygame顯示層
  playSurface.fill(blackColour)
  pygame.draw.rect(playSurface,whiteColour,Rect(board_1[0],(100,20)))
  pygame.draw.rect(playSurface,whiteColour,Rect(board_2[0],(100,20)))
  pygame.draw.rect(playSurface,redColour,Rect(ball,(20,20)))
  # 刷新pygame顯示層
  pygame.display.flip()
  # 判斷勝利
  if ball[1]==board_1[0][1] and (ball[0]<board_1[0][0] or ball[0]>board_1[4][0]):
   gameOver(playSurface,board_1)
  if ball[1]==board_2[0][1] and (ball[0]<board_2[0][0] or ball[0]>board_2[4][0]):
   gameOver(playSurface,board_2)
  
  # 控制游戲速度
  fpsClock.tick(5)

if __name__ == "__main__":
 main()

運行結果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python與pycharm有何區(qū)別

    python與pycharm有何區(qū)別

    在本篇文章里小編給大家整理了關于pycharm與python的區(qū)別相關內容,有需要的朋友們可以學習下。
    2020-07-07
  • Python列表insert()函數(shù)用法詳解

    Python列表insert()函數(shù)用法詳解

    這篇文章主要介紹了Python列表insert()函數(shù)的使用方法,文章通過代碼示例介紹的非常詳細,對我們學習或工作有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • python中的turtle庫函數(shù)簡單使用教程

    python中的turtle庫函數(shù)簡單使用教程

    這篇文章主要介紹了python中的turtle庫函數(shù)簡單使用教程。本文通過圖片的形式給大家展示的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-07-07
  • python 性能提升的幾種方法

    python 性能提升的幾種方法

    本篇文章主要介紹python 性能提升的幾種方法,并附有代碼參考示例,有需要的小伙伴可以參考下
    2016-07-07
  • python學生管理系統(tǒng)開發(fā)

    python學生管理系統(tǒng)開發(fā)

    這篇文章主要為大家詳細介紹了基礎版和函數(shù)版的python學生管理系統(tǒng)開發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Python實現(xiàn)人生重開模擬器小游戲講解

    Python實現(xiàn)人生重開模擬器小游戲講解

    這篇文章主要介紹了Python實現(xiàn)人生重開模擬器小游戲,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • Python基礎之模塊詳解

    Python基礎之模塊詳解

    本文詳細講解了Python基礎之模塊,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Python利用PyPDF2庫合并多個pdf文件

    Python利用PyPDF2庫合并多個pdf文件

    PyPDF2庫是一個用于處理PDF文件的Python庫,它提供了一系列的工具來讀取、編輯、合并、拆分和加密PDF文件,使得我們可以在Python環(huán)境下輕松地對PDF文件進行操作,本文將帶大家介紹如何通過Python的PyPDF2庫合并多個pdf文件,需要的朋友可以參考下
    2023-05-05
  • django中celery的定時任務使用

    django中celery的定時任務使用

    這篇文章主要介紹了django中celery的定時任務使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python發(fā)送多人郵件沒有展示收件人問題的解決方法

    python發(fā)送多人郵件沒有展示收件人問題的解決方法

    這篇文章主要為大家詳細介紹了python發(fā)送多人郵件沒有展示收件人問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06

最新評論