今天要來分享之前用pygame去寫的貪吃蛇遊戲
首先先訂下簡單的規則
1.基於生存競爭的原則,兩條蛇要共同去搶食(一次只會出現一個食物)
為了平衡不讓大者恆大,所以咬到別人,別人會變小
2.咬到自己算輸
3.碰到牆不會輸,會穿到對面去
4.成績計算,初始值2(就是至少2格)加上吃到及扣掉的數目
5.綠色食物(隨機出現且固定位置直到被吃掉)會加一格
6.紅色(有毒)食物(在畫面中隨機移動)會減短
7.黑色食物(在畫面中隨機移動)會隨機轉向
8.橘色食物(在畫面中隨機移動且不斷閃爍)可以連續便長直到蛇身不再碰到

再來畫流程圖

決定好上述規則後再來就是程式碼

import pygame, sys
from pygame.locals import *
import random
pygame.init()
FPS = 30 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
DISPLAYSURF = pygame.display.set_mode((401, 401), 0, 32)
#螢幕大小
pygame.display.set_caption('Animation')
#顏色設定
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
YELLOW = (255, 255, 0)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
ORANGE = (255, 127, 0)
#方向設定
RIGHT=[1,0]
UP=[0,-1]
LEFT=[-1,0]
DOWN=[0,1]
#判斷有無吃到
global eat
eat = False
font = pygame.font.SysFont('arial', 16)
#畫出螢幕
def draw_screen():
DISPLAYSURF.fill(WHITE)
for i in range(21):
pygame.draw.line(DISPLAYSURF, BLACK, (0+i*20, 0), (0+i*20, 400), 1)
for i in range(21):
pygame.draw.line(DISPLAYSURF, BLACK, (0, 0+i*20), (399, 0+i*20), 2)
#顯示文字
def show_text(text, x, y):#專門顯示文字的方法,除了顯示文字還能指定顯示的位置
x = x
y = y
text = font.render(text, True, (255, 255, 255))
screen.blit(text, (x, y))
pygame.display.update()
#艷麗方塊物件
class Block():
def __init__(self, x, y, width, height,color):
self.x = x
self.y = y
self.color = color
self.rect = pygame.rect.Rect((self.x*20, self.y*20, width, height))
#位置顏色初始化
def draw(self):
pygame.draw.rect(DISPLAYSURF, self.color, self.rect)
def move(self, vect):
self.rect.move_ip((vect[0]-self.x)*20, (vect[1]-self.y)*20)
self.x = vect[0]
self.y = vect[1]
#建立蛇物件
class Snake():
def __init__(self,x,y,color,dir):
self.time=200
self.delta=200
self.headx=x
self.heady=y
self.color=color
self.colide=False
self.selfcolide=False
self.body=[]
self.body.append(Block(self.headx,self.heady,20,20,self.color))
self.d=dir
#參數初始化
def add_block(self,x,y):
self.body.append(Block(x,y,20,20,self.color))
#吃到食物會加長
def move(self):
self.headx=(self.headx+self.d[0])%20
self.heady=(self.heady+self.d[1])%20
tmp=self.body.pop()
tmp.move([self.headx,self.heady])
self.body.insert(0,tmp)
#被咬到會縮短
for i in range(len(s.body)):
if s.body[i].rect.colliderect(s1.body[0].rect):
#s1.colide=True
if len(s.body) > 3:
s.body.pop()
print(len(s.body)-2)
break
for i in range(len(s1.body)):
if s1.body[i].rect.colliderect(s.body[0].rect):
#s.colide=True
if len(s1.body) > 3:
s1.body.pop()
print(len(s1.body)-2)
break
#咬到自己會輸
for i in range(1,len(s.body)):
if s.body[i].rect.colliderect(s.body[0].rect):
s.selfcolide=True
for i in range(1,len(s1.body)):
if s1.body[i].rect.colliderect(s1.body[0].rect):
s1.selfcolide=True
#畫出蛇和她移動
def draw(self):
if ((pygame.time.get_ticks()>self.time) and not self.selfcolide):
self.move()
self.time=self.time+self.delta
for i in range(len(self.body)):
self.body[i].draw()
#建立食物物件
class food():
def __init__(self, x, y, color):
self.time=200
self.delta=200
self.x=x
self.y=y
self.color=color
def draw(self):
global eat
if eat:
self.x=random.randrange(0,17,1)
self.y=random.randrange(0,17,1)
eat = False
self.body=Block(self.x,self.y,20,20,self.color)
self.body.draw()
draw_screen()
#建立兩條蛇
s=Snake(55,55,BLUE,RIGHT)
s1=Snake(30,30,YELLOW,LEFT)
s2=Snake(40,40,RED,RIGHT)
s3=Snake(random.randrange(0,17,1),random.randrange(0,17,1),ORANGE,UP)
s5=Snake(random.randrange(0,17,1),random.randrange(0,17,1),BLACK,DOWN)
s.draw()
s1.draw()
s2.draw()
s3.draw()
s5.draw()
#初始長度為2
s1.add_block(60,60)
s.add_block(30,30)
f = food(15,15,GREEN)
print("BLUE = ")
print(len(s.body)-2)
print("YELLOW = ")
print(len(s1.body)-2)
q=20
#計算分數
direction = [RIGHT, UP, LEFT, DOWN]
B=0
while True: # the main game loop
DISPLAYSURF.fill(WHITE)
#show_text("text", 55, 55)
global eat
B=B+1
if B > 10:
B=0
s3.headx=random.randrange(0,17,1)
s3.heady=random.randrange(0,17,1)
s.draw()
s1.draw()
s2.draw()
s3.draw()
s5.draw()
D = random.randrange(0,3,1)
D1 = random.randrange(0,3,1)
D2 = random.randrange(0,3,1)
s2.d = direction[D]
s3.d = direction[D1]
s5.d = direction[D2]
#畫出食物
f.draw()
#判斷有無吃到
if s.headx == f.x and s.heady == f.y:
eat = True
s.add_block(30,30)
print("BLUE = ")
print(len(s.body)-2)
if s1.headx == f.x and s1.heady == f.y:
eat = True
s1.add_block(30,30)
print("YELLOW = ")
print(len(s1.body)-2)
if s.headx == s3.headx and s.heady == s3.heady:
s.add_block(30,30)
s.add_block(30,30)
print("BLUE = ")
print(len(s.body)-2)
if s1.headx == s3.headx and s1.heady == s3.heady:
s1.add_block(30,30)
s1.add_block(30,30)
print("YELLOW = ")
print(len(s1.body)-2)
if s.headx == s2.headx and s.heady == s2.heady:
if len(s.body) > 2:
s.body.pop()
print("BLUE = ")
print(len(s.body)-2)
if s1.headx == s5.headx and s1.heady == s5.heady:
s1.d = direction[D1]
if s.headx == s5.headx and s.heady == s5.heady:
s.d = direction[D1]
if s1.headx == s2.headx and s1.heady == s2.heady:
if len(s1.body) > 2:
s1.body.pop()
print("YELLOW = ")
print(len(s1.body)-2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#兩隻蛇的移動方式
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN and s.d != UP:
s.d=DOWN
if event.key == pygame.K_UP and s.d != DOWN:
s.d=UP
if event.key == pygame.K_LEFT and s.d != RIGHT:
s.d=LEFT
if event.key == pygame.K_RIGHT and s.d != LEFT:
s.d=RIGHT
if event.key == pygame.K_s and s1.d != UP:
s1.d=DOWN
if event.key == pygame.K_w and s1.d != DOWN:
s1.d=UP
if event.key == pygame.K_a and s1.d != RIGHT:
s1.d=LEFT
if event.key == pygame.K_d and s1.d != LEFT:
s1.d=RIGHT
pygame.display.update()
fpsClock.tick(FPS)
製作出來的影片
請先 登入 以發表留言。