1. 程式人生 > 程式設計 >python運用pygame庫實現雙人彈球小遊戲

python運用pygame庫實現雙人彈球小遊戲

使用python pygame庫實現一個雙人彈球小遊戲,兩人分別控制一個左右移動的擋板用來攔截小球,小球會在兩板間不停彈跳,攔截失敗的一方輸掉遊戲,規則類似於簡化版的乒乓球。

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

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

程式碼如下:

執行環境為python 3.7,需要安裝pygame庫

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

# 定義gameOver函式
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!',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',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函式
def main():
 # 初始化pygame
 pygame.init()
 fpsClock = pygame.time.Clock()
 # 建立pygame顯示層
 playSurface = pygame.display.set_mode((640,480))
 pygame.display.set_caption('ping pang ball')

 # 初始化變數
 #兩塊板子為5塊正方形組成的矩形,小球為1塊正方形,正方形大小為20x20
 board_1 = [[100,0],[120,[140,[160,[180,0]] 
 board_2 = [[100,460],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軸方向正向移動
   #判斷小球擊中板子左角的狀態,如果小球擊中板子左角並且移動方向為正向,則:
   if ball[0]==board_1[0][0]-20 and direction_x==1:
    direction=0 #設此刻方向改為0
   #如果小球擊中板子左數第一塊,則:
   if ball[0]==board_1[0][0]:
    direction=1 #設此刻方向改為1
   #如果小球擊中板子左數第二塊,則:
   if ball[0]==board_1[1][0]:
    direction=2 #設此刻方向改為2
   #如果小球擊中板子正中間,則:
   if ball[0]==board_1[2][0]:
    direction=3 #設此刻方向改為3
   #如果小球擊中板子左數第四塊,則:
   if ball[0]==board_1[3][0]:
    direction=4 #設此刻方向改為4
   #如果小球擊中板子左數第五塊,則:
   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,Rect(board_2[0],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()

執行結果如下:

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