20192221 2019-2020-2 《Python程序設計》實驗四報告
課程:《Python程序設計》
班級:1922班
姓名:葉蕊馨
學號:20192221
實驗教師:王志強老師
實驗日期:2020年6月9日
必修/選修: 公選課
實驗內容選擇
Python綜合應用:爬蟲、數據處理、可視化、機器學習、神經網絡、游戲、網絡安全等。
1.實驗內容
python利用pygame進行簡單的游戲開發(打磚塊)
2. 實驗過程及結果
1.功能划分
1)顯現一個屏幕
- 1.導入所需模塊
- 2.畫出屏幕,並添加進主循環內
(如果點擊關閉窗口,即關閉)
2)畫一個小球在屏幕上移動
- 1.畫出小球
- 2.小球的運動參數
- 3.主循環中小球的運動
3)碰到邊緣要能夠反彈
- 邊緣碰撞檢測
4)擋板繪制和移動
- 繪制
- 擋板的移動
5)磚塊的繪制
- 初始化磚塊數組
- 繪制
6)游戲流程和控制邏輯
- 1.把磚塊打破
- 2.被擋板擋住才返回,落沒擋住則要減少一條生命
- 3.生命用完了則Game Over
7)繪制文字顯示游戲信息
完整代碼
import pygame,random
import sys
import time
from pygame.locals import *
#界面長度寬度定義
width = 640
hight = 480
#界面顏色定義
backcolor = (230,230,230)
#游戲狀態、屬性常量定義
state_init = 0
stata_level = 1
state_run = 2
state_gameover = 3
state_shutdown = 4
state_exit = 5
fps = 25
#游戲狀態
game_state = state_init
blocks = []
life_left = 1
game_over = False
blocks_hit = 0
#擋板和位置顏色
paddle = {'rect':pygame.Rect(0,0,32,8),
'color':(128,64,64)}
#擋板的運動控制
paddle_move_left = False
paddle_move_right = False
#小球的位置和速度
ball_x = 0
ball_y = 0
ball_dx = 0
ball_dy = 0
#界面初始化
pygame.init()
mainClock = pygame.time.Clock()
surface = pygame.display.set_mode((width, hight), 0, 32)
pygame.display.set_caption('打磚塊')
#初始化磚塊數組
def InitBlocks():
blocks = [[1] * 8] * 6
return blocks
#檢測小球與擋板是否碰撞
def ProcessBall(blocks, ball_x, ball_y, paddle):
if (ball_y > hight//2):
if (ball_x+4 >= paddle['rect'].left and \
ball_x-4 <= paddle['rect'].left+32 and \
ball_y+4 >= paddle['rect'].top and \
ball_y-4 <= paddle['rect'].top+8):
return None
#顯示文字
def DrawText(text, font, surface, x, y):
text_obj = font.render(text, 1, (255, 255, 255))
text_rect = text_obj.get_rect()
text_rect.topleft = (x, y)
surface.blit(text_obj, text_rect)
#退出游戲
def Terminate():
pygame.quit()
sys.exit()
#等待用戶輸入
def WaitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
Terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
Terminate()
return
game_start_font = pygame.font.SysFont(None, 48)
game_over_font = pygame.font.SysFont(None, 48)
text_font = pygame.font.SysFont(None, 20)
pygame.display.update()
WaitForPlayerToPressKey()
while True:
#界面設置
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
#事件監聽
if event.key == K_LEFT:
paddle_move_left = True
if event.key == K_RIGHT:
paddle_move_right = True
if event.type == KEYUP:
if event.key == K_LEFT:
paddle_move_left = False
if event.key == K_RIGHT:
paddle_move_right = False
#游戲控制流程
if game_state == state_init:
# 初始化游戲
ball_x = random.randint(8, width - 8)
ball_y = (hight//2)
ball_dx = random.randint(-3, 4)
ball_dy = random.randint(5, 8)
paddle['rect'].left = (width/2 - 16)
paddle['rect'].top = (hight - 32)
paddle_move_left = False
paddle_move_right = False
#life_left = TOTAL_LIFE
game_over = False
blocks_hit = 0
level = 1
game_state = stata_level
#新的一關
elif game_state == stata_level:
blocks = InitBlocks()
game_state = state_run
elif game_state == state_run:
# 游戲運行
# 球的運動
ball_x += ball_dx;
ball_y += ball_dy;
if ball_x > (width - 4) or ball_x < 4:
ball_dx = -ball_dx
ball_x += ball_dx;
elif ball_y < 4:
ball_dy = -ball_dy
ball_y += ball_dy
elif ball_y > hight - 4:
if life_left == 0:
game_state = state_gameover
else:
life_left -= 1
# 初始化游戲
ball_x = paddle['rect'].left + 32 // 2
ball_y = (hight//2)
ball_dx = random.randint(-4, 5)
ball_dy = random.randint(6, 9)
#檢測球與擋板是否碰撞
if ball_y > hight // 2:
if (ball_x + 4 >= paddle['rect'].left and \
ball_x - 4 <= paddle['rect'].left + 32 and \
ball_y + 4 >= paddle['rect'].top and \
ball_y - 4 <= paddle['rect'].top + 8):
ball_dy = - ball_dy
ball_y += ball_dy
if paddle_move_left:
ball_dx -= random.randint(0, 3)
elif paddle_move_right:
ball_dx += random.randint(0, 3)
else:
ball_dx += random.randint(-1, 2)
#檢測球與磚塊是否碰撞
cur_x = 8
cur_y = 8
for row in range(6):
cur_x = 8
for col in range(8):
if blocks[row][col] != 0:
if (ball_x + 4 >= cur_x and \
ball_x - 4 <= cur_x + 64 and \
ball_y + 4 >= cur_y and \
ball_y - 4 <= cur_y + 16):
blocks[row][col] = 0
blocks_hit += 1
ball_dy = -ball_dy
ball_dx += random.randint(-1, 2)
#score += 5 * (level + abs(ball_dx))
cur_x += 80
cur_y += 32
if blocks_hit == 6 * 8:
level += 1
blocks_hit = 0
game_state = stata_level
#擋板的運動
if paddle_move_left:
paddle['rect'].left -= 8
if paddle['rect'].left < 0:
paddle['rect'].left = 0
if paddle_move_right:
paddle['rect'].left += 8
if paddle['rect'].left > width - 32:
paddle['rect'].left = width - 32
#繪制背景
surface.fill(backcolor)
#繪制擋板
pygame.draw.rect(surface, paddle['color'], paddle['rect'])
#繪制小球
pygame.draw.circle(surface,(0,0,255), (ball_x, ball_y), 4, 0)
#繪制磚塊
cur_x = 8
cur_y = 8
for row in range(6):
cur_x = 8
for col in range(8):
if blocks[row][col] != 0:
pygame.draw.rect(surface, (255,128,0),(cur_x, cur_y, 64, 16))
cur_x += 80
cur_y += 32
#文字信息繪制
message = ' Life: ' + str(life_left)
DrawText(message, text_font, surface, 8, (hight - 16))
elif game_state == state_gameover:
DrawText('GAME OVER', game_over_font, surface,
(width / 3), (hight / 3))
DrawText('Press any key to play again.', game_over_font, surface,
(width / 3) - 80, (hight / 3) + 150)
pygame.display.update()
pygame.mixer.music.stop()
WaitForPlayerToPressKey()
game_state = state_init
elif game_state == state_shutdown:
game_state = state_exit
pygame.display.update()
mainClock.tick(30)
2.運行
其他(感悟、思考等)
*1.課程總結
這次的python學習包含序列、文件操作、網絡編程、GUI、模塊、爬蟲以及其它基礎知識點,對我c語言的學習也有很大的幫助,通過python的學習,了解到一些編程語言的思想,尤其是因為這學期的特殊情況,這門課比c語言先開一周,於是先一步了解編程方面的知識,對我后續c語言的學習先行掀開一角,會更加輕松。王志強老師也盡心盡責,出現問題先自己嘗試解決的方法也讓我獲益匪淺。(要善用搜索引擎)
- 2.課程感想體會
經過這一特殊學期的學習,掌握了不少python編程技巧,從最開始對編程一竅不通(因為初高中完全沒有接觸過這一類的學習),到現在的綜合實踐都能成功寫出一個簡單的小游戲
,都是這一學期積累的知識點,從hello world一步步的學習中,踩入計算機的大門;從不知道怎么解決問題,到善於自己網上查找答案。我相信這對於我后續的學習也會有很大的幫助。剛選課的時候學長學姐們都很推薦王志強老師的課,選上后也覺得不虛此行。而對於我這個計算機專業的來說,多學一門語言相信以后對我的幫助會很大。 - 3.意見和建議
因為是網上授課的緣故最開始老師采用的方式是自己找時間看視頻,但后來直接上直播課對我來說可能效果會更好。
建議:
希望如果有多余時間可以課上拓寬一下,講一些比較復雜一點的例子。