Pygame基础教程(三)-无聊小游戏贪吃蛇

贪吃蛇初级版代码

import pygame,sys
from pygame.locals import *
import random
import copy
FPS = 10 #刷帧率
fpsClock = pygame.time.Clock() #确保程序以一个最大的FPS运行
SIZE = (400,400)
class snake:#蛇体结构
    def __init__(self,x,y):
        self.x = x
        self.y = y

def init(displaysur,snakes):
    x,y,x1,y1 = 0,0,0,0
    x = random.randint(50, SIZE[0] - 20)
    y = random.randint(50, SIZE[0] - 20)
    s = snake(x,y)
    snakes.append(s)
    f = True
    while f:
        x1 = random.randint(20, SIZE[0] - 20)
        y1 = random.randint(20, SIZE[0] - 20)
        if x1 != x or y1 != y:
            f = False
    pygame.draw.rect(displaysur,(255,0,0),(x,y,5,5))
    return x1,y1
def faileCheck(snakes):#撞墙检测
    headSnake = snakes[0]
    if headSnake.x < 0 or headSnake.x > 399 or headSnake.y < 0 or headSnake.y > 399 :
        return True
def eatCheck(displaysur,snakes,fx,fy):
    flag = False
    x = fx
    y = fy
    headSnake = snakes[0]
    h_rect = pygame.Rect( headSnake.x, headSnake.y,5,5)
    f_rect = pygame.Rect(fx,fy,5,5)
    if h_rect.colliderect(f_rect):#碰撞检测
        flag = True
    if flag:#生成新的食物位置
        x = random.randint(0, SIZE[0] - 1)
        y = random.randint(0, SIZE[0] - 1)
    return flag ,x , y


def main():
    global DisplaySurface, snakes, foodx, foody
    snakes = []
    direction = 'right'
    pygame.init()
    DisplaySurface = pygame.display.set_mode(SIZE)
    pygame.display.set_caption("贪吃蛇")
    foodx,foody = init(DisplaySurface,snakes)#游戏初始化

    while True:
        DisplaySurface.fill((255,255,255))
        if faileCheck(snakes):
            pygame.quit()
            sys.exit()
        F, foodx, foody= eatCheck(DisplaySurface,snakes,foodx,foody) #是否吃到食物,并返回食物坐标
        pygame.draw.rect(DisplaySurface, (0, 0, 128), (foodx, foody, 5, 5)) #绘制食物
        if F:
            sna = snake(0,0)
            snakes.append(sna)

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:#控制方向
                if event.key == K_LEFT and direction != 'right':
                    direction = 'left'
                if event.key == K_RIGHT and direction != 'left':
                    direction = 'right'
                if event.key == K_UP and direction != 'down':
                    direction = 'up'
                if event.key == K_DOWN and direction != 'up':
                    direction = 'down'
        snakes_copy = copy.deepcopy(snakes)
        for i in range(len(snakes)):
            posx , posy = snakes[i].x,snakes[i].y
            x ,y = posx,posy
            if i == 0:
                if direction == 'right':
                    posx += 5
                elif direction == 'left':
                    posx -= 5
                elif direction == 'up':
                    posy -= 5
                elif direction == 'down':
                    posy += 5
            else:
                posx,posy = snakes_copy[i-1].x,snakes_copy[i-1].y
            snakes[i].x , snakes[i].y = posx,posy #更新位置
            pygame.draw.rect(DisplaySurface,(255,0,0),(posx,posy,5,5))
        pygame.display.update()
        fpsClock.tick(FPS)
main()

 

主要内容:刷帧率

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUISUIZHIBO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值