贪吃蛇py按照snake文档的要求,根据所提供的snake.py代码框架,实现功能。最后合并为完整的代码代码
时间: 2024-11-10 15:13:47 浏览: 8
根据提供的 `Snake.docx` 文档,以下是实现贪吃蛇游戏的完整代码。该代码遵循文档中的要求,实现了屏幕设置、颜色配置、PyGame初始化、游戏循环、蛇的移动、食物生成及检测、游戏结束条件以及文本显示等功能。
```python
import pygame
import random
class GameClass:
def __init__(self):
# Grid size
self.Width = 20
self.Height = 20
self.Scale = 30
# Colours
self.BackgroundColour = (40, 40, 60)
self.TextColour = (255, 150, 0)
self.FoodColour = (255, 100, 100)
self.SnakeColour = (255, 255, 255)
# Other
self.FoodPosition = None
self.GameSpeed = 15
self.Score = 0
self.Direction = "Right"
self.GameStarted = False
# Initialise pygame
pygame.init()
self.Display = pygame.display.set_mode((self.Width * self.Scale, self.Height * self.Scale))
self.Clock = pygame.time.Clock()
pygame.display.set_caption('Snake')
# Create a snake instance
self.Snake = Snake(self.Width // 2, self.Height // 2)
def Play(self):
GameOver = False
while not GameOver:
self.DisplayFrame()
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameOver = True
if event.type == pygame.KEYDOWN:
if not self.GameStarted and event.key in [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]:
self.GameStarted = True
if event.key == pygame.K_LEFT and self.Direction != "Right":
self.Direction = "Left"
elif event.key == pygame.K_RIGHT and self.Direction != "Left":
self.Direction = "Right"
elif event.key == pygame.K_UP and self.Direction != "Down":
self.Direction = "Up"
elif event.key == pygame.K_DOWN and self.Direction != "Up":
self.Direction = "Down"
if self.GameStarted:
self.Snake.Move(self.Direction)
if self.Snake.EatenFood(self.FoodPosition):
self.Score += 1
self.Snake.Grow()
self.NewFood()
if self.Snake.OffScreen(self.Width, self.Height) or self.Snake.EatenSelf():
self.GameOver()
pygame.display.update()
self.Clock.tick(self.GameSpeed)
def DisplayFrame(self):
self.Display.fill(self.BackgroundColour)
self.ColourCell(self.Snake.Head, self.SnakeColour)
for cell in self.Snake.Body:
self.ColourCell(cell, self.SnakeColour)
if self.FoodPosition:
self.ColourCell(self.FoodPosition, self.FoodColour)
self.DisplayText(f"Score: {self.Score}", (1, 1))
def ColourCell(self, Position, Colour):
X, Y = Position
X *= self.Scale
Y *= self.Scale
pygame.draw.rect(self.Display, Colour, [X, Y, self.Scale, self.Scale])
def DisplayText(self, text, position):
font = pygame.font.Font(None, 36)
text_surface = font.render(text, True, self.TextColour)
self.Display.blit(text_surface, (position[0] * self.Scale, position[1] * self.Scale))
def NewFood(self):
x = random.randint(0, self.Width - 1)
y = random.randint(0, self.Height - 1)
self.FoodPosition = (x, y)
def GameOver(self):
self.DisplayFrame()
self.DisplayText("Game Over!", (self.Width // 4, self.Height // 2))
self.DisplayText(f"Final Score: {self.Score}", (self.Width // 4, self.Height // 2 + 2))
self.DisplayText("Press any key to restart", (self.Width // 4, self.Height // 2 + 4))
pygame.display.update()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
new_game = GameClass()
new_game.Play()
waiting = False
class Snake:
def __init__(self, start_x, start_y):
self.Head = [start_x, start_y]
self.Body = [[start_x, start_y]]
self.PreviousCell = None
def Move(self, Direction):
Head = self.Head.copy()
if Direction == "Left":
Head[0] -= 1
elif Direction == "Right":
Head[0] += 1
elif Direction == "Up":
Head[1] -= 1
elif Direction == "Down":
Head[1] += 1
self.PreviousCell = self.Body[-1].copy()
self.Body.insert(0, Head)
self.Head = Head
self.Body.pop()
def Grow(self):
self.Body.append(self.PreviousCell)
def EatenFood(self, FoodPosition):
return self.Head == FoodPosition
def OffScreen(self, Width, Height):
return self.Head[0] < 0 or self.Head[0] >= Width or self.Head[1] < 0 or self.Head[1] >= Height
def EatenSelf(self):
return self.Head in self.Body[1:]
if __name__ == "__main__":
game = GameClass()
game.Play()
```
### 说明
1. **初始化**:设置了屏幕大小、颜色等基本参数,并初始化PyGame。
2. **游戏循环**:处理事件、更新蛇的状态、检查碰撞和食物的生成。
3. **显示帧**:绘制背景、蛇、食物和文本。
4. **蛇类**:实现了蛇的移动、生长、检测食物和自身碰撞等功能。
5. **游戏结束**:显示最终分数并等待玩家重启游戏。
希望这段代码能满足你的需求。如果有任何问题或需要进一步的帮助,请随时告诉我。
阅读全文