日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python打砖块游戏算法设计分析_基于pygame的打砖块游戏,做到一半,不带做了

發布時間:2023/12/16 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python打砖块游戏算法设计分析_基于pygame的打砖块游戏,做到一半,不带做了 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

跟著一個博主做的,前面的變量的定義全是內個哥們的,沒帶任何改動,就做了個界面,背景音樂,繪制了個小球,繪制了擋板

小球可以撞到邊界反彈,然后做了磚塊,定義了一個存放磚塊的列表,,,就沒有下文了

原博主鏈接 Python+pyGame 打磚塊游戲 - Python知識庫 http://lib.csdn.net/article/python/1817

import pygame,sys,time

from pygame.locals import *

import random

#游戲界面

WINDOWWIDTH=640

WINDOWHEIGHT=480

# 一些關于窗口的常量定義

WINDOW_WIDTH = 640

WINDOW_HEIGHT = 480

# 游戲狀態常量定義

GAME_STATE_INIT = 0

GAME_STATE_START_LEVEL = 1

GAME_STATE_RUN = 2

GAME_STATE_GAMEOVER = 3

GAME_STATE_SHUTDOWN = 4

GAME_STATE_EXIT = 5

# 小球的常量定義

BALL_START_Y = (WINDOW_HEIGHT // 2)

BALL_SIZE = 4

# 擋板的常量定義

PADDLE_START_X = (WINDOW_WIDTH / 2 - 16)

PADDLE_START_Y = (WINDOW_HEIGHT - 32);

PADDLE_WIDTH = 32

PADDLE_HEIGHT = 8

# 磚塊的常量定義

NUM_BLOCK_ROWS = 6

NUM_BLOCK_COLUMNS = 8

BLOCK_WIDTH = 64

BLOCK_HEIGHT = 16

BLOCK_ORIGIN_X = 8

BLOCK_ORIGIN_Y = 8

BLOCK_X_GAP = 80

BLOCK_Y_GAP = 32

# 一些顏色常量定義

BACKGROUND_COLOR = (0, 0, 0)

BALL_COLOR = (0, 0, 255)

PADDLE_COLOR = (128, 64, 64)

BLOCK_COLOR = (255, 128, 0)

TEXT_COLOR = (255, 255, 255)

# 游戲的一些屬性信息

TOTAL_LIFE = 5

FPS = 25

# # 初始化磚塊數組

# def InitBlocks():

# # blocks = [[1] * NUM_BLOCK_COLUMNS] * NUM_BLOCK_ROWS

# blocks = []

# for i in range(NUM_BLOCK_ROWS): # @UnusedVarialbe

# blocks.append([i + 1] * NUM_BLOCK_COLUMNS)

# return blocks

# 初始化磚塊數組

def InitBlocks():

blocks = [[1] * NUM_BLOCK_COLUMNS] * NUM_BLOCK_ROWS

return blocks

# 檢測小球是否與擋板或者磚塊碰撞

def ProcessBall(blocks, ball_x, ball_y, paddle):

if (ball_y > WINDOW_HEIGHT // 2):

if (ball_x + BALL_SIZE >= paddle['rect'].left and \

ball_x - BALL_SIZE <= paddle['rect'].left + PADDLE_WIDTH and \

ball_y + BALL_SIZE >= paddle['rect'].top and \

ball_y - BALL_SIZE <= paddle['rect'].top + PADDLE_HEIGHT):

return None

# 顯示文字

def DrawText(text, font, surface, x, y):

text_obj = font.render(text, 1, TEXT_COLOR)

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

# 游戲界面的初始化

pygame.init()

mainClock = pygame.time.Clock()

# 小球的位置和速度

ball_x = 0

ball_y = 0

ball_dx = 0

ball_dy = 0

# 擋板的運動控制

paddle_move_left = False

paddle_move_right = False

# 擋板的位置和顏色

paddle = {'rect': pygame.Rect(0, 0, PADDLE_WIDTH, PADDLE_HEIGHT),

'color': PADDLE_COLOR}

# 游戲狀態

game_state = GAME_STATE_INIT

blocks = []

life_left = TOTAL_LIFE

game_over = False

blocks_hit = 0

score = 0

level = 1

game_start_font = pygame.font.SysFont(None, 48)

game_over_font = pygame.font.SysFont(None, 48)

text_font = pygame.font.SysFont(None, 20)

game_over_sound = pygame.mixer.Sound('game_over.wav')

game_hit_sound = pygame.mixer.Sound('get_point.wav')

pygame.mixer.music.load('bg_music.mp3')

windowSurface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)

pygame.display.set_caption('打磚塊')

DrawText('pyFreakOut', game_start_font, windowSurface,

(WINDOW_WIDTH / 3), (WINDOW_HEIGHT / 3 + 50))

DrawText('Press any key to start.', game_start_font, windowSurface,

(WINDOW_WIDTH / 3) - 60, (WINDOW_HEIGHT) / 3 + 100)

pygame.display.update()

WaitForPlayerToPressKey()

# 播放背景音樂

pygame.mixer.music.play(-1, 0.0)

#游戲主循環

while True:

for event in pygame.event.get():

if event.type==QUIT:

pygame.quit()

exit()

if event.type == KEYDOWN:

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==GAME_STATE_INIT:

ball_x=random.randint(8,WINDOW_WIDTH-8)

ball_y=BALL_START_Y

ball_dx=random.randint(-4,4)

ball_dy=random.randint(6,8)

paddle['rect'].left = PADDLE_START_X

paddle['rect'].top = PADDLE_START_Y

paddle_move_left = False

paddle_move_right = False

game_state=GAME_STATE_START_LEVEL

elif game_state==GAME_STATE_START_LEVEL:

#新的一關

blocks = InitBlocks()

game_state=GAME_STATE_RUN

elif game_state==GAME_STATE_RUN:

ball_x += ball_dx

ball_y += ball_dy

if ball_x > (WINDOW_WIDTH - BALL_SIZE) or ball_x < BALL_SIZE:

ball_dx = -ball_dx

ball_x += ball_dx;

elif ball_y > (WINDOW_HEIGHT - BALL_SIZE) or ball_y < BALL_SIZE:

ball_dy = -ball_dy

ball_y += ball_dy

# 擋板的運動

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 > WINDOW_WIDTH - PADDLE_WIDTH:

paddle['rect'].left = WINDOW_WIDTH - PADDLE_WIDTH

#繪制過程

windowSurface.fill(BACKGROUND_COLOR)

#繪制擋板

pygame.draw.rect(windowSurface, paddle['color'], paddle['rect'])

#繪制小球

pygame.draw.circle(windowSurface, BALL_COLOR, (ball_x, ball_y), BALL_SIZE, 0)

#繪制磚塊

cur_x = BLOCK_ORIGIN_X

cur_y = BLOCK_ORIGIN_Y

for row in range(NUM_BLOCK_ROWS):

cur_x = BLOCK_ORIGIN_X

for col in range(NUM_BLOCK_COLUMNS):

#print(blocks[row][col])

if (blocks[row][col] == 1):

pygame.draw.rect(windowSurface, BLOCK_COLOR,

(cur_x, cur_y, BLOCK_WIDTH, BLOCK_HEIGHT))

cur_x += BLOCK_X_GAP

cur_y += BLOCK_Y_GAP

elif game_state == GAME_STATE_SHUTDOWN:

game_state = GAME_STATE_EXIT

pygame.display.update()

mainClock.tick(30)

總結

以上是生活随笔為你收集整理的python打砖块游戏算法设计分析_基于pygame的打砖块游戏,做到一半,不带做了的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。