天天看點

python學習 之 pygame學習01

python學習 之 pygame學習01

看了幾天 pygame,搗鼓出了一個簡單的小遊戲(也經曆了四個版本),比較簡陋(哭)

#author: Cao Zhanxiang
#time: 2020/7/10
#function: V4,讓彈球真正成為一個遊戲

import pygame
import sys
import time
from random import randint


# 速度,x,y方向分速度
Speed = [7, 7]
# 定義RGB的黑色
BLACK = (0, 0, 0)
# 定義寬高
Width = 1000
Height = 600
# 定義初始成績
Score = -1  # 為什麼是-1,因為寫0開始成績是1(哭),也不知道為啥
# 初始化操作
pygame.init()
Screen = pygame.display.set_mode((Width, Height))
pygame.display.set_caption("BounceBall")

# Surface對象及Rect對象的建立
# pygame的Surface對象涵蓋所有導入的圖像
# get_rect()傳回一個覆寫圖像矩形(外切矩形)的Rect對象,可獲得矩形位置資訊
# 具體操作的時候,還是操作Rect對象(矩形)
Ball = pygame.image.load("littleball.gif")
BallRect = Ball.get_rect()
# 本來想畫小星星的,但找不到合适的資源(哭)
Star = pygame.image.load("littleball.gif")
StarRect = Star.get_rect()

# 設定成績字型
ScoreFont = pygame.font.SysFont('宋體', 30, True)
# 設定時間字型
TimeFont = pygame.font.SysFont('宋體', 30, True)

# 幀率,frames per second
FPS = 300
# Clock對象,用于操縱時間
fClock = pygame.time.Clock()

# 随機位置
RandX = randint(0, Width - StarRect.width)
RandY = randint(0, Height - StarRect.height)

# 開始時間
BeginTime = time.time()

while True:
    # 退出信号
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # 根據鍵盤輸入更改速度
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                Speed[0] -= 1
            elif event.key == pygame.K_RIGHT:
                Speed[0] += 1
            elif event.key == pygame.K_UP:
                Speed[1] -= 1
            elif event.key == pygame.K_DOWN:
                Speed[1] += 1

    # 一直在運動,速度在邊界處變化,實作反彈
    BallRect = BallRect.move(Speed[0], Speed[1])
    if BallRect.left < 0 or BallRect.right > Width:
        Speed[0] = -Speed[0]
    if BallRect.top < 0 or BallRect.bottom > Height:
        Speed[1] = -Speed[1]

    # 若球碰到星星相碰
    if pygame.Rect.colliderect(BallRect, StarRect):
        Score += 1
        RandX = randint(0, Width - StarRect.width)
        RandY = randint(0, Height - StarRect.height)

    # 将星星位置移動
    StarRect.top = RandY
    StarRect.left = RandX

    # 底色填充黑色,因為預設路徑上會殘留之前的圖(像turtle一樣)
    Screen.fill(BLACK)
    # 我們移動的是BallRect,但看到的卻應該是Ball,是以将Ball附在BallRect上,blit:位塊傳送
    Screen.blit(Ball, BallRect)
    Screen.blit(Star, StarRect)

    # 繪制遊戲得分
    Screen.blit(ScoreFont.render('Score: %d' % Score, True, [255, 0, 0]), [20, 20])

    # 繪制剩餘時間
    Screen.blit(TimeFont.render('Time Left: %d s' % (60 - time.time() + BeginTime), True, [255, 0, 0]), [800, 20])

    # 更新圖像
    pygame.display.update()
    fClock.tick(FPS)

    # 如果剩餘時間沒了,退出循環
    if (60 - time.time() + BeginTime) <= 0:
        break

# 繪制最終成績
Screen.fill(BLACK)
Screen.blit(ScoreFont.render('Final Score: %d' % Score, True, [255, 0, 0]), [420, 270])
pygame.display.update()

# 等待退出程式
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

           

使用者操作:

上下左右四個按鍵,改變速度,上使豎直方向分速度(正方向向下,而且注意是速度,不是速率)增加,其他同理,每碰到目标分數加一,限時60秒

效果展示:

python學習 之 pygame學習01
python學習 之 pygame學習01

我最好的成績隻有56分(菜的一批)。

有什麼錯誤,歡迎批評指正(新手上路)。

上一篇: i am freshman
下一篇: Freshman Here~