python中的pygame實現(xiàn)接球小游戲
一、介紹模塊
最小開發(fā)框架:
1、Pygame和sys模塊
import pygame #制作游戲時要使用的模塊 import sys #python的標準庫,對內部各功能模塊進行初始化創(chuàng)建,系統(tǒng)模塊
2、random模塊
需要在屏幕上隨機生成小球
from random import randint
詳情請看此文章:python中的隨機數(shù) Random介紹
二、相關功能
1、窗口尺寸改變
可以調節(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: # 判斷擋板左邊的坐標是否小于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: # 判斷擋板右邊的坐標是否大于屏幕的寬度 board_rect.right = w
board_rect.bottom += board_y3、鼠標控制
#鼠標控制擋板
elif event.type == pygame.MOUSEMOTION:
# 鼠標左鍵按下并跟隨鼠標移動
if event.buttons[0] == 1:
if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標的位置
board_rect.left = event.pos[0] #將鼠標的x坐標給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: #鼠標按鍵按下
# 將鼠標當前位置給擋板
if event.button == 1:
if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標的位置
board_rect.left = event.pos[0] #將鼠標的x坐標給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 -= 15、小球未接住小球
# 下方擋板未接到小球
if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206):
# 游戲結束
ball_y = 200 #小球所在的位置
break6、小球移動
# 移動小球
ball_x += move_x
ball_y += move_y
if ball_x <= 20 or ball_x >= w - 20: # 碰到左右兩側墻壁
move_x = - move_x # x方向速度反向
if ball_y <= 20: # 碰到上方墻壁
move_y = - move_y # y方向速度反向7、顯示分數(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("接球游戲") # 屏幕的標題
fpsClock = pygame.time.Clock() # 幀速率 窗口刷新速度 越大運行越快
board = pygame.image.load(r"D:\pycharm\WorkTime(大二上)\擋板.jpg")
board_rect = board.get_rect() #對圖片進行加框 利用surface生成rect
color = pygame.Color(255,255,255) # 屏幕(窗口)的顏色:白色
Green = pygame.Color('green') # 小球的顏色:綠色
# 隨機生成小球的x、y坐標(整數(shù),包括兩端)
ball_x = randint(20,580)
ball_y = randint(20,200)
# 小球x、y坐標變化量
move_x = 1
move_y = 1
# 擋板x、y坐標變化量
board_x = 46
board_y = 0
score=0 #得分
font=pygame.font.Font(r'D:\字庫\書法和新增字庫\微軟雅黑.ttf',60) #設置字體(前者是字體路徑)和字體大小
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: # 判斷擋板左邊的坐標是否小于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: # 判斷擋板右邊的坐標是否大于屏幕的寬度 board_rect.right = w
board_rect.bottom += board_y
#鼠標控制擋板
elif event.type == pygame.MOUSEMOTION:
# 鼠標左鍵按下并跟隨鼠標移動
if event.buttons[0] == 1:
if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標的位置
board_rect.left = event.pos[0] #將鼠標的x坐標給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: #鼠標按鍵按下
# 將鼠標當前位置給擋板
if event.button == 1:
if event.pos[0] >= 0 and event.pos[0] < w - 186:#判斷鼠標的位置
board_rect.left = event.pos[0] #將鼠標的x坐標給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):
# 游戲結束
ball_y = 200
break
# 移動小球
ball_x += move_x
ball_y += move_y
if ball_x <= 20 or ball_x >= w - 20: # 碰到左右兩側墻壁
move_x = - move_x # x方向速度反向
if ball_y <= 20: # 碰到上方墻壁
move_y = - move_y # y方向速度反向
fpsClock.tick(200)
screen.fill(color)
# 顯示分數(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() # 對顯示窗口進行更新,默認窗口全部重繪到此這篇關于python中的pygame實現(xiàn)接球小游戲的文章就介紹到這了,更多相關pygame接球游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
處理Selenium3+python3定位鼠標懸停才顯示的元素
這篇文章主要介紹了Selenium3+python3--如何定位鼠標懸停才顯示的元素 ,文中通過簡單代碼給大家介紹的非常詳細,需要的朋友可以參考下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

