Python pygame implements the game color backgammon with detailed notes and source code stand-alone version

Project Description

When I was learning python, I wrote a game to practice using pygame. It had no other dependencies and
only used one or two hundred lines of code to implement it. Overall, the function was not complete.

The story behind the project

This project happened when I was in college.
By chance, I met a kid named Python. The kid was about 10 years old and was preparing to enter the first grade of junior high school. The
kid was very, very, very smart! ! !
At that time, when I told him something, he could quickly accept it and imitate it immediately.
There are many things that children can do, and one of them I am very interested in. Haha - Go seems to be a very high level amateur (sorry, I don’t know much about the ranks). It
seems to be What kind of thing can you do professionally after reaching a certain level? It was a very, very powerful existence for me.
At that time, I also asked him to briefly teach me how to play Go and some concepts of Go.

In addition to backgammon, I also wrote games such as Snake and Minesweeper.
I also told him about things related to crawlers, HTML, CSS and the like.
At that time, there was a game called "Human Resource Machine (Human Resource)". The game was a A game that uses simple programming to control a villain to achieve goals
Insert image description here
↑ is this game! I was surprised at how quickly he passed the test! It made me stressed out haha

At that time, I also prepared a few pages of "textbook" so that the children can refer back to it later.
Please add image description

Project expansion ideas

Of course, Go is actually the same principle, but the logic of calculating victory and ki will be different. It
can be improved to use the mouse to make moves, which will be more interesting.
You can refer to the main project on GitHub. In addition to the stand-alone version, there is also a local area network version.

Run screenshot

Insert image description here

Insert image description here

Install dependencies

pip install pygame
或者
pip3 install pygame

run game

Save the game code and run it directly
Move the cursor up, down, left, and right.

import pygame

# 初始化
pygame.init()
# 设置窗口标题
screencaption=pygame.display.set_caption('Gobang')
# 设置大小
screen=pygame.display.set_mode([350,285])

# 初始化字体
myfont=pygame.font.Font(None,30)
textImage=myfont.render("Hello Pygame",True,[255,255,255])
screen.blit(textImage,(100,100))

# 棋子状态0为空 1为白色 2为黑色
status_list = {
    
    }
for i in range(0, 15*18):
    status_list[i] = 0  
#print(status_list)

clock = pygame.time.Clock()

# 0 是白棋走 1是黑棋走
flag = 0
# 将要绘制的棋子的位置
movex = 1
movey = 1
while True:
    clock.tick(30)
    
    # 绘制棋盘
    screen.fill([255,255,255])
    for i in range(0, 15):
        pygame.draw.line(screen,[0,0,0],[0,i*20],[280,i*20],2)
    for i in range(0, 15):
        pygame.draw.line(screen,[0,0,0],[i*20,0],[i*20,280],2)
    
    # 绘制棋子
    for x in range(0, 15):
        for y in range(0, 15):
            if status_list[x*15 + y] == 1:
                pygame.draw.circle(screen,[255,0,0],[ 2 + y * 20,2 + x*20],10)
            elif status_list[x*15 + y] == 2:
                pygame.draw.circle(screen,[0,0,0],[ 2 + y * 20, 2 + x*20],10)
            # 判断是否获胜
            # X轴的判定
            if y < 11:
                # 白棋获胜
                if status_list[x*15 + y] == 1 and status_list[x*15 + y + 1] == 1 and status_list[x*15 + y + 2] == 1 and status_list[x*15 + y + 3] == 1 and status_list[x*15 + y + 4] == 1:
                    print("白棋胜利")
                    # break
                    
                # 黑棋获胜
                if status_list[x*15 + y] == 2 and status_list[x*15 + y + 1] == 2 and status_list[x*15 + y + 2] == 2 and status_list[x*15 + y + 3] == 2 and status_list[x*15 + y + 4] == 2:
                    print("黑棋胜利")
                    # break

            # 判断是否获胜
            # Y轴的判定
            if x < 11:
                if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + y] == 1 and status_list[(x+2)*15 + y] == 1 and status_list[(x+3)*15 + y] == 1 and status_list[(x+4)*15 + y] == 1:
                    print("白棋胜利")
                    # break
                    
                if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + y] == 2 and status_list[(x+2)*15 + y] == 2 and status_list[(x+3)*15 + y] == 2 and status_list[(x+4)*15 + y] == 2:
                    print("黑棋胜利")
                    # break

            # 判断是否获胜
            # 斜着判断 正对角线
            if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y+1)] == 1 and status_list[(x+2)*15 + (y+2)] == 1 and status_list[(x+3)*15 + (y+3)] == 1 and status_list[(x+4)*15 + (y+4)] == 1:
                print("白棋胜利")
                # break
            if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y+1)] == 2 and status_list[(x+2)*15 + (y+2)] == 2 and status_list[(x+3)*15 + (y+3)] == 2 and status_list[(x+4)*15 + (y+4)] == 2:
                print("黑棋胜利")
                # break
            # 判断是否获胜
            # 斜着判断 反对角线
            if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y-1)] == 1 and status_list[(x+2)*15 + (y-2)] == 1 and status_list[(x+3)*15 + (y-3)] == 1 and status_list[(x+4)*15 + (y-4)] == 1:
                print("白棋胜利")
                # break
            if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y-1)] == 2 and status_list[(x+2)*15 + (y-2)] == 2 and status_list[(x+3)*15 + (y-3)] == 2 and status_list[(x+4)*15 + (y-4)] == 2:
                print("黑棋胜利")
                # break
    # 绘制落棋位置
    pygame.draw.circle(screen,[0,0,0],[ 2 + movex*20, 2 + movey*20],10,3)
    
    # 绘制文字 显示到谁落棋子
    if flag == 0: 
        textImage=myfont.render("White",True,[255,0,0])
    else:
        textImage=myfont.render("Black",True,[0,0,255])
    screen.blit(textImage,(290,10))
	
    # 判断事件
    for event in pygame.event.get():
        # 退出事件
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
        # 键盘事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if movex > 0:
                    movex = movex - 1
            if event.key == pygame.K_RIGHT:
                if movex < 14:
                    movex = movex + 1
            if event.key == pygame.K_UP:
                if movey > 0:
                    movey = movey - 1
            if event.key == pygame.K_DOWN:
                if movey < 14:
                    movey = movey + 1
            if event.key == pygame.K_SPACE:
                if flag == 0:
                    if status_list[movey * 15 + movex] == 0:
                        status_list[movey * 15 + movex] = 1
                        flag = 1
                elif flag == 1:
                    if status_list[movey * 15 + movex] == 0:
                        status_list[movey * 15 + movex] = 2
                        flag = 0

    # 刷新页面
    pygame.display.flip()
print("Done!")

Guess you like

Origin blog.csdn.net/w776341482/article/details/128930013