Pygame(十二)打磚塊
目標
- 小球撞擊回應
- 磚塊撞擊回應
- 擋板撞擊回應
完整示例代碼
# /usr/bin/python3
# Author: 愛編程的章老師
# @Time: 2021/1/9 0009
# E-mail: Bluesand2010@163.com
'''設計一個打磚塊的游戲'''
# 1. 開局螢屏上方有4行每行10個磚塊
# 2. 在螢屏下方有一個長100 的擋板
# 3. 有一個小球從擋板中間出發,45角方向
import pygame
import sys
import time
# 主程式
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("打磚塊V.01 by 愛編程的章老師")
# 定義顏色
color1 = 255, 0 ,0 # 目標磚塊的顏色
color2 = 0, 255, 0 # 擋板的顏色
color3 = 0, 0, 255 # 球的顏色
font_color = 255, 255, 255 # 字體顏色
# 生成目標磚塊
rect_list = []
for i in range(10):
for j in range(6):
rect_temp = pygame.Rect(i * 78 + 10, j*20 + 60, 75, 17)
rect_list.append(rect_temp)
# 定義擋板
rect_b = pygame.Rect(350, 550, 100, 15)
# 定義小球
ball = pygame.Rect(380, 520, 30, 30)
# 定義分數字體物件
font = pygame.font.SysFont("fangsong", 20)
# 畫初始圖形
for r in rect_list: # 畫目標磚塊
pygame.draw.rect(screen, color1, r)
pygame.draw.rect(screen,color2, rect_b) # 畫擋板
pygame.draw.ellipse(screen, color3, ball) # 畫小球
# 繪制分數
score_surface = font.render("分數:0", True, font_color)
screen.blit(score_surface, (10, 15))
# 更新螢屏
pygame.display.update()
# 小球移動方向
dx = dy = -1
# 小球移動幅度
step = 5
# 分數
score = 0
# 主程式
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 按鍵檢測
keys = pygame.key.get_pressed()
# 左移擋板
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
if rect_b.left - 10 >= 0:
rect_b.left -= 10
# 右移擋板
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
if rect_b.right + 10 <= 800:
rect_b.right += 10
# 小球移動
ball.move_ip(dx * step, dy * step)
# 小球碰撞檢測
# 撞到擋板, 或者撞到螢屏上邊緣就上下反方向
if ball.colliderect(rect_b) or ball.top <=0 :
dy *= -1
# 撞到左右螢屏邊緣就左右反向
if ball.left <= 0 or ball.right >= 800:
dx *= -1
# 檢測有沒有撞到目標磚
for each in rect_list:
if ball.colliderect(each):
if ball.left <= each.right and each.center[0] > each.center[0]:
dx *= -1
elif ball.right >= each.left and each.center[0] < each.center[0]:
dx *= -1
elif ball.top <= each.bottom and ball.center[1] > each.center[1]:
dy *= -1
elif ball.bottom >= each.top and ball.center[1] < each.center[1]:
dy *= -1
rect_list.remove(each)
score += 100
break
# 沒接到球,游戲失敗
if ball.bottom > rect_b.bottom:
font = pygame.font.SysFont("fangsong",50)
font_surface = font.render("游戲失敗", True, color1)
screen.blit(font_surface, (400- 100, 300-25))
pygame.display.update()
break
# 方塊打完了,游戲勝利
if not rect_list:
font = pygame.font.SysFont("fangsong",50)
font_surface = font.render("游戲勝利", True, color1)
screen.blit(font_surface, (400- 100, 300-25))
pygame.display.update()
break
# 清空螢屏
screen.fill((0,0,0))
# 繪制圖形
font_surface = font.render("分數:" + str(score), True, font_color)
screen.blit(font_surface, (10, 15))
for each in rect_list:
pygame.draw.rect(screen, color1, each)
pygame.draw.rect(screen, color2, rect_b)
pygame.draw.ellipse(screen, color3, ball)
pygame.display.update()
time.sleep(0.05)
# 主程式
if __name__ == '__main__':
main()
游戲效果:

代碼分析
游戲的基本流程:
- 繪制圖形
- 角色移動(運動)
- 角色互動
- 繪制新的圖形
基于以上流程
游戲中兩個會動的角色: 小球, 擋板
擋板只有左右移動.代碼比較簡單
小球的運動,這里選擇了45度角恒定不變的運動方式,簡化編程難度
小球的運動方向有右上,左上,右下,左下四個方向.用dx與dy來控制元件
根據碰撞的情形來改變運動的方向
游戲失敗的條件是小球落地
游戲勝利的條件是磚塊清空
后記
一個簡單的小游戲.
里面卻有很多值得思考的內容.
請各個讀者先自己思考實踐一下,再看代碼.有問題可以評論留言.留言必回
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/247657.html
標籤:python
上一篇:PyQt5入門(六)常用控制元件
