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

该博客介绍了如何基于Python的pygame库和多线程技术改进原有的贪吃蛇游戏,增加了苹果生成及蛇向苹果自动移动的算法。作者建议读者先学习一篇基础的多线程Python教程,然后查看提供的源码进行深入理解。文章提供了相关资源的下载链接。
摘要由CSDN通过智能技术生成

本游戏是在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) 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值