找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 78|回复: 3

学生如何使用PYTHON3创建贪吃蛇游戏

[复制链接]

39

主题

4

回帖

161

积分

管理员

积分
161
发表于 2025-8-26 19:23:49 | 显示全部楼层 |阅读模式
如何安装PYTHON3

一、win10安装python3的步骤:
1、在官网下载Python3.X安装包
2、下载完成后,双击安装包文件安装
3、勾选Add Python 3.6 to PATH(勾选此选项在安装时自动配置环境变量),点击Customize installation(自定义安装)
4、在这个界面,勾选好,然后点击Next按钮
5、勾选Install for all users,再点击Install按钮看详情
6、等待安装
7、安装成功,点击Close按钮关闭
8、打开cmd命令行工具,输入python --version,有下图回显,说明安装成功

以上就是win10怎么安装python3的详细内容!




39

主题

4

回帖

161

积分

管理员

积分
161
 楼主| 发表于 2025-9-12 15:28:42 | 显示全部楼层
如何安装PYCharm
1.进入PyCharm官网
https://www.jetbrains.com/pycharm/download/#section=windows

2.安装Pycharm
根据实际修改路径

全部勾选

安装完成啦!

39

主题

4

回帖

161

积分

管理员

积分
161
 楼主| 发表于 2025-9-12 15:32:20 | 显示全部楼层
这篇文章引导读者通过打开PyCharm,创建新项目,自定义项目名称,并进行运行操作,从而熟悉Python开发环境的设置过程。
1.       打开PyCharm
2.点击New Project
file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg项目名字编辑下自己的
3.create创建进入开始编程
4.编程完成后运行主程序

5.查看运行结果

39

主题

4

回帖

161

积分

管理员

积分
161
 楼主| 发表于 2025-9-12 15:36:44 | 显示全部楼层
给学生的贪吃蛇PYTHON游戏制作导学案
贪吃蛇游戏 (Python3 +Pygame)
以下是一个完整的单文件贪吃蛇游戏实现,不需要任何外部素材文件:
请在PYCHARM内完成后
import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 游戏常量
WIDTH, HEIGHT =800, 600
GRID_SIZE = 20
GRID_WIDTH =WIDTH // GRID_SIZE
GRID_HEIGHT =HEIGHT // GRID_SIZE

# 颜色定义
BLACK = (0, 0,0)
WHITE = (255,255, 255)
RED = (255, 0,0)
GREEN = (0, 255,0)
DARK_GREEN = (0,155, 0)
GRAY = (128,128, 128)

# 创建游戏窗口
screen =pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
clock = pygame.time.Clock()

# 字体
font =pygame.font.SysFont('simhei', 36)
small_font =pygame.font.SysFont('simhei', 24)

