Home Backend Development Python Tutorial How to make games in python

How to make games in python

Jul 20, 2019 am 10:48 AM
python

PyGame is a Python library that makes it easier for you to write a game. It provides functions such as image processing and sound playback, and they can be easily integrated into your game.

How to make games in python

The following is the code for backgammon, and my understanding is written in the comments(recommended learning: Python Video tutorial)

import pygame
# 导入pygame模块
print(pygame.ver)
# 检查pygame的版本,检查pygame有没有导入成功

EMPTY = 0
BLACK = 1
WHITE = 2
# 定义三个常量函数,用来表示白棋,黑棋,以及 空

black_color = [0, 0, 0]
# 定义黑色(黑棋用,画棋盘)
white_color = [255, 255, 255]
# 定义白色(白棋用)

# 定义棋盘这个类
class RenjuBoard(object):

  def __init__(self):
    # self._board = board = [[EMPTY] * 15 for _ in range(15)]
    # 将棋盘每一个交叉点都看作列表的一个元素位,一共有15*15共225个元素
    self._board = [[]] * 15
    self.reset()
  #重置棋盘
  def reset(self):
    for row in range(len(self._board)):
      self._board[row] = [EMPTY] * 15
  #定义棋盘上的下棋函数,row表示行,col表示列,is_black表示判断当前点位该下黑棋,还是白棋
  def move(self, row, col, is_black):
    if self._board[row][col] == EMPTY:
      self._board[row][col] = BLACK if is_black else WHITE
      return True
    return False
  # 给棋盘定义一个函数将自己在screen上面画出来,使用pygame.draw()函数。并且顺便将下了的棋子也画出来
  def draw(self, screen):
    for h in range(1, 16):
      pygame.draw.line(screen, black_color,
               [40, h * 40], [600, h * 40], 1)
      pygame.draw.line(screen, black_color,
    # 给棋盘加一个外框,使美观
    pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 3)
    # 在棋盘上标出,天元以及另外4个特殊点位
    pygame.draw.circle(screen, black_color, [320, 320], 5, 0)
    pygame.draw.circle(screen, black_color, [160, 160], 3, 0)
    pygame.draw.circle(screen, black_color, [160, 480], 3, 0)
    pygame.draw.circle(screen, black_color, [480, 160], 3, 0)
    pygame.draw.circle(screen, black_color, [480, 480], 3, 0)
    #做2次for循环取得棋盘上所有交叉点的坐标
    for row in range(len(self._board)):
      for col in range(len(self._board[row])):
        # 将下在棋盘上的棋子画出来
        if self._board[row][col] != EMPTY:
          ccolor = black_color \
            if self._board[row][col] == BLACK else white_color
          # 取得这个交叉点下的棋子的颜色,并将棋子画出来
          pos = [40 * (col + 1), 40 * (row + 1)]
          # 画出棋子
          pygame.draw.circle(screen, ccolor, pos, 18, 0)

# 定义函数,传入当前棋盘上的棋子列表,输出结果,不管黑棋白棋胜,都是传回False,未出结果则为True
def is_win(board):
  for n in range(15):
    # 判断垂直方向胜利
    flag = 0
    # flag是一个标签,表示是否有连续以上五个相同颜色的棋子
    for b in board._board:
      if b[n] == 1:
        flag += 1
        if flag == 5:
          print('黑棋胜')
          return False
      else:
      # else表示此时没有连续相同的棋子,标签flag重置为0
        flag = 0

    flag = 0
    for b in board._board:
      if b[n] == 2:
        flag += 1
        if flag == 5:
          print('白棋胜')
          return False
      else:
        flag = 0

    # 判断水平方向胜利
    flag = 0
    for b in board._board[n]:
      if b == 1:
        flag += 1
        if flag == 5:
          print('黑棋胜')
          return False
      else:
        flag = 0

    flag = 0
    for b in board._board[n]:
      if b == 2:
        flag += 1
        if flag == 5:
          print('白棋胜')
          return False
      else:
        flag = 0

    # 判断正斜方向胜利

    for x in range(4, 25):
      flag = 0
      for i,b in enumerate(board._board):
        if 14 >= x - i >= 0 and b[x - i] == 1:
          flag += 1
          if flag == 5:
            print('黑棋胜')
            return False
        else:
          flag = 0

    for x in range(4, 25):
      flag = 0
      for i,b in enumerate(board._board):
        if 14 >= x - i >= 0 and b[x - i] == 2:
          flag += 1
          if flag == 5:
            print('白棋胜')
            return False
        else:
          flag = 0

    #判断反斜方向胜利
    for x in range(11, -11, -1):
      flag = 0
      for i,b in enumerate(board._board):
        if 0 <= x + i <= 14 and b[x + i] == 1:
          flag += 1
          if flag == 5:
            print('黑棋胜')
            return False
        else:
          flag = 0

    for x in range(11, -11, -1):
      flag = 0
      for i,b in enumerate(board._board):
        if 0 <= x + i <= 14 and b[x + i] == 2:
          flag += 1
          if flag == 5:
            print('白棋胜')
            return False
        else:
          flag = 0

  return True


def main():
  # 创建棋盘对象
  board = RenjuBoard()
  # 用于判断是下黑棋还是白棋
  is_black = True
  # pygame初始化函数,固定写法
  pygame.init()
  pygame.display.set_caption('五子棋') # 改标题
  # pygame.display.set_mode()表示建立个窗口,左上角为坐标原点,往右为x正向,往下为y轴正向
  screen = pygame.display.set_mode((640,640))
  # 给窗口填充颜色,颜色用三原色数字列表表示
  screen.fill([125,95,24])
  board.draw(screen) # 给棋盘类发命令,调用draw()函数将棋盘画出来
  pygame.display.flip() # 刷新窗口显示

  running = True
  # while 主循环的标签,以便跳出循环
  while running:
    # 遍历建立窗口后发生的所有事件,固定写法
    for event in pygame.event.get():
      # 根据事件的类型,进行判断
      if event.type == pygame.QUIT:
        running = False

      elif event.type == pygame.KEYUP:
        pass
      # pygame.MOUSEBUTTONDOWN表示鼠标的键被按下
      elif event.type == pygame.MOUSEBUTTONDOWN and \
          event.button == 1:# button表示鼠标左键
        x, y = event.pos # 拿到鼠标当前在窗口上的位置坐标
        # 将鼠标的(x, y)窗口坐标,转化换为棋盘上的坐标
        row = round((y - 40) / 40)   
        col = round((x - 40) / 40)
        if board.move(row, col, is_black):
          is_black = not is_black
          screen.fill([125, 95, 24])
          board.draw(screen)
          pygame.display.flip()
          # 调用判断胜负函数
          if not is_win(board):
            #break
          running = False
# 这里我有个bug没找到解决办法,就是判断出胜负后,使用break跳出事件遍历的for循环,但是老是不能跳出来,导致胜负分出来了
#还可以继续下,这里我采用判断胜负后就将running标签赋值为False,跳出主循环,但是这样棋盘的窗口也没了。明天再找找bug在哪

  pygame.quit()

if __name__ == '__main__':
  main()
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to make games in python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles