PyGame贪吃蛇的实现代码示例
作者:Harpsichord1207 时间:2021-04-27 12:09:33
最近帮人做了个贪吃蛇的游戏(交作业用),很简单,界面如下:
开始界面:
游戏中界面:
是不是很简单、朴素。(欢迎大家访问GitHub)
游戏是基于PyGame
框架制作的,程序核心逻辑如下:
游戏界面分辨率是640*480,蛇和食物都是由1个或多个20*20像素的正方形块儿(为了方便,下文用点表示20*20像素的正方形块儿)组成,这样共有32*24个点,使用pygame.draw.rect来绘制每一个点;
初始化时蛇的长度是3,食物是1个点,蛇初始的移动的方向是右,用一个数组代表蛇,数组的每个元素是蛇每个点的坐标,因此数组的第一个坐标是蛇尾,最后一个坐标是蛇头;
游戏开始后,根据蛇的当前移动方向,将蛇运动方向的前方的那个点append到蛇数组的末位,再把蛇尾去掉,蛇的坐标数组就相当于往前挪了一位;
如果蛇吃到了食物,即蛇头的坐标等于食物的坐标,那么在第2点中蛇尾就不用去掉,就产生了蛇长度增加的效果;食物被吃掉后,随机在空的位置(不能与蛇的身体重合)再生成一个;
通过PyGame的event监控按键,改变蛇的方向,例如当蛇向右时,下一次改变方向只能向上或者向下;
当蛇撞上自身或墙壁,游戏结束,蛇头装上自身,那么蛇坐标数组里就有和舌头坐标重复的数据,撞上墙壁则是蛇头坐标超过了边界,都很好判断;
其他细节:做了个开始的欢迎界面;食物的颜色随机生成;吃到实物的时候有声音提示等。
代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@version: v1.0
@author: Harp
@contact: liutao25@baidu.com
@software: PyCharm
@file: MySnake.py
@time: 2018/1/15 0015 23:40
"""
import pygame
from os import path
from sys import exit
from time import sleep
from random import choice
from itertools import product
from pygame.locals import QUIT, KEYDOWN
def direction_check(moving_direction, change_direction):
directions = [['up', 'down'], ['left', 'right']]
if moving_direction in directions[0] and change_direction in directions[1]:
return change_direction
elif moving_direction in directions[1] and change_direction in directions[0]:
return change_direction
return moving_direction
class Snake:
colors = list(product([0, 64, 128, 192, 255], repeat=3))[1:-1]
def __init__(self):
self.map = {(x, y): 0 for x in range(32) for y in range(24)}
self.body = [[100, 100], [120, 100], [140, 100]]
self.head = [140, 100]
self.food = []
self.food_color = []
self.moving_direction = 'right'
self.speed = 4
self.generate_food()
self.game_started = False
def check_game_status(self):
if self.body.count(self.head) > 1:
return True
if self.head[0] < 0 or self.head[0] > 620 or self.head[1] < 0 or self.head[1] > 460:
return True
return False
def move_head(self):
moves = {
'right': (20, 0),
'up': (0, -20),
'down': (0, 20),
'left': (-20, 0)
}
step = moves[self.moving_direction]
self.head[0] += step[0]
self.head[1] += step[1]
def generate_food(self):
self.speed = len(self.body) // 16 if len(self.body) // 16 > 4 else self.speed
for seg in self.body:
x, y = seg
self.map[x//20, y//20] = 1
empty_pos = [pos for pos in self.map.keys() if not self.map[pos]]
result = choice(empty_pos)
self.food_color = list(choice(self.colors))
self.food = [result[0]*20, result[1]*20]
def main():
key_direction_dict = {
119: 'up', # W
115: 'down', # S
97: 'left', # A
100: 'right', # D
273: 'up', # UP
274: 'down', # DOWN
276: 'left', # LEFT
275: 'right', # RIGHT
}
fps_clock = pygame.time.Clock()
pygame.init()
pygame.mixer.init()
snake = Snake()
sound = False
if path.exists('eat.wav'):
sound_wav = pygame.mixer.Sound("eat.wav")
sound = True
title_font = pygame.font.SysFont('arial', 32)
welcome_words = title_font.render('Welcome to My Snake', True, (0, 0, 0), (255, 255, 255))
tips_font = pygame.font.SysFont('arial', 24)
start_game_words = tips_font.render('Click to Start Game', True, (0, 0, 0), (255, 255, 255))
close_game_words = tips_font.render('Press ESC to Close', True, (0, 0, 0), (255, 255, 255))
gameover_words = title_font.render('GAME OVER', True, (205, 92, 92), (255, 255, 255))
win_words = title_font.render('THE SNAKE IS LONG ENOUGH AND YOU WIN!', True, (0, 0, 205), (255, 255, 255))
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption('My Snake')
new_direction = snake.moving_direction
while 1:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
if event.key == 27:
exit()
if snake.game_started and event.key in key_direction_dict:
direction = key_direction_dict[event.key]
new_direction = direction_check(snake.moving_direction, direction)
elif (not snake.game_started) and event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if 213 <= x <= 422 and 304 <= y <= 342:
snake.game_started = True
screen.fill((255, 255, 255))
if snake.game_started:
snake.moving_direction = new_direction # 在这里赋值,而不是在event事件的循环中赋值,避免按键太快
snake.move_head()
snake.body.append(snake.head[:])
if snake.head == snake.food:
if sound:
sound_wav.play()
snake.generate_food()
else:
snake.body.pop(0)
for seg in snake.body:
pygame.draw.rect(screen, [0, 0, 0], [seg[0], seg[1], 20, 20], 0)
pygame.draw.rect(screen, snake.food_color, [snake.food[0], snake.food[1], 20, 20], 0)
if snake.check_game_status():
screen.blit(gameover_words, (241, 310))
pygame.display.update()
snake = Snake()
new_direction = snake.moving_direction
sleep(3)
elif len(snake.body) == 512:
screen.blit(win_words, (33, 210))
pygame.display.update()
snake = Snake()
new_direction = snake.moving_direction
sleep(3)
else:
screen.blit(welcome_words, (188, 100))
screen.blit(start_game_words, (236, 310))
screen.blit(close_game_words, (233, 350))
pygame.display.update()
fps_clock.tick(snake.speed)
if __name__ == '__main__':
main()
来源:https://segmentfault.com/a/1190000013583165
标签:PyGame,贪吃蛇
0
投稿
猜你喜欢
mysql 8.0.12 解压版安装教程
2024-01-24 15:31:57
Python 描述符(Descriptor)入门
2022-10-15 21:06:59
vue中keep-alive组件的入门使用教程
2023-07-02 16:38:51
你凭什么说你的网站用户体验好
2011-03-31 17:08:00
详解Python 中的容器 collections
2021-09-12 07:11:38
解决django FileFIELD的编码问题
2023-07-01 04:28:42
Laravel中数据库迁移操作的示例详解
2023-05-25 06:27:38
讲解MySQL数据库的数据类型和建库策略
2008-12-17 14:39:00
Python序列化基础知识(json/pickle)
2021-04-24 01:09:34
Python中实现字符串类型与字典类型相互转换的方法
2021-07-01 20:37:25
FFrpc python客户端lib使用解析
2023-09-06 00:12:20
python 安全地删除列表元素的方法
2022-11-20 16:21:35
pycharm快捷键汇总
2022-12-30 22:43:40
JavaScript高级程序设计 读书笔记之十 本地对象Date日期
2024-04-22 22:33:48
Go并发编程sync.Cond的具体使用
2024-05-13 10:41:00
Active Server Pages 错误 ASP 0201 修复方法
2011-03-06 10:41:00
Pandas数值排序 sort_values()的使用
2023-02-21 01:12:02
python日期与时间戳的各种转换示例
2021-05-23 07:47:33
Python学习之sys模块使用教程详解
2021-03-31 01:38:50
ASP 使用Filter函数来检索数组
2011-04-30 16:49:00