用户:
白楚不会编程查看:4 回复:2 评论:4 创建时间:2022-12-21T10:28:08
【作品展示】

【作品介绍】
白先生1.0目前有两款游戏
开发中...
要复制的话点个赞
禁止侵权!
【作品源代码】
import time
import sys
from time import sleep
from pygame.locals import *
import random
from pygame import *
import pygame
w=5
sleep(1)
print("启动White Sir games OS 1.0 中文版 系统中......" "属 python 技术构建")
sleep(1.5)
for i in range(20):
print(0+w,"%")
sleep(0.2)
w=w+5
print("压解完毕")
sleep(1)
print("已启动WhiteSir游戏系统 Chinese")
sleep(0.5)
print("贪吃蛇=1","围棋=2")
print("OS-WhiteSir >>>")
a = int(input())
if a == 1:
#!/usr/bin/env python
# 定义颜色变量
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
# 定义gameOver函数
def gameOver(playSurface):
gameOverFont = pygame.font.Font('arial.ttf', 72)
gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(10)
pygame.quit()
sys.exit()
# 定义main函数
def main():
# 初始化pygame
pygame.init()
fpsClock = pygame.time.Clock()
# 创建pygame显示层
playSurface = pygame.display.set_mode((喵0, 480))
pygame.display.set_caption('Raspberry Snake')
# 初始化变量
snakePosition = [100, 100]
snakeSegments = [[100, 100], [80, 100], [60, 100]]
raspberryPosition = [300, 300]
raspberrySpawned = 1
direction = 'right'
changeDirection = direction
while True:
# 检测例如按键等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 == 'right' and not direction == 'left':
direction = changeDirection
if changeDirection == 'left' and not direction == 'right':
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
# 增加蛇的长度
snakeSegments.insert(0, list(snakePosition))
# 判断是否吃掉了树莓
if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments.pop()
# 如果吃掉树莓,则重新生成树莓
if raspberrySpawned == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
raspberryPosition = [int(x*20), int(y*20)]
raspberrySpawned = 1
# 绘制pygame显示层
playSurface.fill(blackColour)
for position in snakeSegments:
pygame.draw.rect(playSurface, whiteColour, Rect(
position[0], position[1], 20, 20))
pygame.draw.rect(playSurface, redColour, Rect(
raspberryPosition[0], raspberryPosition[1], 20, 20))
# 刷新pygame显示层
pygame.display.flip()
# 判断是否喵亡
if snakePosition[0] > 620 or snakePosition[0] < 0:
gameOver(playSurface)
if snakePosition[1] > 460 or snakePosition[1] < 0:
for snakeBody in snakeSegments[1:]:
if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
gameOver(playSurface)
# 控制游戏速度
fpsClock.tick(5)
if __name__ == "__main__":
main()
print("已启动")
import turtle
if a==2 :
pen = turtle.Pen()
pen.speed(0)
turtle.bgcolor("#F4C79E")
turtle.setup(1050, 1050)
turtle.title("中国围棋")
pen.hideturtle()
turtle.tracer(False)
cspace = 20
grid = 50
strblack = "black"
strwhite = "white"
strempty = "empty"
currrole = strblack
opprole = strwhite
gamestatus = True
array = []
checkedarray = []
rolearray = []
opparray = []
life_count = 0
def newchess(role, x, y):
"""
落子
:param pix:
:param role:
:return:
"""
pen.penup()
pen.setposition(x, y)
pen.pendown()
pen.color(role)
pen.dot(30)
array.append({"pix": (x, y), "role": role, "flag": 1})
turtle.update()
print("new chess: (", x, y, "), role:", role, ",flag = 1")
def refreshchess():
for i in array:
pen.penup()
pen.setposition(i["pix"][0], i["pix"][1])
pen.pendown()
pen.color(i["role"])
pen.dot(30)
turtle.update()
def removechess(role, x, y):
array.remove({"pix": (x, y), "role": role, "flag": 1})
#turtle.update()
print("removed chess: (", x, y, "), role:", role, ",flag = 1")
import time
def gamestart():
global gamestatus
gamestatus = True
array.clear()
pen.reset()
pen.penup()
pen.setposition(-200, 150)
pen.width(30)
pen.color(strwhite)
pen.pendown()
pen.write("Game is starting", font=("Arial", 50, "bold"))
time.sleep(1)
pen.undo()
"""
j = 3
for i in range(4):
pen.penup()
pen.setposition(-25, 0)
pen.width(50)
pen.color("red")
pen.pendown()
pen.write(j,font = ("Arial",100,"bold"))
time.sleep(1)
pen.undo()
j = j - 1
"""
draw()
def gameover(role):
time.sleep(2)
pen.clear()
pen.penup()
pen.setposition(-250, 150)
pen.width(20)
pen.color(strwhite)
pen.pendown()
pen.write("Game is over, the Winner is ", font=("Arial", 35, "bold"))
time.sleep(1)
pen.undo()
print("Game is over, the Winner is ", role)
pen.penup()
pen.setposition(-50, 0)
pen.width(20)
pen.color("Red")
pen.pendown()
pen.write(role, font=("Arial", 50, "bold"))
time.sleep(3)
pen.undo()
gamestart()
def draw():
"""
绘制棋盘
:return:
"""
#绘制网格边框
pen.penup()
pen.setposition(-500, 500)
pen.pendown()
pen.color("#6E3F25")
pen.width(30)
pen.hideturtle()
for x in range(1, 5):
if x % 2 != 0:
pen.forward(1000)
else:
pen.forward(1000)
pen.right(90)
#绘制网格
for i in range(1, 20):
#画15条竖线
pen.right(90)
pen.penup()
pen.setposition((50*i-500), 450)
pen.width(2)
pen.pendown()
pen.forward(900)
#画15条横线
pen.left(90)
pen.penup()
pen.setposition(-450, (50*i-500))
pen.width(2)
pen.pendown()
pen.forward(900)
dot = [(300, 300), (-300, 300), (300, -300), (0, 300),
(300, 0), (0, -300), (-300, 0), (0, 0), (-300, -300)]
for i in dot:
pen.penup()
pen.setposition(i)
pen.pendown()
pen.color(strblack)
pen.dot(10)
refreshchess()
turtle.update()
#draw()
def findpix(n):
pixdata = 0
a = n % grid
num = n//grid
if a > 25:
pixdata = (num + 1)*grid
elif a >= 0:
pixdata = num * grid
if pixdata > 450:
print("Illegal Position#1")
return 450
elif pixdata < -450:
print("Illegal Position#2")
return -450
else:
return pixdata
#检查棋子队列中该坐标是否有棋子, 无棋子 = 0; 黑棋子 = 1; 白棋子 = -1;
def posstatus(x, y):
for v in array:
if v["pix"] == (x, y):
if v["flag"] == 1:
return v["role"]
else:
return strempty
return strempty
#检查该位坐标是否为空,如是,则允许落子
def confirmpos(role, x, y):
status = posstatus(x, y)
print("confirmchess")
if status == strempty:
newchess(role, x, y)
else:
print("The current position %d,%d is not available!" % x % y)
def relatedpos(x, y):
relatedpix = []
pos_flag = 0
if x + 50 <= 450:
pos_flag = posstatus((x + 50), y)
relatedpix.append(((x + 50), y, pos_flag))
if x - 50 >= -450:
pos_flag = posstatus((x - 50), y)
relatedpix.append(((x - 50), y, pos_flag))
if y + 50 <= 450:
pos_flag = posstatus(x, (y + 50))
relatedpix.append((x, (y + 50), pos_flag))
if y - 50 >= -450:
pos_flag = posstatus(x, (y - 50))
relatedpix.append((x, (y - 50), pos_flag))
return relatedpix
def checkopp(role, x, y):
print("-----checkopp----", role, x, y)
aroundpos = relatedpos(x, y)
if role == strblack:
for i in aroundpos:
print("checkopp#aroundpos: ", i)
if i[2] == strwhite: # 需要检查异色棋子是否存活
print("checkopp#白色棋子位置:", i[0], i[1])
if (i[0], i[1]) not in opparray:
opparray.append((i[0], i[1]))
elif role == strwhite:
for i in aroundpos:
if i[2] == strblack: # 需要检查异色棋子是否存活
print("checkopp#黑色棋子位置:", i[0], i[1])
if (i[0], i[1]) not in opparray:
opparray.append((i[0], i[1]))
def check(role, x, y):
global life_count, level
aroundpos = relatedpos(x, y)
print("-----check----", role, x, y)
if (x, y) not in rolearray:
rolearray.append((x, y))
if role == strblack:
for i in aroundpos:
print("aroundpos: ", i)
if (i[0], i[1]) not in checkedarray:
#存入已检查的位置
checkedarray.append((i[0], i[1]))
if i[2] == strempty: # 是否有活气
life_count += 1
print("1#life_count = ", life_count,
"Pix is ", i[0], i[1], "Role =", role)
elif i[2] == strblack and role == strblack: # 是否有同色棋子
print("黑色棋子位置:", i[0], i[1])
if (i[0], i[1]) not in rolearray:
rolearray.append((i[0], i[1]))
check(role, i[0], i[1])
elif role == strwhite:
for i in aroundpos:
print("aroundpos: ", i)
if (i[0], i[1]) not in checkedarray:
#存入已检查的位置
checkedarray.append((i[0], i[1]))
if i[2] == strempty: # 是否有活气
life_count += 1
print("1#life_count = ", life_count,
"Pix is ", i[0], i[1], "Role =", role)
elif i[2] == strwhite and role == strwhite: # 是否有同色棋子
print("白色棋子位置:", i[0], i[1])
if (i[0], i[1]) not in rolearray:
rolearray.append((i[0], i[1]))
check(role, i[0], i[1])
def remove(role):
for i in rolearray:
removechess(role, i[0], i[1])
def judgement(role, x, y):
pass
def click(x, y):
"""
鼠标点击事件
:param x:
:param y:
:return:
"""
global currrole, gamestatus, opprole
global life_count, checkedarray, rolearray, opparray
#try :
if gamestatus:
px = findpix(x)
py = findpix(y)
confirmpos(currrole, px, py)
print("==========check opp chess======")
opparray.clear()
checkopp(currrole, px, py)
print("------opparray: ", opparray)
#检查对手棋子,是否被杀喵
for i in opparray:
life_count = 0
checkedarray.clear()
rolearray.clear()
check(opprole, i[0], i[1])
print("2#checkedarray:", checkedarray)
print("2#rolearray:", rolearray)
print("2#:life_count", life_count)
if life_count == 0:
remove(opprole)
life_count = 0
checkedarray.clear()
rolearray.clear()
print("==========check role chess======")
check(currrole, px, py)
print("1#checkedarray:", checkedarray)
print("1#rolearray:", rolearray)
#print("1#opparray:",opparray)
print("1#:life_count", life_count)
if life_count == 0:
remove(currrole)
"""
#检查对手棋子,是否被杀喵
for i in opparray:
life_count = 0
checkedarray.clear()
rolearray.clear()
check(opprole, i[0], i[1])
print("2#checkedarray:", checkedarray)
print("2#rolearray:", rolearray)
print("2#:life_count", life_count)
if life_count == 0:
remove(opprole)
"""
time.sleep(1)
if judgement(currrole, px, py):
gamestatus = False
print("Game Over,%s is winner" % currrole)
gameover(currrole)
else:
if currrole == strblack:
currrole = strwhite
opprole = strblack
else:
currrole = strblack
opprole = strwhite
else:
print("game is over")
pen.reset()
draw()
gamestart()
turtle.onscreenclick(click)
turtle.setup(10, 10, 10, 10)
turtle.done()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn