用Python和Pygame寫遊戲 「貪吃蛇」

2021-08-19 17:19:05 字數 3289 閱讀 2180

# -*- coding: utf-8 -*-

import pygame,sys,random

from pygame.locals import *

# 定義顏色變數

redcolour = pygame.color(255,0,0)

blackcolour = pygame.color(0,0,0)

whitecolour = pygame.color(255,255,255)

greycolour = pygame.color(150,150,150)

def gameover():

pygame.quit()

sys.exit()

# 定義main函式

def main():

# 初始化pygame

pygame.init()

fpsclock = pygame.time.clock()

# 建立pygame顯示層

playsu***ce = pygame.display.set_mode((640,480))

pygame.display.set_caption('貪吃蛇')

#初始化變數

snakeposition = [100,100]

snakebody = [[100,100],[80,100],[60,100]]

targetposition = [300,300]

#定義標記,用來吃掉目標方塊塊塊

targetflag = 1

#direction = 'right'

changedirection = direction

while true:

#pygame.event 事件 檢測例如按鍵等pygame事件

for event in pygame.event.get():

if event.type == quit:

pygame.quit()

sys.exit()

elif event.type == keydown:

# 判斷鍵盤事件

if event.key == k_right or event.key == ord('d'):

changedirection = 'right'

if event.key == k_left or event.key == ord('a'):

changedirection = 'left'

if event.key == k_up or event.key == ord('w'):

changedirection = 'up'

if event.key == k_down or event.key == ord('s'):

changedirection = 'down'

if event.key == k_escape:

pygame.event.post(pygame.event.event(quit))

# 判斷是否輸入了反方向

if changedirection == 'left' and not direction == 'right':

direction = changedirection

if changedirection == 'right' and not direction == 'left':

direction = changedirection

if changedirection == 'up' and not direction == 'down':

direction = changedirection

if changedirection == 'down' and not direction == 'up':

direction = changedirection

# 根據方向移動蛇頭的座標

if direction == 'right':

snakeposition[0] += 20

if direction == 'left':

snakeposition[0] -= 20

if direction == 'up':

snakeposition[1] -= 20

if direction == 'down':

snakeposition[1] += 20

# 增加蛇的長度

snakebody.insert(0,list(snakeposition))

# 判斷是否吃掉了樹莓

if snakeposition[0] == targetposition[0] and snakeposition[1] == targetposition[1]:

targetflag = 0

else:

snakebody.pop()

# 如果吃掉樹莓,則重新生成樹莓

if targetflag == 0:

x = random.randrange(1,32)

y = random.randrange(1,24)

targetposition = [int(x*20),int(y*20)]

targetflag = 1

# 繪製pygame顯示層

playsu***ce.fill(blackcolour)

for  position in snakebody:

pygame.draw.rect(playsu***ce,whitecolour,rect(position[0],position[1],20,20))#蛇

pygame.draw.rect(playsu***ce, redcolour, rect(targetposition[0], targetposition[1], 20, 20))  # 蛇

# 重新整理pygame顯示層

pygame.display.flip()

# 判斷是否死亡

if snakeposition[0] > 620 or snakeposition[0] <0:

gameover()

if snakeposition[1] > 460 or snakeposition[1] < 0:

for snakebody in snakesegments[1:]:

if snakeposition[0] == snakebody[0] and snakeposition[1] == snalebody[1]:

gameover()

# 控制遊戲速度

fpsclock.tick(4)

if __name__ == "__main__":

main()

用Python和Pygame寫遊戲 入門

windows7 下安裝pygame 安裝好,可以用下面的方法確認有沒有安裝成功 import pygame print pygame.ver 2.新的hello world coding utf 8 背景 游標 mouse image filename fugu.png import pygame...

初學Python和pygame寫小遊戲

alien.py 外星人的類,負責外星人的載入 位置 繪製和更新等屬性。alien invasion.py 主函式。bullet.py 對子彈進行管理的類,負責子彈的繪製 更新。button.py 按鈕類,繪製play按鈕。game function.py 包含實現各種操作所需的函式。game da...

Python學習模組 Pygame寫遊戲

第乙個例項程式 建立 main game.py 一張背景 001.jpg 一張跟隨滑鼠!初始化pygame,為使用硬體做準備 pygame.init 建立乙個視窗 screen pygame.display.set mode 640,480 0,32 設定視窗標題 pygame.display.se...