python pygame 贪吃蛇

  • h3_393366
    了解作者
  • 122.8MB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-21 08:15
    上传日期
自己写的贪吃蛇,解压后直接运行贪吃.py即可(q退出,p暂停)(没有安装pygame可以运行exe文件)
贪吃蛇.7z
  • fruit.py
    347B
  • snake.py
    2.2KB
  • 贪吃蛇.exe
    124MB
  • game_functions.py
    4.1KB
  • 贪吃.py
    643B
内容介绍
import sys import pygame import random from pygame.sprite import Group from snake import Snake, Body from fruit import Fruit from time import sleep def update_screen(screen,snake,fruits,body,bodys): screen.fill((230,230,230)) snake.draw_snake() #snake.blitme() update_fruit(screen,fruits, snake, bodys) for body in bodys: snake_go(body) body.draw_body() # body.blitme() check_body(bodys, snake) for fruit in fruits: fruit.draw_fruit() head_body(snake, bodys) check_bdry(snake, bodys) check_sleep(snake) pygame.display.flip() pass def check_events(snake): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: # if snake.change == 1: # snake.y = snake.y-snake.y%10 # elif snake.change == 2: # snake.x = 10-snake.x%10+snake.x # elif snake.change == 3: # snake.y = 10-snake.y%10+snake.y # elif snake.change == 4: # snake.x = snake.x-snake.x%10 if event.key == pygame.K_UP and snake.change != 3: snake.change = 1 elif event.key == pygame.K_RIGHT and snake.change != 4: snake.change = 2 elif event.key == pygame.K_DOWN and snake.change != 1: snake.change = 3 elif event.key == pygame.K_LEFT and snake.change != 2: snake.change = 4 elif event.key == pygame.K_q: sys.exit() elif event.key == pygame.K_p: snake.sleep = 1 snake.foot.append((snake.rect.centerx,snake.rect.centery,snake.change)) snake_go(snake) def snake_go(snake): if snake.change == 1: snake.y -= 1#0 snake.rect.centery = snake.y elif snake.change == 2: snake.x += 1#0 snake.rect.centerx = snake.x elif snake.change == 3: snake.y += 1#0 snake.rect.centery = snake.y elif snake.change == 4: snake.x -= 1#0 snake.rect.centerx = snake.x pass def create_fruit(screen,fruits,snake): new_fruit = Fruit(screen) if snake.score > 170: new_fruit.color = (255,0,0) snake.speed_factor = 15 elif snake.score > 140: new_fruit.color = (255,100,0) snake.speed_factor = 10 elif snake.score > 110: new_fruit.color = (255,200,0) snake.speed_factor = 8 elif snake.score > 90: new_fruit.color = (255,255,0) snake.speed_factor = 5 elif snake.score > 60: new_fruit.color = (200,255,0) snake.speed_factor = 4 elif snake.score > 30: new_fruit.color = (100,255,0) snake.speed_factor = 2 new_fruit.rect.centerx = random.randint(5,695)#//10*10 new_fruit.rect.centery = random.randint(5,395)#//10*10 fruits.add(new_fruit) def update_fruit(screen,fruits,snake,bodys): boom = pygame.sprite.spritecollideany(snake,fruits) if boom: fruits.empty() snake.score += 1 create_fruit(screen,fruits,snake) num = 0 for bd in bodys: num += 1 # print("you got",snake.length) if num == snake.length : body = Body(screen) body.create(bd) snake.length += 1 bodys.add(body) # print(snake.length) break def check_body(bodys,snake): num = 0 for bd in bodys: num += 1 for i in snake.foot: if (bd.rect.centerx == i[0]) and (bd.rect.centery == i[1]): bd.change = i[2] if num == snake.length : if snake.foot: snake.foot.pop(0) pass def head_body(snake,bodys): check_bodys = Group() num = 0 for body in bodys: num += 1 if num == 1: continue check_bodys.add(body) boom = pygame.sprite.spritecollideany(snake,check_bodys) if boom: print("Game Over!") print("Your Score:",snake.score) sys.exit() def check_bdry(snake,bodys): if snake.x > 700: snake.x = 0 print(snake.x) elif snake.x < 0: snake.x = 700 snake.rect.centerx = snake.x if snake.y > 400: snake.y = 0 elif snake.y < 0: snake.y = 400 snake.rect.centery = snake.y for body in bodys: if body.x > 700: body.x = 0 elif body.x < 0: body.x = 700 body.rect.centerx = body.x if body.y > 400: body.y = 0 elif body.y < 0: body.y = 400 body.rect.centery = body.y def check_sleep(snake): while snake.sleep == 1: sleep(1) for event in pygame.event.get(): if event.key == pygame.K_p: snake.sleep = 0 break pass
评论
    相关推荐