搜索
首页后端开发Python教程python怎么做游戏

python怎么做游戏

Jul 20, 2019 am 10:48 AM
python

PyGame是一个Python的库,能够让你更容易的写出一个游戏。它提供的功能包括图片处理和声音重放的功能,并且它们能很容易的整合进你的游戏里。

python怎么做游戏

下面是五子棋的代码,我的理解都写在注释里了(推荐学习:Python视频教程

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()

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上是python怎么做游戏的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

对于循环和python中的循环时:每个循环的优点是什么?对于循环和python中的循环时:每个循环的优点是什么?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用