pygame双人贪吃蛇(入门级代码)

本文详细介绍了如何使用Python的pygame库开发一个基本的贪吃蛇游戏,包括屏幕初始化、事件处理、蛇的移动、食物生成和碰撞检测等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import pygame,random,tkinter,sys
pygame.init()
pygame.display.set_caption('贪吃蛇')
screen_width,screen_height=640,640
screen=pygame.display.set_mode((screen_width,screen_height))
game_clock,game_speed=pygame.time.Clock(),5#时钟对象与游戏运行速度
square_green,square_red,food_color,game_color_line=(0,255,0),(213,50,80),(255,255,102),(128,138,135)#颜色

square_size,square_body=20,[pygame.Rect(0,0,0,0)]*3#尺寸、蛇的身体
square_size1,square_body1=20,[pygame.Rect(0,0,0,0)]*3

square_speed,square_become=20,0#蛇的移动速度与关于速度变化的变量

square_speed_x,square_speed_y,square_x,square_y=square_speed,0,0,0#蛇的初始移动方向与位置
square_speed_x1,square_speed_y1,square_x1,square_y1=square_speed,0,0,screen_height-20

square_rect=pygame.draw.rect(screen,square_green,(square_x,square_y,square_size,square_size))#绘制蛇头
square_rect1=pygame.draw.rect(screen,square_green,(square_x,square_y,square_size,square_size))

food_x,food_y,food_eat=random.randrange(0,screen_width,square_size),random.randrange(0,screen_height,square_size),0#食物初始坐标与吃掉的食物个数
game_field=screen.get_rect()
game_runing,game_playing=True,True#游戏运行状态
while game_runing:                                                                                                                                    
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
        elif event.type==pygame.KEYDOWN:
             if square_speed_y==0:
                 if event.key==pygame.K_w and square_x%20==0:
                     square_speed_x=0
                     square_speed_y=-square_speed
                 elif event.key==pygame.K_s and square_x%20==0:
                     square_speed_x,square_speed_y=0,square_speed
             elif square_speed_x==0:
                 if event.key==pygame.K_a and square_y%20==0:
                     square_speed_x,square_speed_y=-square_speed,0
                 elif event.key==pygame.K_d and square_y%20==0:
                     square_speed_x,square_speed_y=square_speed,0
             if square_speed_y1==0:
                 if event.key==pygame.K_i and square_x1%20==0:
                     square_speed_x1=0
                     square_speed_y1=-square_speed
                 elif event.key==pygame.K_k and square_x1%20==0:
                     square_speed_x1,square_speed_y1=0,square_speed
             elif square_speed_x1==0:
                 if event.key==pygame.K_j and square_y%20==0:
                     square_speed_x1,square_speed_y1=-square_speed,0
                 elif event.key==pygame.K_l and square_y%20==0:
                     square_speed_x1,square_speed_y1=square_speed,0

    square_x+=square_speed_x
    square_y+=square_speed_y
    square_x1+=square_speed_x1
    square_y1+=square_speed_y1

    square_body.insert(0,square_rect)
    square_body.pop()
    square_body1.insert(0,square_rect1)
    square_body1.pop()

    screen.fill((0,0,0))
    for i in range(square_size,screen_width,square_size):
            pygame.draw.line(screen,game_color_line,(i,0),(i,screen_height))
            pygame.draw.line(screen,game_color_line,(0,i),(screen_width,i))

    square_rect=pygame.draw.rect(screen,square_red,(square_x,square_y,square_size,square_size))
    square_rect_png=(screen,square_red,(square_x,square_y,20,20))
    square_rect1=pygame.draw.rect(screen,square_red,(square_x1,square_y1,square_size,square_size))
    square_rect_png1=(screen,square_red,(square_x,square_y,20,20))

    for cell in square_body:
        screen.fill(square_green,cell)
    for cell in square_body1:
        screen.fill(square_green,cell)
        
    food=pygame.draw.rect(screen,food_color,(food_x,food_y,square_size,square_size))

    if square_rect==food:
        food_x,food_y=random.randrange(0,screen_width,square_size),random.randrange(0,screen_height,square_size)
        food=pygame.draw.rect(screen,food_color,(food_x,food_y,square_size,square_size))
        square_body.append(pygame.Rect(0,0,0,0))
        food_eat+=1
        if len(square_body)%5==0:
            game_speed+=1
            square_become=len(square_body)
    if square_rect1==food:
        food_x,food_y=random.randrange(0,screen_width,square_size),random.randrange(0,screen_height,square_size)
        food=pygame.draw.rect(screen,food_color,(food_x,food_y,square_size,square_size))
        square_body1.append(pygame.Rect(0,0,0,0))
        food_eat+=1
        if len(square_body1)%5==0:
            game_speed+=1
            square_become=len(square_body)

    if game_playing:
        if not game_field.contains((square_x,square_y,square_size,square_size)):
            game_playing=False
        for cell in square_body:
            if square_rect==cell:
                game_playing=False
        if not game_field.contains((square_x1,square_y1,square_size,square_size)):
            game_playing=False
        for cell in square_body1:
            if square_rect1==cell:
                game_playing=False               
        if square_rect in square_body or square_rect in square_body1:
            game_playing=False
        if square_rect1 in square_body1 or square_rect1 in square_body:
            game_playing=False
    if not game_playing:
        pygame.quit()
    game_clock.tick(game_speed)
    pygame.display.flip()
    pygame.init()
        

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值