当前位置:   article > 正文

Python实现贪吃蛇小游戏(附源码和资源)_python小游戏下载

python小游戏下载

这个是三年前刚学完Python时写的第一个小游戏,当时为了练手。
用的pygame库。
代码有点多,就不放在这了。到时候整个项目上传,需要的话可以去下载。
下载地址:https://download.csdn.net/download/qq_44651842/20010244
在这里插入图片描述
在这里插入图片描述

部分代码

控制.py

import sys
import pygame
from 身子 import Body
import 检测 as ch

# 响应按键函数
def check_events(snake):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key ==pygame.K_RIGHT and snake.left == False:
                snake.left,snake.right,snake.up,snake.down = (False,True,False,False)
            elif event.key ==pygame.K_LEFT and snake.right == False:
                snake.left, snake.right, snake.up, snake.down = (True, False, False, False)
            elif event.key ==pygame.K_UP and snake.down == False:
                snake.left, snake.right, snake.up, snake.down = (False, False, True, False)
            elif event.key ==pygame.K_DOWN and snake.up == False:
                snake.left, snake.right, snake.up, snake.down = (False, False, False, True)
# 屏幕更新函数
def update_screen(ai_set, screen, snake, tail, apple, bodys, obstale):
    screen.fill(ai_set.bg_color)
    obstale.draw()
    snake.blitme()
    apple.blitme()
    for body in bodys:
        body.blitme()
    tail.blitme()
    pygame.display.flip()

# 更新蛇头数据
def update_snake(snake):
    snake.update()

# 更新蛇尾数据
def update_tail(tail,snake):
    tail.rect.centerx = snake.rect.centerx
    tail.rect.centery = snake.rect.centery

# 更新全部数据函数
def update(snake, apple, tail, screen, bodys, obstale):
    if ch.eatapple(snake, apple):
        add_body(tail, screen, bodys)
        apple.update( bodys, obstale)
    if len(bodys) == 0:
        update_tail(tail, snake)
    update_snake(snake)
    if len(bodys) != 0:
        update_bodys(bodys, snake, tail)

# 吃到苹果后长一节身体函数
def add_body(tail,screen,bodys):
    new_body = Body(screen)
    new_body.rect.centerx = tail.rect.centerx
    new_body.rect.centery = tail.rect.centery
    bodys.add(new_body)

# 更新所有身子函数
def update_bodys(bodys,snake,tail):
    # 通过蛇头的移动,确定第一个身子的更新数据
    if snake.up:
        center_x = snake.rect.centerx
        center_y = snake.rect.centery + snake.rect.width
    elif snake.down:
        center_x = snake.rect.centerx
        center_y = snake.rect.centery - snake.rect.width
    elif snake.right:
        center_x = snake.rect.centerx - snake.rect.width
        center_y = snake.rect.centery
    elif snake.left:
        center_x = snake.rect.centerx + snake.rect.width
        center_y = snake.rect.centery
    else:
        center_x = snake.rect.centerx
        center_y = snake.rect.centery
    for body in bodys:
        x = body.rect.centerx
        y = body.rect.centery
        body.rect.centerx = center_x
        body.rect.centery = center_y
        center_x = x
        center_y = y
        body.blitme()
    tail.rect.centerx = center_x
    tail.rect.centery = center_y
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

检测.py

import sys
import pygame
def endgame(snake, bodys, tail,obstale):
    if  snake.check() or zhuangsi(obstale,snake):
        print("撞死了!!")
        sys.exit()
    eatself(snake, bodys, tail)
def eatapple(snake, apple):
    if pygame.Rect.colliderect(snake.rect,apple.rect):
        return True
    else:
        return False
def eatself(snake, bodys, tail):
    for body in bodys:
        if pygame.Rect.colliderect(snake.rect, body.rect) or pygame.Rect.colliderect(snake.rect,tail.rect) :
            print("咬死了自己!!")
            sys.exit()
def zhuangsi(obstale,snake):
    if pygame.Rect.colliderect(obstale.rect1, snake.rect) or\
        pygame.Rect.colliderect(obstale.rect2, snake.rect) or\
        pygame.Rect.colliderect(obstale.rect3, snake.rect) or\
        pygame.Rect.colliderect(obstale.rect4, snake.rect):
        print("撞到障碍物", end = "")
        return True
    else:
        return False
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/80992
推荐阅读
相关标签
  

闽ICP备14008679号