赞
踩
本游戏是在http://www.csdn.net/article/2013-05-02/2815101多线程的 Python 教程——“贪吃蛇”基础上改编而来,原教程里只实现了多个蛇在跑,然而不能吃东西。本在添加了生成苹果的程序,且加了向苹果靠近的算法。此程序须看原教程的简介,在看本文的代码简介,才能明白。
具体可以参考一下步骤学习:
1)看原教程http://www.csdn.net/article/2013-05-02/2815101
2)懂了之后看本文的源码
- # Threadworms (a Python/Pygame threading demonstration)
- # By Al Sweigart al@inventwithpython.com
- # http://inventwithpython.com/blog
- # Released under a "Simplified BSD" license
-
- # This is meant to be an educational example of multithreaded programming,
- # so I get kind of verbose in the comments.
-
- import random, pygame, sys, threading
- from pygame.locals import *
-
- # Setting up constants
- NUM_WORMS = 3 # the number of worms in the grid
- FPS = 30 # frames per second that the program runs
- CELL_SIZE = 20 # how many pixels wide and high each "cell" in the grid is
- CELLS_WIDE = 32 # how many cells wide the grid is
- CELLS_HIGH = 24 # how many cells high the grid is
-
-
- # Create the global grid data structure. GRID[x][y] contains None for empty
- # space or an RGB triplet. The grid is the shared data structure that the worms
- # write data to, and since each worm runs in a separate thread we will have to
- # add locks so that the worms don't step over each other when checking and
- # updating the values in this shared data structure.
- #
- # If we were not using threads, then it would be impossible for the worms
- # to step over each other since their code would always be executing in
- # normal order. (But then our program wouldn't be multithreaded.)
- GRID = []
- for x in range(CELLS_WIDE):
- GRID.append([None] * CELLS_HIGH)
-
- GRID_LOCK = threading.Lock() # pun was not intended
-
- # Constants for some colors.
- # R G B
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- DARKGRAY = ( 40, 40, 40)
- BGCOLOR = BLACK # color to use for the background of the grid
- GRID_LINES_COLOR = DARKGRAY # color to use for the lines of the grid
-
- # Calculate total pixels wide and high that the full window is
- WINDOWWIDTH = CELL_SIZE * CELLS_WIDE
- WINDOWHEIGHT = CELL_SIZE * CELLS_HIGH
-
- # Constants for the four cardinal directions, because a mistyped variable
- # like DWON will cause an immediate NameError crash and be easy to spot. But a
- # mistyped string like 'dwon' is still syntactically valid Python code, so
- # it will cause bugs that might be hard to track down.
- UP = 'up'
- DOWN = 'down'
- LEFT = 'left'
- RIGHT = 'right'
-
- # Since the data structure for a worm's body segments is a list
- # where the "head" is the first item in the list, we can use
- # HEAD as the index.
- HEAD = 0
-
- # In queues in computer science, the "tail" often doesn't refer to the last
- # item but rather *every* item after the head. So I'll use "butt" to refer
- # to the index of the last body segment for a worm.
- BUTT = -1 # negative indexes count from the end, so -1 will always be the last index
-
- # A global variable that the Worm threads check to see if they should exit.
- WORMS_RUNNING = True
-
- class Worm(threading.Thread): # "Thread" is a class in the "threading" module.
- def __init__(self, name='Worm', maxsize=None, color=None, speed=50):
- # name can be used for debugging purposes. It will appear in any thrown exceptions so you can tell which thread crashed.
- # maxsize is the length of the worm (in body segments).
- # color is an RGB tuple for the worm. The darker shade is automatically calculated.
- # speed is an integer of milliseconds the worm waits after moving once. 1000=move once a second, 0=move as fast as possible
-
- threading.Thread.__init__(self)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。