pygame小游戏代码_用 python 写一个小游戏

本文介绍如何使用Python的pygame库从零开始编写贪吃蛇小游戏。通过设置窗口、绘制蛇和食物、监听键盘事件以及处理碰撞检测,实现游戏的基本功能。文章最后鼓励读者自行优化代码,增加更多特色功能。
摘要由CSDN通过智能技术生成

今天玩点别的吧

都说 Python 除了生孩子

什么都能干

e8d95d04ec78e420b8abb5952180232e.png

咱们今天

就用 Python 写个小游戏

贪吃蛇

598aed079ca76e94a76293c87d5651ec.gif

想必大家都玩过这玩意

有没有想过

你自己来实现一个这样的游戏呢

那么接下来就是

学习 python 的正确姿势

6207c6d81bb75cb8c32ede33a02f9606.png

想要用 Python 写一个小游戏

就不得不知道 pygame 这个库了

这个库是在 sdl 之上构建的

可以使用到很多 GUI、渲染画布等

废话不多说

我们开始

首先我们得有个窗口

通过 pygame 的初始化就

可以使用它提供给我们的工具了

5aa6d2932e7a6981c1acfb3499d502cf.png

既然有东西可用了

来设置一下游戏名称吧

想了想

就叫做

《帅气的python》

d5d1b2517edb813cc81f192c43bd7785.png

恩,很符合小帅b的气质

20303e9efc5da0486760fc2e6415fcfd.png

通过 set_model 方法

可以设置窗口的大小

比如:

e59f3550dd7c3f3af37f1d6a509ffce8.png

那么这时候得到的窗口就是这样

好的,我可以帮你设计一个经典的贪吃蛇小游戏的源代码。以下是代码: ```python import pygame import random # 初始化游戏 pygame.init() # 游戏界面大小 width = 640 height = 480 screen = pygame.display.set_mode((width, height)) # 设置游戏标题 pygame.display.set_caption("贪吃蛇小游戏") # 设置游戏时钟 clock = pygame.time.Clock() # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) green = (0, 255, 0) # 定义蛇的初始位置 snake_pos = [100, 50] snake_body = [[100, 50], [90, 50], [80, 50]] # 定义食物的初始位置 food_pos = [random.randrange(1, 64) * 10, random.randrange(1, 48) * 10] food_spawned = 1 # 定义蛇的初始移动方向 direction = "RIGHT" change_to = direction # 定义游戏结束的函数 def game_over(): font = pygame.font.SysFont('Arial', 30) text = font.render('游戏结束!', True, red) screen.blit(text, [width // 4, height // 3]) pygame.display.flip() pygame.time.wait(2000) pygame.quit() quit() # 开始游戏循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: change_to = "RIGHT" elif event.key == pygame.K_LEFT: change_to = "LEFT" elif event.key == pygame.K_UP: change_to = "UP" elif event.key == pygame.K_DOWN: change_to = "DOWN" elif event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) # 判断蛇是否撞到墙壁 if snake_pos[0] > width or snake_pos[0] < 0 or snake_pos[1] > height or snake_pos[1] < 0: game_over() # 更新蛇的移动方向 if change_to == "RIGHT" and direction != "LEFT": direction = "RIGHT" elif change_to == "LEFT" and direction != "RIGHT": direction = "LEFT" elif change_to == "UP" and direction != "DOWN": direction = "UP" elif change_to == "DOWN" and direction != "UP": direction = "DOWN" # 移动蛇的身体 if direction == "RIGHT": snake_pos[0] += 10 elif direction == "LEFT": snake_pos[0] -= 10 elif direction == "UP": snake_pos[1] -= 10 elif direction == "DOWN": snake_pos[1] += 10 # 增加蛇的长度 snake_body.insert(0, list(snake_pos)) if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]: food_spawned = 0 else: snake_body.pop() # 生成新的食物 if food_spawned == 0: food_pos = [random.randrange(1, 64) * 10, random.randrange(1, 48) * 10] food_spawned = 1 # 更新游戏界面 screen.fill(black) for pos in snake_body: pygame.draw.rect(screen, green, pygame.Rect(pos[0], pos[1], 10, 10)) pygame.draw.rect(screen, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10)) # 刷新游戏界面 pygame.display.update() # 控制游戏帧率 clock.tick(15) ``` 这个小游戏是用 pygame 模块实现的,通过键盘控制贪吃蛇的移动方向,吃到食物后蛇会变长,撞到墙壁或者自己的身体就会游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值