import pygame, sys, time, random
from pygame.locals import *
redColour = pygame.Color(255, 0, 0)
greenColour = pygame.Color(0, 255, 0)
blueColour = pygame.Color(0, 0, 255)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
yellowColour =pygame.Color(255, 255, 0)
purpleColour =pygame.Color(255, 0, 255)
brownColour =pygame.Color(150, 75, 75)
DeepPinkColour=pygame.Color(255 ,20 ,147)
#初始化並播放背景音樂
#pygame.mixer.init() #初始化混音器
#pygame.mixer.music.load('Ken Arai - NEXT TO YOU.mp3') #加載背景音樂
#pygame.mixer.music.set_volume(0.2) #設置音量
#pygame.mixer.music.play() #播放背景音樂
# 定義遊戲結束函數。當雙方長度相等且頭頭相撞時,爲平局。遊戲結束,並顯示雙方分數。
def gameOver (playSurface,score1,score2):
gameOverFont = pygame.font.SysFont('arial', 32)
gameOverSurf = gameOverFont.render('tie '+'pink:'+str(score1-3)+' '+'blue:'+str(score2-3), True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
main()
# 定義遊戲結束函數。當1號玩家撞牆或被2號玩家吃了時,遊戲結束,顯示1號玩家失敗,以及雙方分數。
def gameOver1(playSurface,score1,score2):
gameOverFont = pygame.font.SysFont('arial', 32)
gameOverSurf = gameOverFont.render('green snake Game Over '+'pink:'+str(score1-3)+' '+'blue:'+str(score2-3), True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
main()
# 定義遊戲結束函數。當2號玩家撞牆或被1號玩家吃了時,遊戲結束,顯示2號玩家失敗,以及雙方分數。
def gameOver2(playSurface,score1,score2):
gameOverFont = pygame.font.SysFont('arial', 32)
gameOverSurf = gameOverFont.render(' blue snake Game Over '+'pink'+str(score1-3)+' '+'blue:'+str(score2-3), True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
main()
def main():
pygame.init() #pygame初始化
#顯示歡迎界面,有開始遊戲和退出遊戲兩個選項
title_font = pygame.font.SysFont('arial', 32)
welcome_words = title_font.render('Welcome to My Snake', True, (0, 0, 0), (255, 255, 255))
tips_font = pygame.font.SysFont('arial', 24)
start_game_words = tips_font.render('Click to Start Game', True, (0, 0, 0), (255, 255, 255))
close_game_words = tips_font.render('Press ESC to Close', True, (0, 0, 0), (255, 255, 255))
playSurface = pygame.display.set_mode((640, 480), FULLSCREEN, 32) #全屏顯示
fpsClock = pygame.time.Clock()
pygame.display.set_caption('貪喫蛇') #歡迎界面,此時未開始遊戲
game_started = False
#初始化兩條蛇的起始位置和長度
snakePosition1 = [100, 100]
snakeSegments1 = [[100, 100], [80, 100], [60, 100]]
snakePosition2 = [200, 200]
snakeSegments2 = [[200, 200], [180, 200], [160, 200]]
#初始化樹莓的起始位置
raspberryPosition = [300, 300]
raspberrySpawned = 1
#初始化兩條蛇的起始方向
direction1 = 'right'
direction2 = 'right'
changeDirection1 = direction1
changeDirection2 = direction2
while True: #遊戲循環主體
for event in pygame.event.get(): #獲取事件
if event.type == QUIT:
sys.exit()
elif event.type == KEYDOWN: #鍵盤輸入
#方向鍵控制玩家1
if event.key == K_RIGHT :
changeDirection1 = 'right'
if event.key == K_LEFT:
changeDirection1 = 'left'
if event.key == K_UP :
changeDirection1 = 'up'
if event.key == K_DOWN :
changeDirection1 = 'down'
#‘W’‘A’‘S’‘D’控制玩家2
if event.key == ord('d'):
changeDirection2 = 'right'
if event.key == ord('a'):
changeDirection2= 'left'
if event.key == ord('w'):
changeDirection2 = 'up'
if event.key == ord('s'):
changeDirection2 = 'down'
if event.key == K_ESCAPE: #按動ESC退出遊戲
pygame.event.post(pygame.event.Event(QUIT))
elif (not game_started) and event.type == pygame.MOUSEBUTTONDOWN: #在遊戲歡迎界面時,根據鼠標位置判斷是否開始遊戲
x, y = pygame.mouse.get_pos()
if 213 <= x <= 422 and 304 <= y <= 342:
game_started = True
playSurface.fill((255, 255, 255)) #遊戲畫面背景爲白色
if game_started: #開始遊戲
# 判斷是否輸入了反方向,如果輸入相反方向,則方向不改變
if changeDirection1 == 'right' and direction1 != 'left':
direction1 = changeDirection1
if changeDirection1 == 'left' and direction1 != 'right':
direction1 = changeDirection1
if changeDirection1 == 'up' and direction1 != 'down':
direction1 = changeDirection1
if changeDirection1 == 'down' and direction1 != 'up':
direction1 = changeDirection1
if changeDirection2 == 'right' and direction2 != 'left':
direction2 = changeDirection2
if changeDirection2 == 'left' and direction2 != 'right':
direction2 = changeDirection2
if changeDirection2 == 'up' and direction2 != 'down':
direction2 = changeDirection2
if changeDirection2 == 'down' and direction2 != 'down':
direction2 = changeDirection2
# 根據方向移動蛇頭的座標
if direction1 == 'right':
snakePosition1[0] += 20
if direction1 == 'left':
snakePosition1[0] -= 20
if direction1 == 'up':
snakePosition1[1] -= 20
if direction1 == 'down':
snakePosition1[1] += 20
if direction2 == 'right':
snakePosition2[0] += 20
if direction2 == 'left':
snakePosition2[0] -= 20
if direction2 == 'up':
snakePosition2[1] -= 20
if direction2 == 'down':
snakePosition2[1] += 20
# 增加蛇的長度
snakeSegments1.insert(0, list(snakePosition1))
snakeSegments2.insert(0, list(snakePosition2))
# 判斷是否喫掉了樹莓
if snakePosition1[0] == raspberryPosition[0] and snakePosition1[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments1.pop()
if snakePosition2[0] == raspberryPosition[0] and snakePosition2[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments2.pop()
# 如果喫掉樹莓,則重新生成樹莓
if raspberrySpawned == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
raspberryPosition = [int(x * 20), int(y * 20)] #先隨機在任意位置生成一個樹莓
while raspberryPosition in snakePosition1 or raspberryPosition in snakePosition2: #判斷樹莓是否聲稱在蛇的身體上,如果是,則重新生成樹莓
x = random.randrange(1, 32)
y = random.randrange(1, 24)
raspberryPosition = [int(x * 20), int(y * 20)]
raspberrySpawned = 1
# 刷新pygame顯示層,顯示兩條蛇以及樹莓
playSurface.fill(whiteColour)
for position in snakeSegments1:
pygame.draw.rect(playSurface, DeepPinkColour, Rect(position[0], position[1], 20, 20))
for position in snakeSegments2:
pygame.draw.rect(playSurface, blueColour, Rect(position[0], position[1], 20, 20))
colour = random.choice([yellowColour, purpleColour, brownColour, blackColour])
pygame.draw.rect(playSurface, colour, Rect(raspberryPosition[0], raspberryPosition[1], 20, 20))
pygame.display.flip()
# 蛇的位置超出邊框就算死亡,或者和另一條蛇相撞。
score1 = len(snakeSegments1) #分數
score2 = len(snakeSegments2)
if snakePosition1[0] > 620 or snakePosition1[0] < 0:
gameOver1(playSurface, score1, score2)
if snakePosition1[1] > 460 or snakePosition1[1] < 0:
gameOver1(playSurface, score1, score2)
for snakeBody1 in snakeSegments1[1:]: # 玩家二撞上了玩家一,玩家二失敗
if snakePosition2[0] == snakeBody1[0] and snakePosition2[1] == snakeBody1[1]:
gameOver2(playSurface, score1, score2)
if snakePosition2[0] > 620 or snakePosition2[0] < 0:
gameOver2(playSurface, score1, score2)
if snakePosition2[1] > 460 or snakePosition2[1] < 0:
gameOver2(playSurface, score1, score2)
for snakeBody2 in snakeSegments2[1:]: # 玩家一撞上了玩家二,玩家一失敗
if snakePosition1[0] == snakeBody2[0] and snakePosition1[1] == snakeBody2[1]:
gameOver1(playSurface, score1, score2)
if snakePosition1[0] == snakePosition2[0] and snakePosition1[1] == snakePosition2[1]: # 頭碰頭,誰長誰贏,否則平局。
if len(snakeSegments1) > len(snakeSegments2):
gameOver2(playSurface, score1, score2)
elif len(snakeSegments1) < len(snakeSegments2):
gameOver1(playSurface, score1, score2)
else:
gameOver(playSurface, score1, score2)
else: # 遊戲沒有開始,顯示歡迎界面
playSurface.blit(welcome_words, (188, 100))
playSurface.blit(start_game_words, (236, 310))
playSurface.blit(close_game_words, (233, 350))
pygame.display.update()
fpsClock.tick(5)
if __name__ == "__main__":
main()
【pygame】一個精簡版的雙人貪喫蛇
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.