生活随笔
收集整理的這篇文章主要介紹了
【编程6】贪吃蛇游戏(python+pygame)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
效果圖~新鮮出爐
一、pygame模塊概覽
模塊名稱功能 pygame.cdrom 訪問光驅 pygame.cursors 加載光標 pygame.display 訪問顯示設備 pygame.draw 繪制形狀、線和點 pygame.event 管理事件 pygame.font 使用字體 pygame.image 加載和存儲圖片 pygame.joystick 使用游戲手柄或類似的東西 pygame.key 讀取鍵盤按鍵 pygame.mixer 聲音 pygame.mouse 鼠標 pygame.movie 播放視頻 pygame.music 播放音頻 pygame.overlay 訪問高級視頻疊加 pygame 目前學習的 pygame.rect 管理矩形區域 pygame.sndarray 操作聲音數據 pygame.sprite 操作移動圖像 pygame.surface 管理圖像和屏幕 pygame.surfarray 管理點陣圖像數據 pygame.time 管理時間和幀信息 pygame.transform 縮放和移動圖像
二、核心代碼
思路
首頁面:會涉及圖片和文字提示的顯示,進入(任意鍵)或退出(ESC)游戲; 游戲頁面:主要涉及食物的創建繪制,蛇的移動和顯示,蛇是否吃到食物或者是否撞到邊界或自身,再者就是音效的實現(背景音樂+gameover音效); 結束頁面:會涉及圖片和文字提示的顯示,重來(任意鍵)或退出(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
( )
總結
以上是生活随笔 為你收集整理的【编程6】贪吃蛇游戏(python+pygame) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。