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

python運(yùn)用pygame庫(kù)實(shí)現(xiàn)雙人彈球小游戲

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

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

因?yàn)槭堑谝淮斡胮ygame寫python小游戲并且只用了兩三個(gè)小時(shí),所以有些粗糙,部分方面有些bug,比如板子可以移動(dòng)出屏幕外,游戲結(jié)束后的提示顯示不全。

但是關(guān)鍵部分如小球的移動(dòng)和基本功能等,還算比較完善。

代碼如下:

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

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塊正方形組成的矩形,小球?yàn)?塊正方形,正方形大小為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軸的移動(dòng)方向及速度 
 direction_x=0 #判斷小球沿X軸正向還是反向移動(dòng) 0反向 1正向,2沒(méi)有速度
 direction_y=1 #控制小球Y軸的移動(dòng)方向及速度 0反向,1正向
 # 檢測(cè)例如按鍵等pygame事件
 while True:
  for event in pygame.event.get():
   if event.type == QUIT:
    pygame.quit()
    sys.exit()
   elif event.type == KEYDOWN:
    # 判斷鍵盤事件控制板子移動(dòng)
    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軸方向正向移動(dòng)
   #判斷小球擊中板子左角的狀態(tài),如果小球擊中板子左角并且移動(dòng)方向?yàn)檎?,則:
   if ball[0]==board_1[0][0]-20 and direction_x==1:
    direction=0 #設(shè)此刻方向改為0
   #如果小球擊中板子左數(shù)第一塊,則:
   if ball[0]==board_1[0][0]:
    direction=1 #設(shè)此刻方向改為1
   #如果小球擊中板子左數(shù)第二塊,則:
   if ball[0]==board_1[1][0]:
    direction=2 #設(shè)此刻方向改為2
   #如果小球擊中板子正中間,則:
   if ball[0]==board_1[2][0]:
    direction=3 #設(shè)此刻方向改為3
   #如果小球擊中板子左數(shù)第四塊,則:
   if ball[0]==board_1[3][0]:
    direction=4 #設(shè)此刻方向改為4
   #如果小球擊中板子左數(shù)第五塊,則:
   if ball[0]==board_1[4][0]:
    direction=5 #設(shè)此刻方向改為5
   #如果小球擊中板子右角并且移動(dòng)方向?yàn)榉聪颍?
   if ball[0]==board_1[4][0]+20 and direction_x==0:
    direction=6 #設(shè)此刻方向改為6
   #如果小球擊中板子兩角但是沒(méi)有速度,即豎直移動(dòng)
   if direction_x==2 and (ball[0]==board_1[0][0]-20 or ball[0]==board_1[4][0]+20):
    direction_y=0 #設(shè)此刻Y軸方向改為0
  #判斷小球擊中board_2的位置,與擊中board_1時(shí)相比只改變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
  #設(shè)置小球Y軸的移動(dòng)速度
  if direction_y==0: 
   ball[1]-=20
  if direction_y==1:
   ball[1]+=20
  #設(shè)置小球X軸的移動(dòng)速度,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()

運(yùn)行結(jié)果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論