【编程6】贪吃蛇游戏(python+pygame)
發(fā)布時(shí)間:2025/3/19
45
豆豆
生活随笔
收集整理的這篇文章主要介紹了
【编程6】贪吃蛇游戏(python+pygame)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
效果圖~新鮮出爐
一、pygame模塊概覽
模塊名稱功能 pygame.cdrom 訪問(wèn)光驅(qū) pygame.cursors 加載光標(biāo) pygame.display 訪問(wèn)顯示設(shè)備 pygame.draw 繪制形狀、線和點(diǎn) pygame.event 管理事件 pygame.font 使用字體 pygame.image 加載和存儲(chǔ)圖片 pygame.joystick 使用游戲手柄或類似的東西 pygame.key 讀取鍵盤按鍵 pygame.mixer 聲音 pygame.mouse 鼠標(biāo) pygame.movie 播放視頻 pygame.music 播放音頻 pygame.overlay 訪問(wèn)高級(jí)視頻疊加 pygame 目前學(xué)習(xí)的 pygame.rect 管理矩形區(qū)域 pygame.sndarray 操作聲音數(shù)據(jù) pygame.sprite 操作移動(dòng)圖像 pygame.surface 管理圖像和屏幕 pygame.surfarray 管理點(diǎn)陣圖像數(shù)據(jù) pygame.time 管理時(shí)間和幀信息 pygame.transform 縮放和移動(dòng)圖像
二、核心代碼
思路
首頁(yè)面:會(huì)涉及圖片和文字提示的顯示,進(jìn)入(任意鍵)或退出(ESC)游戲; 游戲頁(yè)面:主要涉及食物的創(chuàng)建繪制,蛇的移動(dòng)和顯示,蛇是否吃到食物或者是否撞到邊界或自身,再者就是音效的實(shí)現(xiàn)(背景音樂(lè)+gameover音效); 結(jié)束頁(yè)面:會(huì)涉及圖片和文字提示的顯示,重來(lái)(任意鍵)或退出(ESC)游戲。
核心代碼
def main ( ) : pygame
. init
( ) snake_speed_clock
= pygame
. time
. Clock
( ) screen
= pygame
. display
. set_mode
( ( windows_width
, windows_height
) ) screen
. fill
( white
) pygame
. display
. set_caption
( "貪吃蛇~" ) show_start_info
( screen
) while True : music
( ) running_game
( screen
, snake_speed_clock
) show_end_info
( screen
)
def running_game ( screen
, snake_speed_clock
) : start_x
= random
. randint
( 3 , map_width
- 8 ) start_y
= random
. randint
( 3 , map_height
- 8 ) snake_coords
= [ { 'x' : start_x
, 'y' : start_y
} , { 'x' : start_x
- 1 , 'y' : start_y
} , { 'x' : start_x
- 2 , 'y' : start_y
} ] direction
= RIGHT food
= get_random_location
( ) while True : for event
in pygame
. event
. get
( ) : if event
. type == QUIT
: terminate
( ) elif event
. type == KEYDOWN
: if ( event
. key
== K_LEFT
or event
. key
== K_a
) and direction
!= RIGHT
: direction
= LEFT
elif ( event
. key
== K_RIGHT
or event
. key
== K_d
) and direction
!= LEFT
: direction
= RIGHT
elif ( event
. key
== K_UP
or event
. key
== K_w
) and direction
!= DOWN
: direction
= UP
elif ( event
. key
== K_DOWN
or event
. key
== K_s
) and direction
!= UP
: direction
= DOWN
elif event
. key
== K_ESCAPE
: terminate
( ) move_snake
( direction
, snake_coords
) ret
= snake_is_alive
( snake_coords
) if not ret
: gameover_music
( ) break snake_is_eat_food
( snake_coords
, food
) screen
. fill
( BG_COLOR
) draw_snake
( screen
, snake_coords
) draw_food
( screen
, food
) show_score
( screen
, len ( snake_coords
) - 3 ) pygame
. display
. update
( ) snake_speed_clock
. tick
( snake_speed
)
def draw_food ( screen
, food
) : x
= food
[ 'x' ] * cell_sizey
= food
[ 'y' ] * cell_sizeappleRect
= pygame
. Rect
( x
, y
, cell_size
, cell_size
) pygame
. draw
. rect
( screen
, red
, appleRect
)
def draw_snake ( screen
, snake_coords
) : for coord
in snake_coords
: x
= coord
[ 'x' ] * cell_sizey
= coord
[ 'y' ] * cell_sizewormSegmentRect
= pygame
. Rect
( x
, y
, cell_size
, cell_size
) pygame
. draw
. rect
( screen
, dark_blue
, wormSegmentRect
) wormInnerSegmentRect
= pygame
. Rect
( x
+ 4 , y
+ 4 , cell_size
- 8 , cell_size
- 8 ) pygame
. draw
. rect
( screen
, blue
, wormInnerSegmentRect
)
def move_snake ( direction
, snake_coords
) : if direction
== UP
: newHead
= { 'x' : snake_coords
[ HEAD
] [ 'x' ] , 'y' : snake_coords
[ HEAD
] [ 'y' ] - 1 } elif direction
== DOWN
: newHead
= { 'x' : snake_coords
[ HEAD
] [ 'x' ] , 'y' : snake_coords
[ HEAD
] [ 'y' ] + 1 } elif direction
== LEFT
: newHead
= { 'x' : snake_coords
[ HEAD
] [ 'x' ] - 1 , 'y' : snake_coords
[ HEAD
] [ 'y' ] } elif direction
== RIGHT
: newHead
= { 'x' : snake_coords
[ HEAD
] [ 'x' ] + 1 , 'y' : snake_coords
[ HEAD
] [ 'y' ] } snake_coords
. insert
( 0 , newHead
)
def snake_is_alive ( snake_coords
) : tag
= True if ( snake_coords
[ HEAD
] [ 'x' ] == - 1 \
or snake_coords
[ HEAD
] [ 'x' ] == map_width \
or snake_coords
[ HEAD
] [ 'y' ] == - 1 \
or snake_coords
[ HEAD
] [ 'y' ] == map_height
) : tag
= False for snake_body
in snake_coords
[ 1 : ] : if snake_body
[ 'x' ] == snake_coords
[ HEAD
] [ 'x' ] and snake_body
[ 'y' ] == snake_coords
[ HEAD
] [ 'y' ] : tag
= False return tag
def snake_is_eat_food ( snake_coords
, food
) : if ( snake_coords
[ HEAD
] [ 'x' ] == food
[ 'x' ] and snake_coords
[ HEAD
] [ 'y' ] == food
[ 'y' ] ) : food
[ 'x' ] = random
. randint
( 0 , map_width
- 1 ) food
[ 'y' ] = random
. randint
( 0 , map_height
- 1 ) else : del snake_coords
[ - 1 ]
def get_random_location ( ) : return { 'x' : random
. randint
( 0 , map_width
- 1 ) , 'y' : random
. randint
( 0 , map_height
- 1 ) }
def show_start_info ( screen
) : font
= pygame
. font
. Font
( "simsun.ttc" , 40 ) tip
= font
. render
( '按任意鍵開始游戲' , True , ( 65 , 105 , 255 ) ) gamestart
= pygame
. image
. load
( 'startlogo.jpg' ) . convert
( ) screen
. blit
( gamestart
, ( 140 , 30 ) ) screen
. blit
( tip
, ( 240 , 550 ) ) pygame
. display
. update
( ) while True : for event
in pygame
. event
. get
( ) : if event
. type == QUIT
: terminate
( ) elif event
. type == KEYDOWN
: return if ( event
. key
== K_ESCAPE
) : terminate
( ) else : return
def music ( ) : pygame
. mixer
. init
( ) pygame
. mixer
. music
. load
( '111.mp3' ) pygame
. mixer
. music
. play
( 1 , 0 ) def gameover_music ( ) : pygame
. mixer
. init
( ) pygame
. mixer
. music
. load
( 'gameover.mp3' ) pygame
. mixer
. music
. play
( 1 , 0 )
def show_end_info ( screen
) : font
= pygame
. font
. Font
( "simsun.ttc" , 40 ) tip
= font
. render
( "按ESC退出游戲,按任意鍵重新開始游戲" , True , ( 65 , 105 , 255 ) ) screen
. blit
( tip
, ( 80 , 300 ) ) pygame
. display
. update
( ) while True : for event
in pygame
. event
. get
( ) : if event
. type == QUIT
: terminate
( ) elif event
. type == KEYDOWN
: if event
. key
== K_ESCAPE
: terminate
( ) else : return
def show_score ( screen
, score
) : font
= pygame
. font
. Font
( "simsun.ttc" , 30 ) scoreSurf
= font
. render
( "得分:%s" % score
, True , green
) scoreRect
= scoreSurf
. get_rect
( ) scoreRect
. topleft
= ( windows_width
- 120 , 10 ) screen
. blit
( scoreSurf
, scoreRect
)
def terminate ( ) : pygame
. quit
( ) sys
. exit
( )
總結(jié)
以上是生活随笔 為你收集整理的【编程6】贪吃蛇游戏(python+pygame) 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。