猫史档案馆


【Python作品分享】阿贝尔沙群2.0

用户:秤杆不平衡秤杆不平衡查看:1 回复:3 评论:1 创建时间:2023-03-19T13:15:41


【作品展示】

center_image

 

【作品介绍】

阿贝尔沙群2.0版!

新增大量参数与功能,方便观察

大家可以调整flag的值

(可以取1,2,3,4,40,41,42,43,44,45,46,47,48,50,51,52,53,54,55,56,57,58,60,61,62,63,喵,65,66,67,68等值,不建议使用60,61,62,63参数,40与1等价但开始速度稍慢。)

来观看各种精美图形。

默认大小为101*101,可以更改参数(自己琢磨吧)调整大小。

开头注释写的有点奇怪,只能懂得都懂了。后续会补上中文注释。

 

【作品源代码】

import pygame
import sys
import time
import random


flag = 53
#one is centre and two is random.
#three is centre in random.
#four is random(chess),not centre or random(push)
#41,42,43,44... is centre of 1,2,3,4...
#51,52,53,54... is random of 1,2,3,4...
#喵,65,66,67... is not of 4,5,6,7...
#(40+x or 50+x or 60+x or y)(0<=x<=8)(1<=y<=3)(int(x)==x)(int(y)==y)
time.sleep(2)
scr_len = 909
rectl = 9
chess_len = int(scr_len/rectl)
print_chesss = []
prepare_chess = []
sum_chess_n = 0
numt = [0, 0, 0, 0, 0]
f = open("num_for_print.txt","w")
one = open("num_for_print_one.txt", "w")
two = open("num_for_print_two.txt", "w")
three = open("num_for_print_three.txt", "w")
four = open("num_for_print_four.txt", "w")
zero = open("num_for_print_zero.txt", "w")
sum_c = open("num_for_print_sum_chess.txt", "w")
for i in range(chess_len**2):
    if flag == 1:
        print_chesss.append(0)
        prepare_chess.append(0)
    elif flag // 10 == 4 or flag // 10 == 5 or flag // 10 == 6:
        print_chesss.append(flag % 10)
        prepare_chess.append(flag % 10)
    elif flag == 2 or flag == 3:
        w = random.randint(1, 3)
        print_chesss.append(w)
        prepare_chess.append(w)
    elif flag == 4:
        w = random.randint(1, 4)
        print_chesss.append(w)
        prepare_chess.append(w)


pygame.init()
screen = pygame.display.set_mode((scr_len, scr_len))
colour_t = ((0, 0, 0), (255, 0, 0), (255, 255, 0),
            (0, 255, 0), (0, 0, 255), (255, 0, 255),
            (255, 155, 192), (192, 192, 192), (255, 255, 255))
#  0    1    2      3     4     5    6    7     8
#black red yellow grenn blue purple pink grey white
pygame.display.set_caption("阿贝尔沙群")
fps = 2
cnt = 0
cnt1 = 1
fps1 = 2


def get_c(x, y, f):
    if f == 0:
        return print_chesss[int(y*chess_len+x)]
    else:
        return prepare_chess[int(y*chess_len+x)]


def append_c(x, y, f, n):#其实append应该改为insert的,但是懒得改了。
    if f == 0:
        print_chesss[int(y*chess_len+x)] = n
    else:
        prepare_chess[int(y*chess_len+x)] = n


def work():
    for x in range(chess_len):
        for y in range(chess_len):
            num = get_c(x, y, 0)
            if num >= 4:
                num -= 4
                append_c(x, y, 1, num)
            if x+1 <= chess_len-1:
                if get_c(x+1, y, 0) >= 4:
                    num += 1
                    append_c(x, y, 1, num)
            if y+1 <= chess_len-1:
                if get_c(x, y+1, 0) >= 4:
                    num += 1
                    append_c(x, y, 1, num)
            if x-1 >= 0:
                if get_c(x-1, y, 0) >= 4:
                    num += 1
                    append_c(x, y, 1, num)
            if y-1 >= 0:
                if get_c(x, y-1, 0) >= 4:
                    num += 1
                    append_c(x, y, 1, num)


