【Dison夏令营 Day 13】使用 Python 创建扫雷游戏

news2024/9/27 12:17:24

在本文中,我们将介绍如何使用 Python 语言创建自己的基于终端的扫雷程序。

关于游戏
1992年4月6日,扫雷和纸牌、空当接龙等小游戏搭载在Windows 3.1系统中与用户见面,主要目的是让用户训练使用鼠标。扫雷是一款单人游戏,这个游戏的玩法很简单,有初级、中级、高级和自定义等模式,雷区中随机布置一定数量的地雷,玩家需要清除一个包含地雷和数字的正方形网格。玩家需要借助相邻方格中的数字来防止自己落在地雷上,但不许踩到地雷。

在这里插入图片描述

使用 Python 设计扫雷游戏

在创建游戏逻辑之前,我们需要设计游戏的基本布局。使用 Python 创建正方形网格非常容易:

# Printing the Minesweeper Layout
def print_mines_layout():
    global mine_values
    global n
 
    print()
    print("\t\t\tMINESWEEPER\n")
 
    st = "   "
    for i in range(n):
        st = st + "     " + str(i + 1)
    print(st)   
 
    for r in range(n):
        st = "     "
        if r == 0:
            for col in range(n):
                st = st + "______" 
            print(st)
 
        st = "     "
        for col in range(n):
            st = st + "|     "
        print(st + "|")
         
        st = "  " + str(r + 1) + "  "
        for col in range(n):
            st = st + "|  " + str(mine_values[r][col]) + "  "
        print(st + "|") 
 
        st = "     "
        for col in range(n):
            st = st + "|_____"
        print(st + '|')
 
    print()

每次迭代显示的网格如下图所示:

在这里插入图片描述
M "符号表示该单元格中存在 “地雷”。我们可以清楚地看到,网格上的任何数字都表示相邻 "8 "单元格中存在的地雷数量。

本教程将进一步解释如何使用 mine_values 等变量。

输入系统

任何游戏最重要的部分之一就是输入法。在我们的扫雷版本中,我们将使用行数和列数作为输入技术。

在开始游戏之前,脚本必须为玩家提供一组指令。我们的游戏会打印以下内容。

在这里插入图片描述
与网格一起显示的行数和列数对我们的输入系统很有帮助。我们知道,在没有任何指示器的情况下追踪地雷是很困难的。因此,扫雷游戏提供了一种使用 "标志 "来标记我们已知含有地雷的单元格的方法。

数据存储

对于一局扫雷游戏,我们需要记录以下信息:

  • 网格大小
  • 地雷数量
  • 实际 "网格值——在游戏开始时,我们需要一个容器来存储玩家未知的游戏实际值。例如,地雷的位置。
  • 表面 "网格值 - 每次移动后,我们都需要更新所有必须显示给玩家的值。
  • 标记位置 - 已标记的单元格。

这些值通过以下数据结构存储

if __name__ == "__main__":
 
    # Size of grid
    n = 8
    # Number of mines
    mines_no = 8
 
    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)] 
    # The apparent values of the grid
    mine_values = [[' ' for y in range(n)] for x in range(n)]
    # The positions that have been flagged
    flags = []

扫雷的游戏逻辑并不复杂。所有的努力都是为了设置扫雷布局。

设置地雷

我们需要随机设置地雷的位置,这样玩家就无法预测它们的位置。这可以通过以下方法实现

# Function for setting up Mines
def set_mines():
 
    global numbers
    global mines_no
    global n
 
    # Track of number of mines already set up
    count = 0
    while count < mines_no:
 
        # Random number from all possible grid positions 
        val = random.randint(0, n*n-1)
 
        # Generating row and column from the number
        r = val // n
        col = val % n
 
        # Place the mine, if it doesn't already have one
        if numbers[r][col] != -1:
            count = count + 1
            numbers[r][col] = -1

在代码中,我们从网格中所有可能的单元格中随机选择一个数字。我们一直这样做,直到得到所述的地雷数量。

注意:地雷的实际值存储为-1,而为显示而存储的值则表示地雷为 “M”。

注意:"randint "函数只能在导入随机库后使用。在程序开始时写入 "import random "即可。

设置网格编号

对于网格中的每个单元格,我们必须检查所有相邻单元格是否存在地雷。具体方法如下

