Python练习:贪吃蛇

b站贪吃蛇视频

P01

import sys # 退出用此模块
import pygame
import os # 操作系统模块

os.environ["SDL_VIDEO_CENTERED"] = "1" # 使得游戏窗口居中

pygame.init()   # 初始化模块。返回值为一个元组,(x, y),其中x是成功载入的模块数,y为失败载入的模块数
pygame.display.set_caption("pysnake") # 设置标题

game_clock = pygame.time.Clock() # 创建一个时钟对象来确定游戏运行的帧数
game_speed = 60 # 游戏帧数,帧数太小会卡顿,过高浪费CPU

game_screen_width, game_screen_height = 640, 480 # 窗口尺寸参数
game_screen = pygame.display.set_mode((game_screen_width, game_screen_height)) # 初始化窗口,返回一个surface对象
game_bgcolor = 33, 66, 33 # 窗口颜色参数

# 定义一个正方形的参数:颜色,坐标,尺寸
square_color = 33, 255, 33
square_x, square_y = 0, 0
square_size = 20

game_playing = True # 标记游戏是否进行
while game_playing:
    # 用户控制
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # pygame.QUIT的实际值为数值12
            game_playing = False
    # 更新数据

    # 更新画面
    game_screen.fill(game_bgcolor) # 游戏窗口填充指定颜色
    pygame.draw.rect(game_screen, square_color, (square_x, square_y,
                                                 square_size, square_size)) # 在game_screen上画一个正方形

    pygame.display.flip() # 更新游戏画面
    game_clock.tick(game_speed)

pygame.quit() # 退出模块
sys.exit(0)

P02

import sys # 退出用此模块
import pygame
import os # 操作系统模块

os.environ["SDL_VIDEO_CENTERED"] = "1" # 使得游戏窗口居中

pygame.init()   # 初始化模块。返回值为一个元组,(x, y),其中x是成功载入的模块数,y为失败载入的模块数
pygame.display.set_caption("pysnake") # 设置标题

game_clock = pygame.time.Clock() # 创建一个时钟对象来确定游戏运行的帧数
game_speed = 60 # 游戏帧数,帧数太小会卡顿,过高浪费CPU

game_screen_width, game_screen_height = 640, 480 # 窗口尺寸参数
game_screen = pygame.display.set_mode((game_screen_width, game_screen_height)) # 初始化窗口,返回一个surface对象
game_bgcolor = 33, 66, 33 # 窗口颜色参数

# 定义一个正方形的参数:颜色,坐标,尺寸,速率值,x和y方向上的初始速率
square_color = 33, 255, 33
square_x, square_y = 0, 0
square_size = 20
square_speed = 2
square_speed_x, square_speed_y = square_speed, 0 # 初始默认方块水平向右移动

game_playing = True # 标记游戏是否进行
while game_playing:
    # 用户控制,# 可通过在for循环内print(event)查看每个事件对象的具体内容
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # pygame.QUIT的实际值为数值12,pygame对每种事件进行了编号
            game_playing = False
        # 若事件对象的属性 类型type 为 键盘摁下,则进一步看事件对象的属性 值key
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: # 若摁下的为K_UP键
                square_speed_x = 0 # x方向速率置0
                square_speed_y = -square_speed # y方向速率置-square_speed
            elif event.key == pygame.K_DOWN:
                square_speed_x = 0
                square_speed_y = square_speed
            elif event.key == pygame.K_LEFT:
                square_speed_x = -square_speed
                square_speed_y = 0
            elif event.key == pygame.K_RIGHT:
                square_speed_x = square_speed
                square_speed_y = 0
    # 更新数据
    square_x += square_speed_x # 更新方块坐标位置
    square_y += square_speed_y
    if square_x < 0: # 控制方块位置在游戏窗口内
        square_x =0
    elif square_x > game_screen_width - square_size:
        square_x = game_screen_width - square_size
    if square_y < 0:
        square_y =0
    elif square_y > game_screen_height - square_size:
        square_y = game_screen_height - square_size


    # 更新画面
    game_screen.fill(game_bgcolor) # 游戏窗口填充指定颜色
    pygame.draw.rect(game_screen, square_color, (square_x, square_y,
                                                 square_size, square_size)) # 在game_screen上画一个正方形

    pygame.display.flip() # 更新游戏画面
    game_clock.tick(game_speed)

