python中的pygame實現(xiàn)接球小游戲
一、介紹模塊
最小開發(fā)框架:
1、Pygame和sys模塊
import pygame #制作游戲時要使用的模塊 import sys #python的標(biāo)準(zhǔn)庫,對內(nèi)部各功能模塊進(jìn)行初始化創(chuàng)建,系統(tǒng)模塊
2、random模塊
需要在屏幕上隨機(jī)生成小球
from random import randint
詳情請看此文章:python中的隨機(jī)數(shù) Random介紹
二、相關(guān)功能
1、窗口尺寸改變
可以調(diào)節(jié)游戲屏幕的大小
# 改變窗口尺寸 elif event.type == pygame.VIDEORESIZE: size = w,h = event.w,event.h screen = pygame.display.set_mode(size,pygame.RESIZABLE)
pygame.VIDEORESIZE 這是窗口大小改變事件,事件發(fā)生后,返回event.size元組,包含新窗口的寬度和高度。 .size[0] 高度,也可以用event.w表示 .size[1] 寬度,也可以用event.h表示 返回參數(shù)僅在事件發(fā)生時有用
2、鍵盤控制擋板
# 鍵盤控制擋板 elif event.type == pygame.KEYDOWN: #鍵盤按下事件檢測 if event.key == pygame.K_LEFT: # 判斷擋板是否左移 if board_rect.left > 0 and board_rect.left <= w - 186: board_rect.left -= board_x elif board_rect.left <= 0: # 判斷擋板左邊的坐標(biāo)是否小于0 board_rect.left = 0 board_rect.top -= board_y elif event.key == pygame.K_RIGHT: # 判斷擋板是否右移 if board_rect.right >= 186 and board_rect.right < w: board_rect.right += board_x elif board_rect.right >= w: # 判斷擋板右邊的坐標(biāo)是否大于屏幕的寬度 board_rect.right = w board_rect.bottom += board_y
3、鼠標(biāo)控制
#鼠標(biāo)控制擋板 elif event.type == pygame.MOUSEMOTION: # 鼠標(biāo)左鍵按下并跟隨鼠標(biāo)移動 if event.buttons[0] == 1: if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標(biāo)的位置 board_rect.left = event.pos[0] #將鼠標(biāo)的x坐標(biāo)給Rect對象的左邊 elif event.pos[0] >= w - 186 and event.pos[0] <= w: board_rect.left = w - 186 # board_rect.top = h - 17 #檔板位置在底部 elif event.type == pygame.MOUSEBUTTONDOWN: #鼠標(biāo)按鍵按下 # 將鼠標(biāo)當(dāng)前位置給擋板 if event.button == 1: if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標(biāo)的位置 board_rect.left = event.pos[0] #將鼠標(biāo)的x坐標(biāo)給Rect對象的左邊 if event.pos[0] >= w - 186 and event.pos[0] <= w: board_rect.left = w - 186 # board_rect.top = h - 17
4、擋板接住小球并得分
# 下方擋板接到小球 if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x <= board_rect.left + 206): move_y = - move_y # y方向速度反向 score += points #得分 count += 1 #次數(shù)增加1次 if count == 5: # 每滿五次,難度和單次接球得分增加 count = 0 # 接球得分的次數(shù)清零 points += points # x方向速度增加 if move_x > 0: move_x += 1 else: move_x -= 1 move_y -= 1
5、小球未接住小球
# 下方擋板未接到小球 if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206): # 游戲結(jié)束 ball_y = 200 #小球所在的位置 break
6、小球移動
# 移動小球 ball_x += move_x ball_y += move_y if ball_x <= 20 or ball_x >= w - 20: # 碰到左右兩側(cè)墻壁 move_x = - move_x # x方向速度反向 if ball_y <= 20: # 碰到上方墻壁 move_y = - move_y # y方向速度反向
7、顯示分?jǐn)?shù)
my_score = font.render(str(score), False, (255, 255, 0)) # 創(chuàng)建文字對象(文字,是否平滑,文字顏色) screen.blit(my_score, (w - 100, 30)) # 將文字添加到窗口上
三、完整代碼
import sys from random import randint import pygame pygame.init() # 初始化 size = w, h = (600,500) # 屏幕顯示的區(qū)域,高度和寬度 screen = pygame.display.set_mode(size,pygame.RESIZABLE) pygame.display.set_caption("接球游戲") # 屏幕的標(biāo)題 fpsClock = pygame.time.Clock() # 幀速率 窗口刷新速度 越大運行越快 board = pygame.image.load(r"D:\pycharm\WorkTime(大二上)\擋板.jpg") board_rect = board.get_rect() #對圖片進(jìn)行加框 利用surface生成rect color = pygame.Color(255,255,255) # 屏幕(窗口)的顏色:白色 Green = pygame.Color('green') # 小球的顏色:綠色 # 隨機(jī)生成小球的x、y坐標(biāo)(整數(shù),包括兩端) ball_x = randint(20,580) ball_y = randint(20,200) # 小球x、y坐標(biāo)變化量 move_x = 1 move_y = 1 # 擋板x、y坐標(biāo)變化量 board_x = 46 board_y = 0 score=0 #得分 font=pygame.font.Font(r'D:\字庫\書法和新增字庫\微軟雅黑.ttf',60) #設(shè)置字體(前者是字體路徑)和字體大小 points=1 #一次接球的加分 count=0 #接球得分的次數(shù) # size1 = board.get_size() #獲取圖片大小 # print(size1) while True: board_rect.top = h - 17 for event in pygame.event.get(): # pygame.event.get() 從事件隊列中取出事件,并從隊列中刪除該事件 if event.type == pygame.QUIT: sys.exit() # 改變窗口尺寸 elif event.type == pygame.VIDEORESIZE: size = w,h = event.w,event.h screen = pygame.display.set_mode(size,pygame.RESIZABLE) # 鍵盤控制擋板 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: # 擋板左移 if board_rect.left > 0 and board_rect.left <= w - 186: board_rect.left -= board_x elif board_rect.left <= 0: # 判斷擋板左邊的坐標(biāo)是否小于0 board_rect.left = 0 board_rect.top -= board_y elif event.key == pygame.K_RIGHT: # 擋板右移 if board_rect.right >= 186 and board_rect.right < w: board_rect.right += board_x elif board_rect.right >= w: # 判斷擋板右邊的坐標(biāo)是否大于屏幕的寬度 board_rect.right = w board_rect.bottom += board_y #鼠標(biāo)控制擋板 elif event.type == pygame.MOUSEMOTION: # 鼠標(biāo)左鍵按下并跟隨鼠標(biāo)移動 if event.buttons[0] == 1: if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標(biāo)的位置 board_rect.left = event.pos[0] #將鼠標(biāo)的x坐標(biāo)給Rect對象的左邊 elif event.pos[0] >= w - 186 and event.pos[0] <= w: board_rect.left = w - 186 # board_rect.top = h - 17 #檔板位置在底部 elif event.type == pygame.MOUSEBUTTONDOWN: #鼠標(biāo)按鍵按下 # 將鼠標(biāo)當(dāng)前位置給擋板 if event.button == 1: if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標(biāo)的位置 board_rect.left = event.pos[0] #將鼠標(biāo)的x坐標(biāo)給Rect對象的左邊 if event.pos[0] >= w - 186 and event.pos[0] <= w: board_rect.left = w - 186 # board_rect.top = h - 17 # 下方擋板接到小球 if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x <= board_rect.left + 206): move_y = - move_y # y方向速度反向 score += points count += 1 if count == 5: # 每滿五次,難度和單次接球得分增加 count = 0 # 接球得分的次數(shù)清零 points += points # x方向速度增加 if move_x > 0: move_x += 1 else: move_x -= 1 move_y -= 1 # 下方擋板未接到小球 if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206): # 游戲結(jié)束 ball_y = 200 break # 移動小球 ball_x += move_x ball_y += move_y if ball_x <= 20 or ball_x >= w - 20: # 碰到左右兩側(cè)墻壁 move_x = - move_x # x方向速度反向 if ball_y <= 20: # 碰到上方墻壁 move_y = - move_y # y方向速度反向 fpsClock.tick(200) screen.fill(color) # 顯示分?jǐn)?shù) my_score = font.render(str(score), False, (255, 255, 0)) # 創(chuàng)建文字對象(文字,是否平滑,文字顏色) screen.blit(my_score, (w - 100, 30)) # 將文字添加到窗口上 screen.blit(board,board_rect) #將一個圖像繪制在另一個圖像上 把surface對象覆蓋到移動后的rect對象 pygame.draw.circle(screen, Green, (ball_x, ball_y), 20) # 繪制小球 pygame.display.update() # 對顯示窗口進(jìn)行更新,默認(rèn)窗口全部重繪
到此這篇關(guān)于python中的pygame實現(xiàn)接球小游戲的文章就介紹到這了,更多相關(guān)pygame接球游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
處理Selenium3+python3定位鼠標(biāo)懸停才顯示的元素
這篇文章主要介紹了Selenium3+python3--如何定位鼠標(biāo)懸停才顯示的元素 ,文中通過簡單代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-07-07淺談python中字典append 到list 后值的改變問題
今天小編就為大家分享一篇淺談python中字典append 到list 后值的改變問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05淺談Python中re.match()和re.search()的使用及區(qū)別
這篇文章主要介紹了淺談Python中re.match()和re.search()的使用及區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04對Django 轉(zhuǎn)發(fā)和重定向的實例詳解
今天小編就為大家分享一篇對Django 轉(zhuǎn)發(fā)和重定向的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08