# Function for setting up the other grid values
def set_values():
 
    global numbers
    global n
 
    # Loop for counting each cell value
    for r in range(n):
        for col in range(n):
 
            # Skip, if it contains a mine
            if numbers[r][col] == -1:
                continue
 
            # Check up  
            if r > 0 and numbers[r-1][col] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check down    
            if r < n-1  and numbers[r+1][col] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check left
            if col > 0 and numbers[r][col-1] == -1:
                numbers[r][c] = numbers[r][c] + 1
            # Check right
            if col < n-1 and numbers[r][col+1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check top-left    
            if r > 0 and col > 0 and numbers[r-1][col-1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check top-right
            if r > 0 and col < n-1 and numbers[r-1][col+1]== -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check below-left  
            if r < n-1 and col > 0 and numbers[r+1][col-1]== -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check below-right
            if r < n-1 and col< n-1 and numbers[r+1][col+1]==-1:
                numbers[r][col] = numbers[r][col] + 1

这些值是不对玩家公开的,因此被存储在数字变量中。

游戏循环

游戏循环是游戏中非常关键的一部分。它需要更新玩家的每一步棋以及游戏的结局。

# Set the mines
set_mines()
 
# Set the values
set_values()
 
# Display the instructions
instructions()
 
# Variable for maintaining Game Loop
over = False
         
# The GAME LOOP 
while not over:
    print_mines_layout()

在循环的每次迭代中,都必须显示扫雷网格并处理玩家的移动。

处理玩家输入

正如我们之前提到的,有两种玩家输入:

# Input from the user
inp = input("Enter row number followed by space and column number = ").split()

标准输入

在普通移动中,会提到行和列的编号。玩家此举的动机是解锁一个没有地雷的单元格。

# Standard Move
if len(inp) == 2:
 
    # Try block to handle errant input
    try: 
        val = list(map(int, inp))
    except ValueError:
        clear()
        print("Wrong input!")
        instructions()
        continue

旗子输入

在插旗动作中,游戏者会输入三个数值。前两个值表示小区位置,最后一个值表示插旗。

# Flag Input
elif len(inp) == 3:
    if inp[2] != 'F' and inp[2] != 'f':
        clear()
        print("Wrong Input!")
        instructions()
        continue
 
    # Try block to handle errant input  
    try:
        val = list(map(int, inp[:2]))
    except ValueError:
        clear()
        print("Wrong input!")
        instructions()
        continue

净化输入

在存储输入后,我们必须进行一些合理性检查,以便游戏顺利运行。

# Sanity checks
if val[0] > n or val[0] < 1 or val[1] > n or val[1] < 1:
    clear()
    print("Wrong Input!")
    instructions()
    continue
 
# Get row and column numbers
r = val[0]-1
col = val[1]-1

输入过程完成后,行号和列号将被提取并存储在 "r "和 "c "中。

处理标志输入

管理标记输入并不是一个大问题。在标记单元格为地雷之前,需要检查一些先决条件。

必须进行以下检查:

  • 单元格是否已被标记。
  • 要标记的单元是否已经显示给玩家。
  • 标记数量不超过地雷数量。

处理完这些问题后,该单元就会被标记为地雷。

# If cell already been flagged
if [r, col] in flags:
    clear()
    print("Flag already set")
    continue
 
# If cell already been displayed
if mine_values[r][col] != ' ':
    clear()
    print("Value already known")
    continue
 
# Check the number for flags    
if len(flags) < mines_no:
    clear()
    print("Flag set")
 
    # Adding flag to the list
    flags.append([r, col])
     
    # Set the flag for display
    mine_values[r][col] = 'F'
    continue
else:
    clear()
    print("Flags finished")
    continue    

处理标准输入

标准输入涉及游戏的整体运行。有三种不同的情况:

锚定地雷

一旦玩家选择了有地雷的单元格,游戏就结束了。这可能是运气不好或判断失误造成的。

# If landing on a mine --- GAME OVER    
if numbers[r][col] == -1:
    mine_values[r][col] = 'M'
    show_mines()
    print_mines_layout()
    print("Landed on a mine. GAME OVER!!!!!")
    over = True
    continue

当我们降落到有地雷的单元格后,我们需要显示游戏中的所有地雷,并改变游戏循环后面的变量。

函数 "show_mines() "负责执行此操作。

def show_mines():
    global mine_values
    global numbers
    global n
 
    for r in range(n):
        for col in range(n):
            if numbers[r][col] == -1:
                mine_values[r][col] = 'M'

访问 "0 "值单元格。
创建游戏最棘手的部分就是管理这种情况。每当游戏者访问一个 "0 "值单元格时,所有相邻的元素都必须显示出来,直到访问到一个非零值单元格为止。

# If landing on a cell with 0 mines in neighboring cells
elif numbers[r][n] == 0:
    vis = []
    mine_values[r][n] = '0'
    neighbours(r, col)  

这一目标可以通过递归来实现。递归是一种编程工具,其中的函数会调用自身,直到基本情况得到满足。相邻函数就是一个递归函数,它解决了我们的问题。

def neighbours(r, col):
     
    global mine_values
    global numbers
    global vis
 
    # If the cell already not visited
    if [r,col] not in vis:
 
        # Mark the cell visited
        vis.append([r,col])
 
        # If the cell is zero-valued
        if numbers[r][col] == 0:
 
            # Display it to the user
            mine_values[r][col] = numbers[r][col]
 
            # Recursive calls for the neighbouring cells
            if r > 0:
                neighbours(r-1, col)
            if r < n-1:
                neighbours(r+1, col)
            if col > 0:
                neighbours(r, col-1)
            if col < n-1:
                neighbours(r, col+1)    
            if r > 0 and col > 0:
                neighbours(r-1, col-1)
            if r > 0 and col < n-1:
                neighbours(r-1, col+1)
            if r < n-1 and col > 0:
                neighbours(r+1, col-1)
            if r < n-1 and col < n-1:
                neighbours(r+1, col+1)  
                 
        # If the cell is not zero-valued            
        if numbers[r][col] != 0:
                mine_values[r][col] = numbers[r][col]

针对游戏的这一特殊概念,我们使用了一种新的数据结构,即 vis。vis 的作用是在递归过程中跟踪已访问过的单元格。如果没有这些信息,递归将永远持续下去。

在显示所有零值单元格及其相邻单元格后,我们就可以进入最后一个场景了。

选择非零值单元格

处理这种情况无需费力,因为我们只需更改显示值即可。

# If selecting a cell with atleast 1 mine in neighboring cells  
else:   
    mine_values[r][col] = numbers[r][col]

结束游戏

每次下棋时,都需要检查棋局是否结束。具体做法如下

# Check for game completion 
if(check_over()):
    show_mines()
    print_mines_layout()
    print("Congratulations!!! YOU WIN")
    over = True
    continue

函数 check_over()负责检查游戏是否结束。

# Function to check for completion of the game
def check_over():
    global mine_values
    global n
    global mines_no
 
    # Count of all numbered values
    count = 0
 
    # Loop for checking each cell in the grid
    for r in range(n):
        for col in range(n):
 
            # If cell not empty or flagged
            if mine_values[r][col] != ' ' and mine_values[r][col] != 'F':
                count = count + 1
     
    # Count comparison          
    if count == n * n - mines_no:
        return True
    else:
        return False

我们计算没有空格或标记的单元格数量。当这一数字等于除含有地雷的单元格外的所有单元格时,游戏即宣告结束。

每次移动后清除输出

当我们不断在终端上打印内容时,终端就会变得很拥挤。因此,必须不断清除输出。方法如下

# Function for clearing the terminal
def clear():
    os.system("clear")

完整代码

以下是扫雷游戏的完整代码:

# Importing packages
import random
import os
 
# Printing the Minesweeper Layout
def print_mines_layout():
 
    global mine_values
    global n
 
    print()
    print("\t\t\tMINESWEEPER\n")
 
    st = "   "
    for i in range(n):
        st = st + "     " + str(i + 1)
    print(st)   
 
    for r in range(n):
        st = "     "
        if r == 0:
            for col in range(n):
                st = st + "______" 
            print(st)
 
        st = "     "
        for col in range(n):
            st = st + "|     "
        print(st + "|")
         
        st = "  " + str(r + 1) + "  "
        for col in range(n):
            st = st + "|  " + str(mine_values[r][col]) + "  "
        print(st + "|") 
 
        st = "     "
        for col in range(n):
            st = st + "|_____"
        print(st + '|')
 
    print()
  
# Function for setting up Mines
def set_mines():
 
    global numbers
    global mines_no
    global n
 
    # Track of number of mines already set up
    count = 0
    while count < mines_no:
 
        # Random number from all possible grid positions 
        val = random.randint(0, n*n-1)
 
        # Generating row and column from the number
        r = val // n
        col = val % n
 
        # Place the mine, if it doesn't already have one
        if numbers[r][col] != -1:
            count = count + 1
            numbers[r][col] = -1
 
# Function for setting up the other grid values
def set_values():
 
    global numbers
    global n
 
    # Loop for counting each cell value
    for r in range(n):
        for col in range(n):
 
            # Skip, if it contains a mine
            if numbers[r][col] == -1:
                continue
 
            # Check up  
            if r > 0 and numbers[r-1][col] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check down    
            if r < n-1  and numbers[r+1][col] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check left
            if col > 0 and numbers[r][col-1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check right
            if col < n-1 and numbers[r][col+1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check top-left    
            if r > 0 and col > 0 and numbers[r-1][col-1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check top-right
            if r > 0 and col < n-1 and numbers[r-1][col+1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check below-left  
            if r < n-1 and col > 0 and numbers[r+1][col-1] == -1:
                numbers[r][col] = numbers[r][col] + 1
            # Check below-right
            if r < n-1 and col < n-1 and numbers[r+1][col+1] == -1:
                numbers[r][col] = numbers[r][col] + 1
 
# Recursive function to display all zero-valued neighbours  
def neighbours(r, col):
     
    global mine_values
    global numbers
    global vis
 
    # If the cell already not visited
    if [r,col] not in vis:
 
        # Mark the cell visited
        vis.append([r,col])
 
        # If the cell is zero-valued
        if numbers[r][col] == 0:
 
            # Display it to the user
            mine_values[r][col] = numbers[r][col]
 
            # Recursive calls for the neighbouring cells
            if r > 0:
                neighbours(r-1, col)
            if r < n-1:
                neighbours(r+1, col)
            if col > 0:
                neighbours(r, col-1)
            if col < n-1:
                neighbours(r, col+1)    
            if r > 0 and col > 0:
                neighbours(r-1, col-1)
            if r > 0 and col < n-1:
                neighbours(r-1, col+1)
            if r < n-1 and col > 0:
                neighbours(r+1, col-1)
            if r < n-1 and col < n-1:
                neighbours(r+1, col+1)  
 
        # If the cell is not zero-valued            
        if numbers[r][col] != 0:
                mine_values[r][col] = numbers[r][col]
 
# Function for clearing the terminal
def clear():
    os.system("clear")      
 
# Function to display the instructions
def instructions():
    print("Instructions:")
    print("1. Enter row and column number to select a cell, Example \"2 3\"")
    print("2. In order to flag a mine, enter F after row and column numbers, Example \"2 3 F\"")
 
# Function to check for completion of the game
def check_over():
    global mine_values
    global n
    global mines_no
 
    # Count of all numbered values
    count = 0
 
    # Loop for checking each cell in the grid
    for r in range(n):
        for col in range(n):
 
            # If cell not empty or flagged
            if mine_values[r][col] != ' ' and mine_values[r][col] != 'F':
                count = count + 1
     
    # Count comparison          
    if count == n * n - mines_no:
        return True
    else:
        return False
 
# Display all the mine locations                    
def show_mines():
    global mine_values
    global numbers
    global n
 
    for r in range(n):
        for col in range(n):
            if numbers[r][col] == -1:
                mine_values[r][col] = 'M'
 
 
if __name__ == "__main__":
 
    # Size of grid
    n = 8
    # Number of mines
    mines_no = 8
 
    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)] 
    # The apparent values of the grid
    mine_values = [[' ' for y in range(n)] for x in range(n)]
    # The positions that have been flagged
    flags = []
 
    # Set the mines
    set_mines()
 
    # Set the values
    set_values()
 
    # Display the instructions
    instructions()
 
    # Variable for maintaining Game Loop
    over = False
         
    # The GAME LOOP 
    while not over:
        print_mines_layout()
 
        # Input from the user
        inp = input("Enter row number followed by space and column number = ").split()
         
        # Standard input
        if len(inp) == 2:
 
            # Try block to handle errant input
            try: 
                val = list(map(int, inp))
            except ValueError:
                clear()
                print("Wrong input!")
                instructions()
                continue
 
        # Flag input
        elif len(inp) == 3:
            if inp[2] != 'F' and inp[2] != 'f':
                clear()
                print("Wrong Input!")
                instructions()
                continue
 
            # Try block to handle errant input  
            try:
                val = list(map(int, inp[:2]))
            except ValueError:
                clear()
                print("Wrong input!")
                instructions()
                continue
 
            # Sanity checks 
            if val[0] > n or val[0] < 1 or val[1] > n or val[1] < 1:
                clear()
                print("Wrong input!")
                instructions()
                continue
 
            # Get row and column numbers
            r = val[0]-1
            col = val[1]-1 
 
            # If cell already been flagged
            if [r, col] in flags:
                clear()
                print("Flag already set")
                continue
 
            # If cell already been displayed
            if mine_values[r][col] != ' ':
                clear()
                print("Value already known")
                continue
 
            # Check the number for flags    
            if len(flags) < mines_no:
                clear()
                print("Flag set")
 
                # Adding flag to the list
                flags.append([r, col])
                 
                # Set the flag for display
                mine_values[r][col] = 'F'
                continue
            else:
                clear()
                print("Flags finished")
                continue    
 
        else: 
            clear()
            print("Wrong input!")   
            instructions()
            continue
             
 
        # Sanity checks
        if val[0] > n or val[0] < 1 or val[1] > n or val[1] < 1:
            clear()
            print("Wrong Input!")
            instructions()
            continue
             
        # Get row and column number
        r = val[0]-1
        col = val[1]-1
 
        # Unflag the cell if already flagged
        if [r, col] in flags:
            flags.remove([r, col])
 
        # If landing on a mine --- GAME OVER    
        if numbers[r][col] == -1:
            mine_values[r][col] = 'M'
            show_mines()
            print_mines_layout()
            print("Landed on a mine. GAME OVER!!!!!")
            over = True
            continue
 
        # If landing on a cell with 0 mines in neighboring cells
        elif numbers[r][col] == 0:
            vis = []
            mine_values[r][col] = '0'
            neighbours(r, col)
 
        # If selecting a cell with atleast 1 mine in neighboring cells  
        else:   
            mine_values[r][col] = numbers[r][col]
 
        # Check for game completion 
        if(check_over()):
            show_mines()
            print_mines_layout()
            print("Congratulations!!! YOU WIN")
            over = True
            continue
        clear() 

结论

我们希望本教程能让大家明白如何创建自己的扫雷游戏,并从中获得乐趣。如有任何疑问,欢迎在下方评论。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1909666.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

单例模式(大话设计模式)C/C++版本

单例模式 C 饿汉 /* HM hungry man 饿汉 */ #include <iostream> using namespace std; class Singleton { private:Singleton() { cout << "单例对象创建&#xff01;" << endl; };Singleton(const Singleton &);Singleton &operator(c…

app: 和 android:的区别

人不走空 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌赋&#xff1a;斯是陋室&#xff0c;惟吾德馨 目录 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌…

如何切换手机的ip地址

在数字时代的浪潮中&#xff0c;智能手机已成为我们日常生活中不可或缺的一部分。然而&#xff0c;随着网络安全问题的日益凸显&#xff0c;保护个人隐私和数据安全变得尤为重要。其中&#xff0c;IP地址作为网络身份的重要标识&#xff0c;其安全性与隐私性备受关注。本文将详…

百度旋转验证码

声明(lianxi a15018601872) 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 前言(lianxi a…

OpenWrt入门 (1) - 登录及ssh命令接入wifi

本文参考自: [OpenWrt 维基]在 OpenWrt 上启用 Wi-Fi 接入点 --- [OpenWrt Wiki] Enabling a Wi-Fi access point on OpenWrt 需要详细了解的小伙伴请看原文 基本概念 OpenWrt是适用于嵌入式设备的一个Linux发行版。 相对原厂固件而言&#xff0c;OpenWrt不是一个单一、静态…

手写简单实现IOC

这个小demo是利用反射从最基础一步一步模拟实现了IOC的功能,所有的代码基本都给出了注释,方便大家阅读. 目录结构&#xff1a; 这里需要导入一下junit依赖 <!-- junit测试 --><dependency><groupId>junit</groupId><artifactId>junit</artif…

string类(STL开始)

相信大家都知道STL在C中的重要性&#xff0c;作为其模板库中的一部分&#xff0c;包含了常见的数据结构和算法&#xff0c;是C的标准库 而我们今天要讲的String类&#xff08;String底层是一个字符顺序数组的顺序表对象&#xff0c;可以归类为容器&#xff09;&#xff0c;其实…

JavaWeb系列二十二: 线程数据共享和安全(ThreadLocal)

韩顺平-线程数据共享和安全ThreadLocal 什么是ThreadLocal?ThreadLocal环境搭建ThreadLocal快速入门ThreadLocal源码阅读threadLocal.set()源码threadLocal.get()源码 什么是ThreadLocal? ThreadLocal的作用: 可以实现在同一个线程数据共享, 从而解决多线程数据安全问题.Thr…

一.6 存储设备形成层次结构

在处理器和一个较大的较慢的设备&#xff08;例如主存&#xff09;之间插入一个更小更快的存储设备&#xff08;例如高速缓存&#xff09;的想法已经成为一个普遍的概念。实际上&#xff0c;每个计算机系统重的存储设备都被组织成了一个存储器层次结构&#xff0c;如图1-9所示。…

楼梯导航案例

楼梯导航 <!DOCTYPE html> <html lang"zh-cn"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>滚动到展示区</title><link re…

Python 中将字典内容保存到 Excel 文件使用详解

概要 在数据处理和分析的过程中,经常需要将字典等数据结构保存到Excel文件中,以便于数据的存储、共享和进一步分析。Python提供了丰富的库来实现这一功能,其中最常用的是pandas和openpyxl。本文将详细介绍如何使用这些库将字典内容保存到Excel文件中,并包含具体的示例代码…

NodeJS小饰品销售管理系统-计算机毕业设计源码21597

摘 要 在当今的数字化时代&#xff0c;电子商务已经成为了商业领域中不可或缺的一部分。随着消费者对于购物体验的要求越来越高&#xff0c;一个高效、便捷、用户友好的小饰品销售管理系统显得尤为重要。 本系统旨在利用 JavaScript 技术&#xff0c;设计并实现一个功能强大的小…

Python实现动态银河系:模拟旋转的银河动画

文章目录 引言准备工作前置条件 代码实现与解析导入必要的库初始化Pygame定义星系类主循环 完整代码 引言 银河系的旋转动画是一个迷人且富有挑战性的项目。通过模拟星系的旋转&#xff0c;我们可以更好地理解天文学现象&#xff0c;并创造出视觉上令人惊叹的效果。在这篇博客…

计算机网络 - 万字长文

计算机网络 二、计算机网络2.1 七层模型表格2.2 通俗讲解七层模型2.3 TCP与UDP对比2.4 TCP 三次握手过程==为什么握手是三次,而不是两次或者四次?====三次握手可以携带数据吗?====TCP三次握手失败,服务端会如何处理?====什么是半连接队列?全连接====ISN(Initial Sequence…

昇思MindSpore学习入门-CELL与参数一

Cell作为神经网络构造的基础单元&#xff0c;与神经网络层(Layer)的概念相对应&#xff0c;对Tensor计算操作的抽象封装&#xff0c;能够更准确清晰地对神经网络结构进行表示。除了基础的Tensor计算流程定义外&#xff0c;神经网络层还包含了参数管理、状态管理等功能。而参数(…

【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决&#xff1a;&#xff08;最新版selenium框架元素定位报错&#xff09;NameError: name ‘By’ is not defined 一、分析问题背景 在使用Selenium进行Web自动化测试或爬虫开…

R包:‘patchwork合并多个R图的包‘

介绍 patchwork是基于gglot2的拼图包&#xff0c;它使得基于ggplot2的图形更容易拼接在同一个图层。 安装 因为作者仅仅在GitHub发布了patchwork&#xff0c;因此无法使用install.packages("patchwork")从CRAN处获取。为了获取该包&#xff0c;首先应该安装devtoo…

十进制与十六进制,和二进制的相互转变

十六进制与十进制 十六进制&#xff08;Hexadecimal&#xff09;是一种进位制&#xff0c;基数为16&#xff0c;常用于计算机科学和电子工程中。十六进制使用16个符号来表示数值&#xff1a;0-9表示0到9&#xff0c;A-F表示10到15。十六进制的每一位可以表示4位二进制数&#…

万界星空科技MES:磷酸铁锂正极新材料生产管理系统

磷酸铁锂MES通过对生产现场的数据进行实时采集、处理和监控&#xff0c;实现对生产过程的优化和控制。它可以实时监控生产设备的运行状态、物料的使用情况、产品的生产进度等信息&#xff0c;并根据这些信息对生产过程进行调整和优化。例如&#xff0c;当发现某个生产设备的故障…

电脑桌面日历记事本怎么弄 好用的桌面日历记事本

在这个数字化的时代&#xff0c;电脑已成为我们日常生活中不可或缺的伙伴。我常常在电脑上记录各种事项&#xff0c;以便随时查看和提醒自己。而我最钟爱的记事方式&#xff0c;莫过于使用桌面日历记事本。 想象一下&#xff0c;你的电脑桌面上有一个直观的日历&#xff0c;每…