python小游戏代码_python 贪吃蛇的源代码(简单的小游戏)

52892b4fce3e164cccf6199924e7ee35.png

话不多说,直接上代码,有需要的小伙伴可以直接拿去用:

import pygame
import random
import sys
import time
from pygame.locals import * # 从pygame模块导入常用的函数和常量
# 定义颜色变量
black_colour = pygame.Color(0, 0, 0)
white_colour = pygame.Color(255, 255, 255)
red_colour = pygame.Color(255, 0, 0)
grey_colour = pygame.Color(150, 150, 150)
# 定义游戏结束函数
def GameOver(gamesurface):
# 设置提示字体的格式
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)
# 设置提示字体的颜色
GameOver_colour = GameOver_font.render('Game Over', True, grey_colour)
# 设置提示位置
GameOver_location = GameOver_colour.get_rect()
GameOver_location.midtop = (320, 10)
gamesurface.blit(GameOver_colour, GameOver_location)
# 提示运行信息
pygame.display.flip()
# 等待5秒
time.sleep(5)
# 退出游戏
pygame.quit()
# 退出程序
sys.exit()
# 定义主函数
def main():
# 初始化pygame
pygame.init()
pygame.time.Clock()
ftpsClock = pygame.time.Clock()
# 创建一个窗口
gamesurface = pygame.display.set_mode((640, 480))
# 设置窗口的标题
pygame.display.set_caption('贪吃蛇~')
# 初始化变量
# 初始化贪吃蛇的起始位置
snakeposition = [100, 100]
# 初始化贪吃蛇的长度
snakelength = [[100, 100], [80, 100], [60, 100]]
# 初始化目标方块的位置
square_purpose = [300, 300]
# 初始化一个数来判断目标方块是否存在
square_position = 1
# 初始化方向,用来使贪吃蛇移动
derection = "right"
change_derection = derection
# 进行游戏主循环
while True:
# 检测按键
for event in pygame.event.get():
if event.type == QUIT:
# 退出程序
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
# 判断按的键,用w,s,a,d来表示上下左右
if event.key == K_RIGHT or event.key == ord('d'):
change_derection = "right"
if event.key == K_LEFT or event.key == ord('a'):
change_derection = "left"
if event.key == K_UP or event.key == ord('w'):
change_derection = "up"
if event.key == K_DOWN or event.key == ord('s'):
change_derection = "down"
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
# 判断移动的方向是否相反
if change_derection == 'left' and not derection == 'right':
derection = change_derection
if change_derection == 'right' and not derection == 'left':
derection = change_derection
if change_derection == 'up' and not derection == 'down':
derection = change_derection
if change_derection == 'down' and not derection == 'up':
derection = change_derection
# 根据方向,改变坐标
if derection == 'left':
snakeposition[0] -= 20
if derection == 'right':
snakeposition[0] += 20
if derection == 'up':
snakeposition[1] -= 20
if derection == 'down':
snakeposition[1] += 20
# 增加蛇的长度
snakelength.insert(0, list(snakeposition))
# 判断是否吃掉目标方块
if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:
square_position = 0
else:
snakelength.pop()
# 重新生成目标方块
if square_position == 0:
# 随机生成x,y,扩大二十倍,在窗口范围内
x = random.randrange(1, 32)
y = random.randrange(1, 24)
square_purpose = [int(x * 20), int(y * 20)]
square_position = 1
# 绘制pygame显示层
gamesurface.fill(black_colour)
for position in snakelength:
pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))
pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))
# 刷新pygame显示层
pygame.display.flip()
# 判断是否死亡
if snakeposition[0] < 0 or snakeposition[0] > 620:
GameOver(gamesurface)
if snakeposition[1] < 0 or snakeposition[1] > 460:
GameOver(gamesurface)
for snakebody in snakelength[1:]:
if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
GameOver(gamesurface)
# 控制游戏速度
ftpsClock.tick(5)
if __name__ == "__main__":
main()

想要更多代码或者想学习的小伙伴:

Green_F的博客_范祸祸_CSDN博客​blog.csdn.net
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python实现的贪吃蛇游戏源代码,你可以在Python环境中运行它: ```python import pygame import time import random pygame.init() white = (255, 255, 255) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) dis_width = 800 dis_height = 600 dis = pygame.display.set_mode((dis_width, dis_height)) pygame.display.set_caption('Snake Game by Edureka') clock = pygame.time.Clock() snake_block = 10 snake_speed = 30 font_style = pygame.font.SysFont(None, 50) def our_snake(snake_block, snake_List): for x in snake_List: pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) dis.blit(mesg, [dis_width / 6, dis_height / 3]) def gameLoop(): game_over = False game_close = False x1 = dis_width / 2 y1 = dis_height / 2 x1_change = 0 y1_change = 0 snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 while not game_over: while game_close == True: dis.fill(blue) message("You Lost! Press Q-Quit or C-Play Again", red) 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: gameLoop() 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 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change dis.fill(blue) pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1) snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake: del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True our_snake(snake_block, snake_List) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() gameLoop() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值