python贪吃蛇编程代码大全_200行python代码实现贪吃蛇游戏

本文实例为大家分享了python实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

这次我们来写一个贪吃蛇游戏

下面贴出具体代码

import pygame

import time

import numpy as np

# 此模块包含游戏所需的常量

from pygame.locals import *

# 设置棋盘的长宽

BOARDWIDTH = 48

BOARDHEIGHT = 28

# 分数

score = 0

class Food(object):

def __init__(self):

self.item = (4, 5)

# 画出食物

def _draw(self, screen, i, j):

color = 255, 0, 255

radius = 10

width = 10

# i:1---34 j:1---25

position = 10 + 20 * i, 10 + 20 * j

# 画出半径为 10 的粉色实心圆

pygame.draw.circle(screen, color, position, radius, width)

# 随机产生食物

def update(self, screen, enlarge, snack):

if enlarge:

self.item = np.random.randint(1, BOARDWIDTH - 2), np.random.randint(1, BOARDHEIGHT - 2)

while self.item in snack.item:

self.item = np.random.randint(1, BOARDWIDTH - 2), np.random.randint(1, BOARDHEIGHT - 2)

self._draw(screen, self.item[0], self.item[1])

# 贪吃蛇

class Snack(object):

def __init__(self):

# self.item = [(3, 25), (2, 25), (1, 25), (1,24), (1,23),

# (1,22), (1,21), (1,20), (1,19), (1,18), (1,17), (1,16)]

# x 水平方向 y 竖直方向

# 初始方向竖直向上

self.item = [(3, 25), (2, 25), (1, 25), (1, 24), ]

self.x = 0

self.y = -1

def move(self, enlarge):

# enlarge 标记贪吃蛇有没有吃到食物

if not enlarge:

# 吃到食物删除尾部元素

self.item.pop()

# 新蛇头的坐标为旧蛇头坐标加上移动方向的位移

head = (self.item[0][0] + self.x, self.item[0][1] + self.y)

# 将新的蛇头坐标插入在 list 最前面

self.item.insert(0, head)

def eat_food(self, food):

global score

# snack_x,snack_y 蛇头坐标

# food_x, food_y 食物坐标

snack_x, snack_y = self.item[0]

food_x, food_y = food.item

# 比较蛇头坐标与食物坐标

if (food_x == snack_x) and (food_y == snack_y):

score += 100

return 1

else:

return 0

def toward(self, x, y):

# 改变蛇头朝向

if self.x * x >= 0 and self.y * y >= 0:

self.x = x

self.y = y

def get_head(self):

# 获取蛇头坐标

return self.item[0]

def draw(self, screen):

# 画出贪吃蛇

# 蛇头为半径为 15 的红色实心圆

radius = 15

width = 15

# i:1---34 j:1---25

color = 255, 0, 0

# position 为图形的坐标

position = 10 + 20 * self.item[0][0], 10 + 20 * self.item[0][1]

pygame.draw.circle(screen, color, position, radius, width)

# 蛇身为半径为 10 的黄色实心圆

radius = 10

width = 10

color = 255, 255, 0

for i, j in self.item[1:]:

position = 10 + 20 * i, 10 + 20 * j

pygame.draw.circle(screen, color, position, radius, width)

# 初始界面

def init_board(screen):

board_width = BOARDWIDTH

board_height = BOARDHEIGHT

color = 10, 255, 255

width = 0

# width:x, height:y

# 左右边框占用了 X: 0 35*20

for i in range(board_width):

pos = i * 20, 0, 20, 20

pygame.draw.rect(screen, color, pos, width)

pos = i * 20, (board_height - 1) * 20, 20, 20

pygame.draw.rect(screen, color, pos, width)

# 上下边框占用了 Y: 0 26*20

for i in range(board_height - 1):

pos = 0, 20 + i * 20, 20, 20

pygame.draw.rect(screen, color, pos, width)

pos = (board_width - 1) * 20, 20 + i * 20, 20, 20

pygame.draw.rect(screen, color, pos, width)

# 游戏失败

def game_over(snack):

broad_x, broad_y = snack.get_head()

flag = 0

old = len(snack.item)

new = len(set(snack.item))

# 游戏失败的两种可能

# 咬到自身

if new < old:

flag = 1

