Initial commit
34
README.md
|
@ -1,3 +1,33 @@
|
|||
# Submarine-ladders-Python
|
||||
# python制作海底飞行棋(含源码)
|
||||
|
||||
python制作海底飞行棋(含源码)
|
||||
飞行棋玩过吗?玩过python制作的海底飞行棋玩过吗?额。。。。。。
|
||||
|
||||
今天就来教制作海底飞行棋
|
||||
|
||||
# 核心玩法
|
||||
|
||||
两名玩家通过→和←操控游戏角色,最终全部到达终点,(本游戏适合全年龄段,不要太较真)谁的分数越高谁就获胜
|
||||
|
||||
# 主要代码思想
|
||||
|
||||
实现游戏角色移动,分数,分数判断
|
||||
|
||||
在游戏中,设立三个游戏状态
|
||||
|
||||
- start 开始
|
||||
- running 运行
|
||||
- game——over 游戏结束
|
||||
|
||||
三个状态都很重要,对于制作游戏有很大帮助
|
||||
|
||||
要依次根据玩家操作变换
|
||||
|
||||
其次就是鼠标和键盘的按下事件
|
||||
|
||||
没有这一步,游戏就变成了动画片,不能控制,就看着它运行
|
||||
|
||||
而且这一步很容易报错,因为代码量多
|
||||
|
||||
最后就是完成所有的调用
|
||||
|
||||
一切完成后就OK了
|
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 960 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 90 KiB |
After Width: | Height: | Size: 89 KiB |
After Width: | Height: | Size: 89 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 1.6 MiB |
After Width: | Height: | Size: 1.6 MiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 1.2 MiB |
After Width: | Height: | Size: 8.1 KiB |
|
@ -0,0 +1,108 @@
|
|||
# coding:utf-8
|
||||
import pygame,sys,os,easygui,random,time
|
||||
from pygame.locals import *
|
||||
from sys import exit
|
||||
from func import fillText,drawPlayer,addScore_s,addScore_p,drawStep
|
||||
pygame.init()
|
||||
# 窗口定位
|
||||
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 30)
|
||||
# 设置一个长宽窗口
|
||||
canvas = pygame.display.set_mode((1025, 689))
|
||||
canvas.fill([255, 255, 255])
|
||||
# 设置窗口标题
|
||||
pygame.display.set_caption("飞行棋")
|
||||
|
||||
# 图片加载
|
||||
start = pygame.image.load('images/start.png')
|
||||
bg = pygame.image.load('images/bg.png')
|
||||
end1 = pygame.image.load('images/end1.png')
|
||||
end2 = pygame.image.load('images/end2.png')
|
||||
gameover = pygame.image.load('images/gameover.png')
|
||||
spongebob = 'spongebob'
|
||||
patrickstar = 'patrickstar'
|
||||
|
||||
|
||||
#设定游戏状态和玩家分数
|
||||
state = 'START'
|
||||
score_s = 0
|
||||
score_p = 0
|
||||
#判断哪位玩家移动
|
||||
turn = True
|
||||
#控制玩家到达位置
|
||||
sum_s = 0
|
||||
sum_p = 0
|
||||
#控制玩家移动步数
|
||||
step_s = 0
|
||||
step_p = 0
|
||||
#表示到达终点
|
||||
final = 0
|
||||
#创建游戏控制
|
||||
def gameControl():
|
||||
global step_s,step_p,score_s,score_p,final,sum_s,sum_p,turn
|
||||
#随机生成行进步数
|
||||
step_s = random.randint(1,4)
|
||||
step_p = random.randint(1,4)
|
||||
#游戏开始状态
|
||||
if state == 'START':
|
||||
canvas.blit(start,(0,0))
|
||||
elif state == 'READY':
|
||||
canvas.blit(bg,(0,0))
|
||||
drawPlayer(spongebob,sum_s)
|
||||
drawPlayer(patrickstar,sum_p)
|
||||
drawStep(1,(-10,300))
|
||||
fillText(score_s ,(30,150))
|
||||
fillText(score_p ,(860,150))
|
||||
elif state == 'RUNNING':
|
||||
canvas.blit(bg,(0,0))
|
||||
if turn == True:#表示海绵宝宝移动
|
||||
sum_s = sum_s + step_s
|
||||
if sum_s < 14:
|
||||
drawStep(step_s,(-10,300))
|
||||
score_s = addScore_s(sum_s)
|
||||
else:
|
||||
canvas.blit(gameover,(40,340))
|
||||
final = 1
|
||||
elif turn == False:#表示派大星移动
|
||||
sum_p = sum_p + step_p
|
||||
if sum_p < 14:
|
||||
drawStep(step_p,(-10,300))
|
||||
score_p = addScore_p(sum_p)
|
||||
else:
|
||||
canvas.blit(gameover,(40,340))
|
||||
final = 1
|
||||
drawPlayer(spongebob,sum_s)
|
||||
drawPlayer(patrickstar,sum_p)
|
||||
fillText(score_s,(30,150))
|
||||
fillText(score_p,(860,150))
|
||||
#游戏结束状态
|
||||
elif state == 'END':
|
||||
if score_s >= score_p :
|
||||
canvas.blit(end2,(0,0))
|
||||
else:
|
||||
canvas.blit(end1,(0,0))
|
||||
|
||||
gameControl()
|
||||
# 根据用户的操作切换游戏状态
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
|
||||
if state == 'START':
|
||||
state = 'READY'
|
||||
gameControl()
|
||||
if state == 'READY':
|
||||
state = 'RUNNING'
|
||||
elif event.type == KEYDOWN and event.key == K_LEFT and turn == True:
|
||||
gameControl()
|
||||
turn = False
|
||||
elif event.type == KEYDOWN and event.key == K_RIGHT and turn == False:
|
||||
gameControl()
|
||||
turn = True
|
||||
elif final == 1:
|
||||
pygame.time.delay(1500)
|
||||
pygame.display.update()
|
||||
state = 'END'
|
||||
gameControl()
|
||||
pygame.display.update()
|