贪吃蛇004(绘制贪吃蛇和食物)

import random
import sys

import pygame

#窗口变量
windows_width = 800
windows_height = 480
cell_size = 20 #方块大小
map_width=windows_width//cell_size
map_hight = windows_height//cell_size

#设置颜色变量
white = (255,255,255)
red = (255,0,0)
red2=(201,23,12)
blue = (0,0,255)
blue2 = (4,23,120)
dark_gray = (100,100,100)
green = (0, 255, 0)

#初始化pygame(退出游戏时记得使用pygame.quit())
pygame.init()
screen = pygame.display.set_mode((windows_width,windows_height))
screen.fill(blue2)
pygame.display.set_caption("PYTHON 贪吃蛇98")

#开始界面
gamestart = pygame.image.load('gamestart.png')
gamestart = pygame.transform.scale(gamestart,(windows_width,windows_height)) # transform.scale缩放图片
screen.blit(gamestart,(0,0))  #blit 第一个参数:要显示的元素,第二个参数:座标

font = pygame.font.Font('myfont.ttf',60)
tip = font.render("贪吃蛇",True,red) #渲染
screen.blit(tip,(300,30))

font = pygame.font.Font('my_pygame/resources/font/myfont.ttf',40)
tip = font.render("按任意键开始游戏(按ESC退出游戏)",True,blue) #设定需要显示的文字,True代表打开抗锯齿(字体显示平滑),blue颜色
screen.blit(tip, (100, 300))

#修改屏幕对象后,记得更新操作
pygame.display.update()

#播放背景音乐,set_volume控制音量大小
pygame.mixer.music.stop()
pygame.mixer.music.load("bensound-endlessmotion.wav")
pygame.mixer.music.set_volume(0.9)
pygame.mixer.music.play(-1)

# 使用变量i,巧妙的控制while循环
i=1
while i>0:
    for event in pygame.event.get():
        if event.type == pygame.constants.QUIT:
            print("按关闭键退出")
            pygame.quit()
            sys.exit()
        elif event.type == pygame.constants.KEYDOWN:
            if event.key == pygame.constants.K_ESCAPE:
                print("按ESC退出")
                pygame.quit()
                sys.exit()
            else:
                i=0  # 这里不能使用break,思考一下为什么呢?

#摁任意键进入游戏页面
while True:
    screen.fill(white)

    # 画网格线  # draw.line五个参数,surface,颜色,起始点,终止点,线宽(默认为1)
    for x in range(0, windows_width, cell_size):  # 垂直线,每隔cell_size(20)划一条垂直线
        pygame.draw.line(screen, blue2, (x, 0), (x, windows_height))
    for y in range(0, windows_height, cell_size):  # 水平线,每隔cell_size(20)划一条水平线
        pygame.draw.line(screen, blue2, (0, y), (windows_width, y))

    # # 设置贪吃蛇的位置
    # # 初始贪吃蛇,默认5节蛇身
    startx = starty = 20
    snake_coords = [{'x': startx, 'y': starty},
                    {'x': startx - 1, 'y': starty},
                    {'x': startx - 2, 'y': starty},
                    {'x': startx - 3, 'y': starty},
                    {'x': startx - 4, 'y': starty}]

    # 画蛇
    for coord in snake_coords:  # 变量列表,得到三节蛇身的座标
        x = coord['x'] * cell_size
        y = coord['y'] * cell_size
        oneRect = pygame.Rect(x, y, cell_size, cell_size)
        # pygame.Rect四个参数,x,y座标,宽,高
        pygame.draw.rect(screen, red2, oneRect)
        # draw.rect四个参数Surface,颜色,上面的Rect(x,y,宽,高), 线条宽度(默认0)
        oneInnerRect = pygame.Rect(x + 2, y + 2, cell_size - 4, cell_size - 4)
        # 为了美观,内部再画一个方块
        pygame.draw.rect(screen, blue, oneInnerRect)

    #画食物
    food = {"x": 10, "y": 9}
    x = food['x'] * cell_size
    y = food['y'] * cell_size
    foodRect = pygame.Rect(x, y, cell_size, cell_size)
    pygame.draw.rect(screen, dark_gray, foodRect)
    foodInnerRect = pygame.Rect(x + 3, y + 3, cell_size - 6, cell_size - 6)
    pygame.draw.rect(screen, green, foodInnerRect)

    pygame.display.update()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章