pygame.quit() # 退出模块
sys.exit(0)

P03

import sys # 退出用此模块
import pygame
import os # 操作系统模块

os.environ["SDL_VIDEO_CENTERED"] = "1" # 使得游戏窗口居中

pygame.init()   # 初始化模块。返回值为一个元组,(x, y),其中x是成功载入的模块数,y为失败载入的模块数
pygame.display.set_caption("pysnake") # 设置标题

game_clock = pygame.time.Clock() # 创建一个时钟对象来确定游戏运行的帧数
game_speed = 120 # 游戏帧数,帧数太小会卡顿,过高浪费CPU

game_screen_width, game_screen_height = 640, 480 # 窗口尺寸参数
game_screen = pygame.display.set_mode((game_screen_width, game_screen_height)) # 初始化窗口,返回一个surface对象
game_bgcolor = 0, 0, 0 # 窗口颜色参数
game_linecolor = 33, 33, 33

# 定义一个正方形的参数
square_color = 33, 255, 33 # 两种颜色装饰方块,增加趣味性
square_color2 = 33, 192, 33

CELL_SIZE = 20 # 方块尺寸
square_rect = pygame.Rect(0, 0, CELL_SIZE, CELL_SIZE) # 方块位置和大小
UP, DOWN, LEFT, RIGHT = (0, -1), (0, 1), (-1, 0), (1, 0) # 定义元组常量表征方块移动的具体方向和速率
square_direction = RIGHT # 定义初始速度(表观速度),只有方块在网格内才转向

square_turn = RIGHT # 定义初始速度(真实速度),方向键一敲击立刻转向


game_running = True # 标记游戏是否进行
while game_running:
    # 用户控制,# 可通过在for循环内print(event)查看每个事件对象的具体内容
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # pygame.QUIT的实际值为数值12,pygame对每种事件进行了编号
            game_running = False
        # 若事件对象的属性 类型type 为 键盘摁下,则进一步看事件对象的属性 值key
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: # 若摁下的为K_UP键
                square_turn = UP # 速度改变
            elif event.key == pygame.K_DOWN:
                square_turn = DOWN
            elif event.key == pygame.K_LEFT:
                square_turn = LEFT
            elif event.key == pygame.K_RIGHT:
                square_turn = RIGHT
    # 更新数据
    if square_rect.x % CELL_SIZE == 0 and square_rect.y % CELL_SIZE == 0: # 只有方块在格子内,表观速度才被赋值改变
        square_direction = square_turn
    square_rect = square_rect.move(square_direction)
    if square_rect.left < 0: # 控制方块位置在游戏窗口内
        square_rect.left = 0
    elif square_rect.right > game_screen_width:
        square_rect.right = game_screen_width
    if square_rect.top < 0:
        square_rect.top = 0
    elif square_rect.bottom > game_screen_height:
        square_rect.bottom = game_screen_height
    print("坐标:(%d, %d) 速度:(%d, %d)" % (square_rect.x, square_rect.y,
                                       square_direction[0], square_direction[1]) )

    # 更新画面
    game_screen.fill(game_bgcolor) # 游戏窗口填充指定颜色
    for i in range(CELL_SIZE, game_screen_width, CELL_SIZE): # 游戏窗口内画纵向线
        pygame.draw.line(game_screen, game_linecolor, (i, 0), (i, game_screen_height))
    for i in range(CELL_SIZE, game_screen_height, CELL_SIZE): # 游戏窗口内画纵向线
        pygame.draw.line(game_screen, game_linecolor, (0, i), (game_screen_width, i))
    # pygame.draw.rect(game_screen, square_color, square_rect) # 在game_screen上画一个正方形
    game_screen.fill(square_color, square_rect) # 在game_screen上画一个方块
    game_screen.fill(square_color2, square_rect.inflate(-4, -4)) # 在方块内再画一个更小的块,比外面的边长小4
    pygame.display.flip() # 更新游戏画面
    game_clock.tick(game_speed)

pygame.quit() # 退出模块
sys.exit(0)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值