资源简介
一个贪吃蛇小游戏
代码片段和文件信息
# Wormy (a Nibbles clone)
# By Al Sweigart al@inventwithpython.com
# http://inventwithpython.com/pygame
# Released under a “Simplified BSD“ license
import random pygame sys
from pygame.locals import *
FPS = 12
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
CELLSIZE = 20
assert WINDOWWIDTH % CELLSIZE == 0 “Window width must be a multiple of cell size.“
assert WINDOWHEIGHT % CELLSIZE == 0 “Window height must be a multiple of cell size.“
CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)
# R G B
WHITE = (255 255 255)
BLACK = ( 0 0 0)
RED = (255 0 0)
GREEN = ( 0 255 0)
DARKGREEN = ( 0 155 0)
DARKGRAY = ( 40 40 40)
BGCOLOR = BLACK
UP = ‘up‘
DOWN = ‘down‘
LEFT = ‘left‘
RIGHT = ‘right‘
HEAD = 0 # syntactic sugar: index of the worm‘s head
def main():
global FPSCLOCK DISPLAYSURF BASICFONT
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH WINDOWHEIGHT))
BASICFONT = pygame.font.Font(‘freesansbold.ttf‘ 18)
pygame.display.set_caption(‘Wormy‘)
showStartScreen()
while True:
runGame()
showGameOverScreen()
def runGame():
# Set a random start point.
startx = random.randint(5 CELLWIDTH - 6)
starty = random.randint(5 CELLHEIGHT - 6)
wormCoords = [{‘x‘: startx ‘y‘: starty}
{‘x‘: startx - 1 ‘y‘: starty}
{‘x‘: startx - 2 ‘y‘: starty}]
direction = RIGHT
# Start the apple in a random place.
apple = getRandomLocation()
while True: # main game loop
for event in pygame.event.get(): # event handling loop
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
direction = LEFT
elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
direction = RIGHT
elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
direction = UP
elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
direction = DOWN
elif event.key == K_ESCAPE:
terminate()
# check if the worm has hit itself or the edge
if wormCoords[HEAD][‘x‘] == -1 or wormCoords[HEAD][‘x‘] == CELLWIDTH or wormCoords[HEAD][‘y‘] == -1 or wormCoords[HEAD][‘y‘] == CELLHEIGHT:
return # game over
for wormBody in wormCoords[1:]:
if wormBody[‘x‘] == wormCoords[HEAD][‘x‘] and wormBody[‘y‘] == wormCoords[HEAD][‘y‘]:
return # game over
# check if worm has eaten an apply
if wormCoords[HEAD][‘x‘] == apple[‘x‘] and wormCoords[HEAD][‘y‘] == apple[‘y‘]:
# don‘t re
- 上一篇:量子计算与量子信息
- 下一篇:南瓜书(PumpkinBook)
相关资源
- Python简单小游戏 五子棋
- python 井字棋 游戏源码
- 70行代码实现贪吃蛇完整游戏功能
- 贪吃蛇游戏.py
- python pygame 自制飞机大战游戏(加入
- 《升级》扑克牌游戏——Python实现
- 使用python编写的打飞机游戏源码
- 扑克小游戏python代码
- Python和Pygame游戏开发
- 一个猥琐的贪吃蛇Python pygame
- XATU_Project.zip
- pygame小游戏-全民打飞机(关卡选择飞
- 关于吃豆人的游戏代码python
- Python小游戏我自己写的
- Python贪吃蛇源码+背景音乐+中文字体
- 基于python的小游戏 含源代码
- python太空大战游戏 源代码完整版
- Python微信打飞机小游戏
- 连连看.zip
- 使用python自己实现神经网络操纵赛车
- Python开发五子棋小游戏
- python版植物大战僵尸源码
- Python雷电飞机大战小游戏
- python飞机大战项目.zip
- python推箱子游戏
- python贪吃蛇进阶版完整代码
- python游戏编程入门--高清版
- Python编写的超级马里奥游戏
- python2.7基于tkinter下实现拼图小游戏
- python实现游戏外星人入侵
评论
共有 条评论