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

python中的pygame實現(xiàn)接球小游戲

 更新時間:2022年04月22日 08:27:39   作者:??牧子川????  
這篇文章主要介紹了python中的pygame實現(xiàn)接球小游戲,文章基于python的相關資料展開詳細的內容,具有一定的參考價值,需要的小伙伴可以參考一下

一、介紹模塊

最小開發(fā)框架:

基于python 的Pygame最小開發(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_y

3、鼠標控制

#鼠標控制擋板
        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 -= 1

5、小球未接住小球

# 下方擋板未接到小球
    if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206):
        # 游戲結束
        ball_y = 200  #小球所在的位置
        break

6、小球移動

# 移動小球
    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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java文件與類動手動腦實例詳解

    Java文件與類動手動腦實例詳解

    在本篇文章里小編給大家整理的是關于Java文件與類動手動腦實例知識點,有需要的朋友們學習參考下。
    2019-11-11
  • python選擇排序算法的實現(xiàn)代碼

    python選擇排序算法的實現(xiàn)代碼

    這篇文章主要介紹了python選擇排序算法的實現(xiàn)代碼,大家參考
    2013-11-11
  • python中format()函數(shù)的簡單使用教程

    python中format()函數(shù)的簡單使用教程

    python中format函數(shù)用于字符串的格式化,接下來通過本文給大家介紹python中format()函數(shù)的簡單使用教程,一起看看吧
    2018-03-03
  • 一文詳解Python中subprocess模塊的用法

    一文詳解Python中subprocess模塊的用法

    Python的subprocess模塊是一個非常強大的工具,用于啟動和與外部進程進行交互,本文將為大家詳細介紹?subprocess模塊的各個方面,希望對大家有所幫助
    2023-11-11
  • python+opencv打開攝像頭,保存視頻、拍照功能的實現(xiàn)方法

    python+opencv打開攝像頭,保存視頻、拍照功能的實現(xiàn)方法

    今天小編就為大家分享一篇python+opencv打開攝像頭,保存視頻、拍照功能的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python進程間通訊與進程池超詳細講解

    Python進程間通訊與進程池超詳細講解

    進程彼此之間互相隔離,要實現(xiàn)進程間通信(IPC),multiprocessing模塊主要通過隊列方式,隊列:隊列類似于一條管道,元素先進先出,需要注意的一點是:隊列都是在內存中操作,進程退出,隊列清空,另外,隊列也是一個阻塞的形態(tài)
    2022-12-12
  • python?scapy抓包獲取udp并轉發(fā)的操作步驟

    python?scapy抓包獲取udp并轉發(fā)的操作步驟

    這篇文章主要介紹了python?scapy抓包獲取udp并轉發(fā)的操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • python文件名批量重命名腳本實例代碼

    python文件名批量重命名腳本實例代碼

    這篇文章主要給大家介紹了關于python文件名批量重命名腳本的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Pandas缺失值刪除df.dropna()的使用

    Pandas缺失值刪除df.dropna()的使用

    本文主要介紹了Pandas缺失值刪除df.dropna()的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Python+OpenCV讀寫視頻的方法詳解

    Python+OpenCV讀寫視頻的方法詳解

    這篇文章主要為大家詳細介紹了Python+OpenCV進行讀寫視頻操作的示例代碼,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下
    2022-08-08

最新評論