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,0)
blackColour = pygame.Color(0,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!', 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函數
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,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軸方向正向移動
#判斷小球擊中板子左角的狀態,如果小球擊中板子左角并且移動方向為正向,則:
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,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[4][0]):
gameOver(playSurface,board_1)
if ball[1]==board_2[0][1] and (ball[0]board_2[4][0]):
gameOver(playSurface,board_2)
# 控制游戲速度
fpsClock.tick(5)
if __name__ == "__main__":
main()
運行結果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持【聽圖閣-專注于Python設計】。
總結
以上是生活随笔為你收集整理的python的pygame库使用方法_[宜配屋]听图阁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue 双向绑定小案例
- 下一篇: 采用我国国产处理器的超级计算机是,“中国