| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- #贪吃蛇.py
 
 - import pygame
 
 - import sys
 
 - from pygame.locals import *
 
 - import 金币
 
 - import 蛇身
 
 - pygame.init()
 
 - pygame.mixer.init()
 
 - size = width, height = 600, 400
 
 - bg = (0, 0, 255)
 
 - YELLOW = (255, 255, 0)
 
 - snake = (0, 255, 0)
 
 - white = (255, 255, 255)
 
 - Sbody = {}
 
 - fanxiang = 'right'
 
 - left = 300
 
 - top = 200
 
 - pygame.mouse.set_visible(False)
 
 - #pygame.mixer.music.load('Ximalaya-4.0.0_99B_sc100006.exe')
 
 - pygame.mixer.music.load('game_music.ogg')
 
 - pygame.mixer.music.set_volume(1)
 
 - a = pygame.mixer.Sound('upgrade.wav')
 
 - screen = pygame.display.set_mode(size)
 
 - pygame.display.set_caption('贪吃蛇')
 
 - move = USEREVENT
 
 - pygame.time.set_timer(move, 1 * 100)
 
 - money = USEREVENT + 1
 
 - pygame.time.set_timer(money, 5 * 1000)
 
 - score = 0 
 
 - score_font = pygame.font.Font(None, 20)
 
 - GameOver_font = pygame.font.Font(None, 100)
 
 - Money = pygame.sprite.Group()
 
 - snakebody = pygame.sprite.Group()
 
 - pygame.mixer.music.play()
 
 - running = True
 
 - clock = pygame.time.Clock()
 
  
 
 
- while running:    
 
 -     for event in pygame.event.get():
 
 -         if event.type == pygame.QUIT:
 
 -             sys.exit()
 
 -         elif event.type == move:
 
 -             if fanxiang == 'left':
 
 -                 Sbody[0] = [left, top]
 
 -                 left -= 11
 
 -                 for b in snakebody:
 
 -                     Sbody[int(b.number)] = [b.width, b.height]
 
 -                 
 
 -                 
 
 -             elif fanxiang == 'top':
 
 -                 Sbody[0] = [left, top]
 
 -                 top -= 11
 
 -                 for b in snakebody:
 
 -                     Sbody[int(b.number)] = [b.width, b.height]
 
 -                 
 
 -                 
 
 -             elif fanxiang == 'right':
 
 -                 Sbody[0] = [left, top]
 
 -                 left += 11
 
 -                 for b in snakebody:
 
 -                     Sbody[int(b.number)] = [b.width, b.height]
 
 -                 
 
 -                 
 
 -             elif fanxiang == 'bottom':
 
 -                 Sbody[0] = [left, top]
 
 -                 top += 11
 
 -                 for b in snakebody:
 
 -                     Sbody[int(b.number)] = [b.width, b.height]
 
 -                 
 
 -                 
 
 -         
 
 -         #生成金币
 
 -         elif event.type == money:
 
 -             print(left, top)
 
 -             m = 金币.money()
 
 -             Money.add(m)
 
 -     
 
 -     key_pressed = pygame.key.get_pressed()
 
 -     
 
 -     #控制方向
 
 -     if key_pressed[K_w] or key_pressed[K_UP]:
 
 -         fanxiang = 'top'
 
 -     elif key_pressed[K_s] or key_pressed[K_DOWN]:
 
 -         fanxiang = 'bottom'
 
 -     elif key_pressed[K_d] or key_pressed[K_RIGHT]:
 
 -         fanxiang = 'right'
 
 -     elif key_pressed[K_a] or key_pressed[K_LEFT]:
 
 -         fanxiang = 'left'
 
  
-     if left <= 0 or left >= 600 or top <= 0 or top >= 400:
 
 -         pygame.mixer.music.stop()
 
 -         #raise OSError('不能碰到身体或边缘!!!')
 
 -         running = False
 
 -         GameOver = True
 
 -         
 
  
-     screen.fill(bg)
 
 -     #蛇身机制
 
 -     for b in snakebody:
 
 -         lastnumber = b.number - 1
 
 -         nextnumber = b.number + 1
 
 -         
 
 -         #pygame.time.delay(1)
 
 -         #print(Sbody)
 
 -         b.width = Sbody[lastnumber][0]
 
 -         b.height = Sbody[lastnumber][1]
 
 -         b.rect = pygame.Rect(b.width, b.height, 10, 10)
 
 -         pygame.draw.rect(screen, snake, b.rect, 0)
 
 -         if b.rect.colliderect(pygame.Rect(left, top, 10, 10)):
 
 -             pygame.mixer.music.stop()
 
 -             #raise OSError('不能碰到身体或边缘!!!')
 
 -             running = False
 
 -             GameOver = True
 
 -             
 
 -         
 
 -     #金币机制
 
 -     for i in Money:
 
 -         pygame.draw.rect(screen, YELLOW, i.rect, 0)
 
 -         if i.rect.colliderect(pygame.Rect(left, top, 10, 10)):
 
 -             score+=1000
 
 -             thousand = score / 1000
 
 -             last = thousand - 1
 
 -             #增加长度
 
 -             s = 蛇身.body(int(Sbody[last][0]), int(Sbody[last][1]), thousand, size)
 
 -             snakebody.add(s)
 
 -             Money.remove(i)
 
 -             a.play()
 
 -     #绘制贪吃蛇
 
 -     pygame.draw.rect(screen, snake, (left, top, 10, 10), 0)
 
 -        #显示字迹
 
 -     score_text = score_font.render(f'Score:{score}', True, white)
 
 -     screen.blit(score_text, (10, 5))
 
 -     pygame.display.flip()
 
  
- while GameOver:
 
 -     #print('已经准备就绪!')
 
 -     GameOver_text = GameOver_font.render('Game Over', True, white)
 
 -     screen.blit(GameOver_text, (100, 100))
 
 -     pygame.time.delay(1000)
 
 -     clock.tick(1000)
 
  复制代码 
- #蛇身.py
 
 - import pygame
 
 - class body(pygame.sprite.Sprite):
 
 -     def __init__(self, width, height, number, bg_size):
 
 -         pygame.sprite.Sprite.__init__(self)
 
 -         self.width = width
 
 -         self.height = height
 
 -         self.rect = pygame.Rect(self.width, self.height, 10, 10)
 
 -         self.number = number
 
 -         self.left = bg_size[0]
 
 -         self.top = bg_size[1]
 
 
  复制代码- #金币.py
 
 - import pygame
 
 - import random
 
 - class money(pygame.sprite.Sprite):
 
 -     def __init__(self):
 
 -         pygame.sprite.Sprite.__init__(self)
 
 -         self.rect = pygame.Rect(random.randint(0, 590), random.randint(0, 390), 10, 10)
 
  复制代码 
不知道为什么游戏失败时突然卡住了,求各位帮忙修复,谢谢 |   
 
 
 
 |