python小遊戲-移動木板
一、遊戲簡介
本遊戲是通過python編寫的小遊戲,給初學者熟悉python程式語言拋磚引玉,希望有所幫助。
成型的效果圖如下:
二、編寫步驟
1.引入庫
程式碼如下:
###### AUTHOR:破繭狂龍 ######
###### DATE:20201002 ######
###### DESCRIPTION:移動的木板 ######
import pygame
from pygame.locals import *
import sys
import time
import random
2.初始化
程式碼如下:
pygame.init()
BLACK = (0, 0, 0) # 黑色
WHITE = (255, 255, 255) # 白色
bg_color = (0,0,70) # 背景顏色
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
smallText = pygame.font.SysFont('SimHei', 20) #comicsansms
midlText = pygame.font.SysFont('SimHei', 50)
barsize = [30, 10]
SCREEN_SIZE = [400, 500] # 螢幕大小
BALL_SIZE = [15, 15] # 球的尺寸
fontcolor = (255,255,255) # 定義字型的顏色
myimg = r"img\b1.jpg"
background = pygame.image.load(myimg) # 圖片位置
background = pygame.transform.scale(background, SCREEN_SIZE)
# ball 初始位置
ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2
ball_pos_y = 0
# ball 移動方向
ball_dir_y = 1 # 1:down
ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
clock = pygame.time.Clock() # 定時器
screen = pygame.display.set_mode(SCREEN_SIZE)
# 設定標題
pygame.display.set_caption('python小遊戲-移動木板')
# 設定圖示
image = pygame.image.load(myimg)
pygame.display.set_icon(image)
3.相關自定義函式
程式碼如下:
###### 自定義函式 ######
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(textSurf, textRect)
def text_objects(text, font):
textSurface = font.render(text, True, fontcolor)
return textSurface, textSurface.get_rect()
def quitgame():
pygame.quit()
quit()
def message_diaplay(text):
largeText = pygame.font.SysFont('SimHei', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((screen[0] / 2), (screen[1] / 2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
4.相關自定義函式
程式碼如下:
def game_first_win():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(bg_color)
###遊戲名稱
TextSurf, TextRect = text_objects('移動木板', midlText)
TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
screen.blit(TextSurf, TextRect)
###作者
TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:破繭狂龍', smallText)
TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
screen.blit(TextSurf_ZZ, TextRect_ZZ)
button("開始", 60, 400, 100, 50, green, bright_green, game_loop)
button("取消", 230, 400, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
###### 移動的木板遊戲類 ######
def game_loop():
pygame.mouse.set_visible(1) # 移動滑鼠不可見
###變數###
score = 0 #分數
count_O = 0 #迴圈的次數變數1 用於統計等級
count_N = 0 #迴圈的次數變數2 用於統計等級
c_level = 1 #等級
x_change = 0 #移動的變數
x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
y = SCREEN_SIZE[1] - barsize[1]
# ball 初始位置
ball_pos_pz = ball_pos
while True:
bar_move_left = False
bar_move_right = False
###當每次滿X分後,升級等級
if count_O != count_N and score % 5 == 0:
c_level += 1
count_O = count_N
###### 獲取鍵盤輸入 ######
for event in pygame.event.get():
if event.type == QUIT: # 當按下關閉按鍵
pygame.quit()
sys.exit() # 接收到退出事件後退出程式
elif event.type == KEYDOWN:
##按鍵盤Q鍵 暫停
if event.key == pygame.K_q:
time.sleep(10)
##左移動
if event.key == pygame.K_LEFT:
bar_move_left = True
x_change = -30
else:
bar_move_left = False
##右移動
if event.key == pygame.K_RIGHT:
bar_move_right = True
x_change = +30
else:
bar_move_right = False
if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
bar_move_left = False
bar_move_right = False
##木板的位置移動
if bar_move_left == True and bar_move_right == False:
x += x_change
if bar_move_left == False and bar_move_right == True:
x += x_change
##填充背景
screen.blit(background, (0, 0)) # (0,0)代表圖片位置起點x 軸 Y軸
##獲取最新的木板位置,並渲染在前臺
bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
bar_pos.left = x
pygame.draw.rect(screen, WHITE, bar_pos)
## 球移動,並渲染在前臺
ball_pos_pz.bottom += ball_dir_y * 3
pygame.draw.rect(screen, WHITE, ball_pos_pz)
## 判斷球是否落到板上
if bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
score += 1 # 分數每次加1
count_N += 1
elif bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
print("Game Over: ", score)
return score
## 更新球下落的初始位置
if bar_pos.top <= ball_pos_pz.bottom:
ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
######### 顯示遊戲等級 #########
TextSurf_lev, TextRect_lev = text_objects("等級 : " + str(c_level), smallText)
TextRect_lev.center = (60, 20)
screen.blit(TextSurf_lev, TextRect_lev)
######### 顯示分數結果 #########
TextSurf_sco, TextRect_sco = text_objects("分數 : " + str(score), smallText)
TextRect_sco.center = (60, 50)
screen.blit(TextSurf_sco, TextRect_sco)
pygame.display.update() # 更新軟體介面顯示
clock.tick(60)
# 三、完整的程式碼
程式碼如下:
###### AUTHOR:破繭狂龍 ######
###### DATE:20201002 ######
###### DESCRIPTION:移動的木板 ######
import pygame
from pygame.locals import *
import sys
import time
import random
pygame.init()
BLACK = (0, 0, 0) # 黑色
WHITE = (255, 255, 255) # 白色
bg_color = (0,0,70) # 背景顏色
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
smallText = pygame.font.SysFont('SimHei', 20) #comicsansms
midlText = pygame.font.SysFont('SimHei', 50)
barsize = [30, 10]
SCREEN_SIZE = [400, 500] # 螢幕大小
BALL_SIZE = [15, 15] # 球的尺寸
fontcolor = (255,255,255) # 定義字型的顏色
myimg = r"img\b1.jpg"
background = pygame.image.load(myimg) # 圖片位置
background = pygame.transform.scale(background, SCREEN_SIZE)
# ball 初始位置
ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2
ball_pos_y = 0
# ball 移動方向
ball_dir_y = 1 # 1:down
ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
clock = pygame.time.Clock() # 定時器
screen = pygame.display.set_mode(SCREEN_SIZE)
# 設定標題
pygame.display.set_caption('python小遊戲-移動木板')
# 設定圖示
image = pygame.image.load(myimg)
pygame.display.set_icon(image)
###### 自定義函式 ######
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(textSurf, textRect)
def text_objects(text, font):
textSurface = font.render(text, True, fontcolor)
return textSurface, textSurface.get_rect()
def quitgame():
pygame.quit()
quit()
def message_diaplay(text):
largeText = pygame.font.SysFont('SimHei', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((screen[0] / 2), (screen[1] / 2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def game_first_win():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(bg_color)
###遊戲名稱
TextSurf, TextRect = text_objects('移動木板', midlText)
TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
screen.blit(TextSurf, TextRect)
###作者
TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:破繭狂龍', smallText)
TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
screen.blit(TextSurf_ZZ, TextRect_ZZ)
button("開始", 60, 400, 100, 50, green, bright_green, game_loop)
button("取消", 230, 400, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
###### 移動的木板遊戲類 ######
def game_loop():
pygame.mouse.set_visible(1) # 移動滑鼠不可見
###變數###
score = 0 #分數
count_O = 0 #迴圈的次數變數1 用於統計等級
count_N = 0 #迴圈的次數變數2 用於統計等級
c_level = 1 #等級
x_change = 0 #移動的變數
x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
y = SCREEN_SIZE[1] - barsize[1]
# ball 初始位置
ball_pos_pz = ball_pos
while True:
bar_move_left = False
bar_move_right = False
###當每次滿X分後,升級等級
if count_O != count_N and score % 5 == 0:
c_level += 1
count_O = count_N
###### 獲取鍵盤輸入 ######
for event in pygame.event.get():
if event.type == QUIT: # 當按下關閉按鍵
pygame.quit()
sys.exit() # 接收到退出事件後退出程式
elif event.type == KEYDOWN:
##按鍵盤Q鍵 暫停
if event.key == pygame.K_q:
time.sleep(10)
##左移動
if event.key == pygame.K_LEFT:
bar_move_left = True
x_change = -30
else:
bar_move_left = False
##右移動
if event.key == pygame.K_RIGHT:
bar_move_right = True
x_change = +30
else:
bar_move_right = False
if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
bar_move_left = False
bar_move_right = False
##木板的位置移動
if bar_move_left == True and bar_move_right == False:
x += x_change
if bar_move_left == False and bar_move_right == True:
x += x_change
##填充背景
screen.blit(background, (0, 0)) # (0,0)代表圖片位置起點x 軸 Y軸
##獲取最新的木板位置,並渲染在前臺
bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
bar_pos.left = x
pygame.draw.rect(screen, WHITE, bar_pos)
## 球移動,並渲染在前臺
ball_pos_pz.bottom += ball_dir_y * 3
pygame.draw.rect(screen, WHITE, ball_pos_pz)
## 判斷球是否落到板上
if bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
score += 1 # 分數每次加1
count_N += 1
elif bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
print("Game Over: ", score)
return score
## 更新球下落的初始位置
if bar_pos.top <= ball_pos_pz.bottom:
ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
######### 顯示遊戲等級 #########
TextSurf_lev, TextRect_lev = text_objects("等級 : " + str(c_level), smallText)
TextRect_lev.center = (60, 20)
screen.blit(TextSurf_lev, TextRect_lev)
######### 顯示分數結果 #########
TextSurf_sco, TextRect_sco = text_objects("分數 : " + str(score), smallText)
TextRect_sco.center = (60, 50)
screen.blit(TextSurf_sco, TextRect_sco)
pygame.display.update() # 更新軟體介面顯示
clock.tick(60)
####程式執行順序######
game_first_win()
game_loop()
pygame.quit()
相關文章
- python——批次移動檔案Python
- Python小遊戲2048Python遊戲
- python之批次移動檔案Python
- 關於Python小遊戲程式Python遊戲
- python手機版做小遊戲程式碼大全-python簡單小遊戲程式碼 怎麼用Python製作簡單小遊戲...Python遊戲
- python移動檔案指標seekPython指標
- python實現超級瑪麗小遊戲(動圖演示+原始碼分享)Python遊戲原始碼
- python爆破一筆畫小遊戲Python遊戲
- [實戰]用flutter編寫炸彈人小遊戲同時支援web和移動端Flutter遊戲Web
- 【Python】 Python小遊戲-貪吃蛇大冒險Python遊戲
- 如何用python編寫猜拳小遊戲?Python遊戲
- Python製作太空射擊小遊戲!Python遊戲
- Python魂鬥羅小遊戲原始碼Python遊戲原始碼
- 微信互動小遊戲:有創意的抽獎小遊戲怎麼做?遊戲
- 用Python繪製移動均線【含原始碼】Python原始碼
- Python練習01-對戰小遊戲Python遊戲
- Python實現三子棋小遊戲Python遊戲
- Python程式碼實現“FlappyBird”小遊戲PythonAPP遊戲
- python之掃雷小遊戲(附程式碼)Python遊戲
- Python寫個“點球大戰”小遊戲Python遊戲
- Python Appium介紹以及移動端自動化測試工具PythonAPP
- 互動答題小遊戲怎麼做?遊戲
- 阿里移動推薦,新人離線賽-python實現阿里Python
- 輕鬆玩轉Python檔案操作:移動、刪除Python
- 學習Python中的一些小遊戲Python遊戲
- iterator移動
- 棋子移動
- 教你怎麼使用ps將木板素材新增凸陷文字效果(技巧分享)
- 小遊戲如何讓使用者主動分享?遊戲
- IAA小遊戲公開課,報名啟動!遊戲
- 移動面積演算法——捕捉移動波峰演算法
- 移動端web整理 移動端問題總結,移動web遇到的那些坑Web
- Python爬取CSDN部落格專家系列——移動開發Python移動開發
- 12歲的少年教你用Python做小遊戲Python遊戲
- 移動的微博,移動的網際網路薦
- KVM線上遷移(動態遷移)
- 用Python寫個魂鬥羅,另附30個Python小遊戲原始碼Python遊戲原始碼
- 移動檔案