当前位置:   article > 正文

python + pygame实现多线程的贪吃蛇游戏_pygame多线程

pygame多线程

本游戏是在http://www.csdn.net/article/2013-05-02/2815101多线程的 Python 教程——“贪吃蛇”基础上改编而来,原教程里只实现了多个蛇在跑,然而不能吃东西。本在添加了生成苹果的程序,且加了向苹果靠近的算法。此程序须看原教程的简介,在看本文的代码简介,才能明白。

具体可以参考一下步骤学习:

1)看原教程http://www.csdn.net/article/2013-05-02/2815101

2)懂了之后看本文的源码

 

  1. # Threadworms (a Python/Pygame threading demonstration)
  2. # By Al Sweigart al@inventwithpython.com
  3. # http://inventwithpython.com/blog
  4. # Released under a "Simplified BSD" license
  5. # This is meant to be an educational example of multithreaded programming,
  6. # so I get kind of verbose in the comments.
  7. import random, pygame, sys, threading
  8. from pygame.locals import *
  9. # Setting up constants
  10. NUM_WORMS = 3 # the number of worms in the grid
  11. FPS = 30 # frames per second that the program runs
  12. CELL_SIZE = 20 # how many pixels wide and high each "cell" in the grid is
  13. CELLS_WIDE = 32 # how many cells wide the grid is
  14. CELLS_HIGH = 24 # how many cells high the grid is
  15. # Create the global grid data structure. GRID[x][y] contains None for empty
  16. # space or an RGB triplet. The grid is the shared data structure that the worms
  17. # write data to, and since each worm runs in a separate thread we will have to
  18. # add locks so that the worms don't step over each other when checking and
  19. # updating the values in this shared data structure.
  20. #
  21. # If we were not using threads, then it would be impossible for the worms
  22. # to step over each other since their code would always be executing in
  23. # normal order. (But then our program wouldn't be multithreaded.)
  24. GRID = []
  25. for x in range(CELLS_WIDE):
  26. GRID.append([None] * CELLS_HIGH)
  27. GRID_LOCK = threading.Lock() # pun was not intended
  28. # Constants for some colors.
  29. # R G B
  30. WHITE = (255, 255, 255)
  31. BLACK = ( 0, 0, 0)
  32. DARKGRAY = ( 40, 40, 40)
  33. BGCOLOR = BLACK # color to use for the background of the grid
  34. GRID_LINES_COLOR = DARKGRAY # color to use for the lines of the grid
  35. # Calculate total pixels wide and high that the full window is
  36. WINDOWWIDTH = CELL_SIZE * CELLS_WIDE
  37. WINDOWHEIGHT = CELL_SIZE * CELLS_HIGH
  38. # Constants for the four cardinal directions, because a mistyped variable
  39. # like DWON will cause an immediate NameError crash and be easy to spot. But a
  40. # mistyped string like 'dwon' is still syntactically valid Python code, so
  41. # it will cause bugs that might be hard to track down.
  42. UP = 'up'
  43. DOWN = 'down'
  44. LEFT = 'left'
  45. RIGHT = 'right'
  46. # Since the data structure for a worm's body segments is a list
  47. # where the "head" is the first item in the list, we can use
  48. # HEAD as the index.
  49. HEAD = 0
  50. # In queues in computer science, the "tail" often doesn't refer to the last
  51. # item but rather *every* item after the head. So I'll use "butt" to refer
  52. # to the index of the last body segment for a worm.
  53. BUTT = -1 # negative indexes count from the end, so -1 will always be the last index
  54. # A global variable that the Worm threads check to see if they should exit.
  55. WORMS_RUNNING = True
  56. class Worm(threading.Thread): # "Thread" is a class in the "threading" module.
  57. def __init__(self, name='Worm', maxsize=None, color=None, speed=50):
  58. # name can be used for debugging purposes. It will appear in any thrown exceptions so you can tell which thread crashed.
  59. # maxsize is the length of the worm (in body segments).
  60. # color is an RGB tuple for the worm. The darker shade is automatically calculated.
  61. # speed is an integer of milliseconds the worm waits after moving once. 1000=move once a second, 0=move as fast as possible
  62. threading.Thread.__init__(self)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/741729
推荐阅读
相关标签
  

闽ICP备14008679号