class Snake:
    def __init__(self):
        self.reset()
   
    def reset(self):
        self.body = [(GRID_WIDTH // 2,GRID_HEIGHT // 2)]
        self.direction = (1, 0)  # 初始向右
        self.length = 1
        self.score = 0
        self.speed = 10  # 初始速度
   
    def move(self):
        head_x, head_y = self.body[0]
        dx, dy = self.direction
        new_head = (head_x + dx, head_y + dy)
        
        # 检查碰撞
        if (new_head in self.body[1:] or
            new_head[0] < 0 or new_head[0]>= GRID_WIDTH or
            new_head[1] < 0 or new_head[1]>= GRID_HEIGHT):
            return False
        
        self.body.insert(0, new_head)
        if len(self.body) > self.length:
            self.body.pop()
        return True
   
    def grow(self):
        self.length += 1
        self.score += 10
        # 每增长5节增加速度
        if self.length % 5 == 0 and self.speed< 20:
            self.speed += 1
   
    def change_direction(self, new_direction):
        # 防止180度转弯
        opposite_directions = {
            (1, 0): (-1, 0),
            (-1, 0): (1, 0),
            (0, 1): (0, -1),
            (0, -1): (0, 1)
        }
        if new_direction !=opposite_directions.get(self.direction, (0, 0)):
            self.direction = new_direction

class Food:
    def __init__(self, snake):
        self.position =self.generate_position(snake)
   
    def generate_position(self, snake):
        while True:
            position = (
                random.randint(0, GRID_WIDTH -1),
                random.randint(0, GRID_HEIGHT -1)
            )
            if position not in snake.body:
                return position
   
    def draw(self):
        x, y = self.position
        rect = pygame.Rect(x * GRID_SIZE, y *GRID_SIZE, GRID_SIZE, GRID_SIZE)
        
        # 绘制苹果形状的食物
        pygame.draw.rect(screen, RED, rect)
        # 苹果柄
        pygame.draw.rect(screen, DARK_GREEN,
                        (x * GRID_SIZE + GRID_SIZE//2 -2, y * GRID_SIZE - 5, 4, 5))

def draw_grid():
    for x in range(0, WIDTH, GRID_SIZE):
        pygame.draw.line(screen, GRAY, (x, 0),(x, HEIGHT))
    for y in range(0, HEIGHT, GRID_SIZE):
        pygame.draw.line(screen, GRAY, (0, y),(WIDTH, y))

defshow_message(text, color, y_offset=0):
    text_surface = font.render(text, True,color)
    text_rect =text_surface.get_rect(center=(WIDTH//2, HEIGHT//2 + y_offset))
    screen.blit(text_surface, text_rect)

defshow_small_message(text, color, position):
    text_surface = small_font.render(text,True, color)
    screen.blit(text_surface, position)

def main():
    snake = Snake()
    food = Food(snake)
    game_over = False
    paused = False
   
    whileTrue:
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            
            if not game_over:
                if event.type == pygame.KEYDOWN:
                    if event.key ==pygame.K_UP:
                       snake.change_direction((0, -1))
                    elif event.key ==pygame.K_DOWN:
                       snake.change_direction((0, 1))
                    elif event.key ==pygame.K_LEFT:
                       snake.change_direction((-1, 0))
                    elif event.key ==pygame.K_RIGHT:
                       snake.change_direction((1, 0))
                    elif event.key ==pygame.K_SPACE:
                        paused = not paused
            
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r andgame_over:
                    snake.reset()
                    food = Food(snake)
                    game_over = False
                elif event.key ==pygame.K_SPACE and game_over:
                    paused = not paused
        
        # 游戏逻辑
        if not game_over and not paused:
            if not snake.move():
                game_over = True
            
            # 检查是否吃到食物
            if snake.body[0] == food.position:
                snake.grow()
                food = Food(snake)
        
        # 绘制
        screen.fill(BLACK)
        draw_grid()
        
        # 绘制蛇
        for i, segment inenumerate(snake.body):
            x, y = segment
            rect = pygame.Rect(x * GRID_SIZE, y* GRID_SIZE, GRID_SIZE, GRID_SIZE)
            
            # 蛇头用不同颜色
            if i == 0:
                pygame.draw.rect(screen,DARK_GREEN, rect)
                # 蛇头眼睛
                eye_size = GRID_SIZE // 4
                eye_offset = GRID_SIZE // 4
                if snake.direction == (1,0):  # 右
                    pygame.draw.rect(screen,BLACK,
                                    (x * GRID_SIZE + eye_offset*2, y * GRID_SIZE+ eye_offset,
                                     eye_size,eye_size))
                elif snake.direction == (-1,0):  # 左
                    pygame.draw.rect(screen,BLACK,
                                    (x * GRID_SIZE + eye_offset, y *GRID_SIZE + eye_offset,
                                     eye_size,eye_size))
                elif snake.direction == (0,-1):  # 上
                    pygame.draw.rect(screen,BLACK,
                                    (x *GRID_SIZE + eye_offset, y * GRID_SIZE + eye_offset,
                                     eye_size,eye_size))
                elif snake.direction == (0,1):  # 下
                    pygame.draw.rect(screen,BLACK,
                                    (x *GRID_SIZE + eye_offset, y * GRID_SIZE + eye_offset*2,
                                     eye_size,eye_size))
            else:
                pygame.draw.rect(screen, GREEN,rect)
        
        # 绘制食物
        food.draw()
        
        # 显示分数
        show_small_message(f"分数: {snake.score}", WHITE, (10, 10))
        show_small_message(f"速度: {snake.speed}", WHITE, (10, 40))
        
        # 游戏状态显示
        if game_over:
            show_message("游戏结束!", RED)
            show_message(f"最终分数: {snake.score}", WHITE, 50)
            show_message("按R键重新开始", WHITE,100)
            show_message("按空格键暂停/继续", WHITE,150)
        elif paused:
            show_message("已暂停", WHITE)
            show_message("按空格键继续", WHITE, 50)
        
        pygame.display.flip()
        clock.tick(snake.speed)

if __name__ =="__main__":
    main()
游戏功能说明
1.     基本功能
o   使用方向键控制蛇的移动
o   随机生成食物(苹果形状)
o   蛇吃到食物后长度增加,分数增加
o   碰撞检测(墙壁和自身)
2.     游戏机制
o   蛇不能180度转弯(不能直接反向移动)
o   随着蛇长度增加,游戏速度会逐渐加快
o   网格背景辅助玩家判断位置
3.     界面元素
o   实时显示分数和当前速度
o   游戏结束画面
o   暂停功能(空格键)
4.     视觉效果
o   蛇头有眼睛,方向不同眼睛位置不同
o   食物设计成苹果形状
o   网格背景
操作说明
·        方向键:控制蛇的移动方向
·        空格键:暂停/继续游戏
·        R键:游戏结束后重新开始
·        关闭窗口:退出游戏
这个实现完全自包含,不需要任何外部素材文件,所有图形都是通过代码绘制的。


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|广州市技工院校数字化素养研究展示--广州市电子商务技工学校 ( 粤ICP备2025447008号 )

GMT+8, 2025-11-6 01:23 , Processed in 0.216475 second(s), 19 queries .

Powered by Discuz! X5.0

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表