# 撞到边框

if broad_x == 0 or broad_x == BOARDWIDTH - 1:

flag = 1

if broad_y == 0 or broad_y == BOARDHEIGHT - 1:

flag = 1

if flag:

return True

else:

return False

# 打印字符

def print_text(screen, font, x, y, text, color=(255, 0, 0)):

# 在屏幕上打印字符

# text是需要打印的文本,color为字体颜色

# (x,y)是文本在屏幕上的位置

imgText = font.render(text, True, color)

screen.blit(imgText, (x, y))

# 按键

def press(keys, snack):

global score

# K_w 为 pygame.locals 中的常量

# keys[K_w] 返回 True or False

# 上移

if keys[K_w] or keys[K_UP]:

snack.toward(0, -1)

# 下移

elif keys[K_s] or keys[K_DOWN]:

snack.toward(0, 1)

# 左移

elif keys[K_a] or keys[K_LEFT]:

snack.toward(-1, 0)

# 右移

elif keys[K_d] or keys[K_RIGHT]:

snack.toward(1, 0)

# 重置游戏

elif keys[K_r]:

score = 0

main()

# 退出游戏

elif keys[K_ESCAPE]:

exit()

# 游戏初始化

def game_init():

# pygame 初始化

pygame.init()

# 设置游戏界面大小

screen = pygame.display.set_mode((BOARDWIDTH * 20, BOARDHEIGHT * 20))

# 设置游戏标题

pygame.display.set_caption('贪吃蛇游戏')

# sound = pygame.mixer.Sound(AUDIONAME)

# channel = pygame.mixer.find_channel(True)

# channel.play(sound)

return screen

# 开始游戏

def game(screen):

snack = Snack()

food = Food()

# 设置中文字体和大小

font = pygame.font.SysFont('SimHei', 20)

is_fail = 0

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

# 填充屏幕

screen.fill((0, 0, 100))

init_board(screen=screen)

# 获得用户按键命令

keys = pygame.key.get_pressed()

press(keys, snack)

# 游戏失败打印提示

if is_fail:

font2 = pygame.font.Font(None, 40)

print_text(screen, font2, 400, 200, "GAME OVER")

# 游戏主进程

if not is_fail:

enlarge = snack.eat_food(food)

food.update(screen, enlarge, snack)

snack.move(enlarge)

is_fail = game_over(snack=snack)

snack.draw(screen)

# 游戏刷新

pygame.display.update()

time.sleep(0.1)

# 主程序

def main():

screen = game_init()

game(screen)

if __name__ == '__main__':

main()

程序运行效果

简单截图了一下

可以按住方向键移动蛇的运动方向

202042490007109.gif

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Python贪吃蛇的完整代码示例: ```python import pygame import random # 初始化pygame pygame.init() # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 设置屏幕尺寸 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("贪吃蛇游戏") # 定义蛇的初始位置和速度 snake_block = 10 snake_speed = 30 x1 = screen_width / 2 y1 = screen_height / 2 x1_change = 0 y1_change = 0 # 定义食物的初始位置 foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0 # 定义蛇的身体 snake_body = [] snake_length = 1 clock = pygame.time.Clock() font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) def score(score): value = score_font.render("Score: " + str(score), True, WHITE) screen.blit(value, [0, 0]) def snake(snake_block, snake_body): for x in snake_body: pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block, snake_block]) def game_loop(): game_over = False game_close = False while not game_over: while game_close: screen.fill(BLACK) message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, RED) screen.blit(message, [screen_width / 6, screen_height / 3]) score(snake_length - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: game_loop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(BLACK) pygame.draw.rect(screen, RED, [foodx, foody, snake_block, snake_block]) snake_head = [] snake_head.append(x1) snake_head.append(y1) snake_body.append(snake_head) if len(snake_body) > snake_length: del snake_body[0] for x in snake_body[:-1]: if x == snake_head: game_close = True snake(snake_block, snake_body) score(snake_length - 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0 snake_length += 1 clock.tick(snake_speed) pygame.quit() game_loop() ``` 这是一个简单的贪吃蛇游戏代码,使用了Pygame库来实现图形化界面和游戏逻辑。你可以将代码复制到Python环境中运,并使用方向键来控制贪吃蛇的移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值