def for_print():
    global numt
    numt = [0, 0, 0, 0, 0]
    for x in range(chess_len):
        for y in range(chess_len):
            b = get_c(x, y, 1)
            append_c(x, y, 0, b)
            if b <= 5:
                numt[b-1] += 1


def print_chess():
    global screen
    for x in range(chess_len):
        for y in range(chess_len):
            point = get_c(x, y, 0)
            if sum(colour_t[point]) != 0:
                pygame.draw.rect(
                    screen, colour_t[point], (x*rectl, y*rectl, rectl, rectl))

def sum_chess():
    global sum_chess_n
    sum_chess_n = 0
    for x in range(chess_len):
        for y in range(chess_len):
            sum_chess_n += get_c(x, y, 0)


def draw_fps():
    global screen,f,one,two,three,four,zero
    pygame.draw.line(screen, (255, 0, 0), (scr_len+10, scr_len), (scr_len+10, scr_len-fps*20),20)
    sys.stdout.write("\r")
    sys.stdout.write("fps:"+str(int(fps*100)/100)+"/s"+"----" +
                     "1 , 2, 3, 4, 0:"+str(int(numt[0]/chess_len**2*10000)/100)+"% "
                     + str(int(numt[1]/chess_len**2*10000)/100)+"% "
                     + str(int(numt[2]/chess_len**2*10000)/100)+"% "
                     + str(int(numt[3]/chess_len**2*10000)/100)+"% "
                     + str(int(numt[4]/chess_len**2*10000)/100)+"% "
                     + "sum_chess:"+str(sum_chess_n)+(" "*10))
    #(" "*10) 的作用是做输出缓冲区,不然最后的sum_chess数据会很奇怪。
    f.write("1, 2, 3, 4, 0:"+str(int(numt[0]/chess_len**2*10000)/100)+" % "
            + str(int(numt[1]/chess_len**2*10000)/100)+"% "
            + str(int(numt[2]/chess_len**2*10000)/100)+"% "
            + str(int(numt[3]/chess_len**2*10000)/100)+"% "
            + str(int(numt[4]/chess_len**2*10000)/100)+"%\n"
            + "sum_chess:"+str(sum_chess_n)+"\n")
    one.write(str(int(numt[0]/chess_len**2*10000)/100) + "\n")
    two.write(str(int(numt[1]/chess_len**2*10000)/100)+"\n")
    three.write(str(int(numt[2]/chess_len**2*10000)/100)+"\n")
    four.write(str(int(numt[3]/chess_len**2*10000)/100)+"\n")
    zero.write(str(int(numt[4]/chess_len**2*10000)/100)+"\n")
    sum_c.write(str(sum_chess_n)+"\n")



t1 = time.time()
while True:
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    if flag == 2 or flag//10==5:
        x = random.randint(0, chess_len-1)
        y = random.randint(0, chess_len-1)
        a = get_c(x, y, 1)+1
        append_c(x, y, 1, a)
    elif flag == 1 or flag ==3 or flag//10==4:
        a = get_c(int(chess_len/2)+1, int(chess_len/2)+1, 1)+1
        append_c(int(chess_len/2)+1, int(chess_len/2)+1, 1, a)
    work()
    for_print()
    sum_chess()
    draw_fps()
    if cnt1 == fps1:
        print_chess()
        cnt1 = 1
        pygame.display.update()
    cnt += 1
    cnt1 += 1
    t2 = time.time()
    if t2-t1 >= 1/4.2:
        t1 = time.time()
        fps = cnt*4.2
        cnt = 0

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
秤杆不平衡秤杆不平衡

开头各中文注释:

1代表棋盘默认为空,在中心落沙。

2代表棋盘默认随机放沙(1到3),随机落沙。

3代表棋盘默认随机放沙(1到3),在中心落沙。

4代表棋盘默认随机放沙(1到4),不落沙。

40+x代表在中心落沙,x取值范围是0到8之间(包括0和8)的整数,代表棋盘每一个格子的沙子数。

50+x代表随机落沙,x同上。

60+x代表不落沙,x同上。

点赞1


评论


秤杆不平衡秤杆不平衡

0,1,2,3,4,5,6,7,8个沙子颜色分别是黑,红,黄,绿,蓝,紫,粉,灰,白。

点赞1


评论


1111111111111111211111111111111112

欢迎加入codework工作室

点赞0


评论