贪吃蛇小游戏(使用python的pygame库实现)
import pygame
import random
import time
from collections import deque
FPS = 5
SCREENWIDTH = 600
SCREENHEIGHT = 600
ZOOM = 20
DIRECTION = {"left": 1, "right": 2, "up": 3, "down": 4, "none":0}
class GSgame:
def __init__(self):
self.CLOCKFPS = pygame.time.Clock()
self.SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("Greedy Snack")
self.score = 0
self.head_x = SCREENWIDTH / 2 / ZOOM
self.head_y = SCREENHEIGHT / 2 / ZOOM
self.snackVal = 1
self.snackDirection = DIRECTION["right"]
self.body = deque()
self.body.append([self.head_x, self.head_y])
self.SnackHeadImage = pygame.Surface((ZOOM, ZOOM))
self.SnackHeadImage.fill((255, 0, 0))
self.SnackHeadRect = pygame.Rect(self.head_x*ZOOM,self.head_y*ZOOM, ZOOM, ZOOM)
self.preApple = self.RandomApple()
pygame.event.set_blocked(None)
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN, pygame.QUIT))
def start(self):
while True:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
quit()
elif (event.type == pygame.KEYDOWN):
if event.key == pygame.K_w and self.snackDirection != DIRECTION["down"]:
self.snackDirection = DIRECTION["up"]
if event.key == pygame.K_a and self.snackDirection != DIRECTION["right"]:
self.snackDirection = DIRECTION["left"]
if event.key == pygame.K_s and self.snackDirection != DIRECTION["up"]:
self.snackDirection = DIRECTION["down"]
if event.key == pygame.K_d and self.snackDirection != DIRECTION["left"]:
self.snackDirection = DIRECTION["right"]
self.body.rotate(1)
if self.snackDirection == DIRECTION["left"]:
self.head_x -= self.snackVal
self.head_x %= (SCREENWIDTH / ZOOM)
elif self.snackDirection == DIRECTION["right"]:
self.head_x += self.snackVal
self.head_x %= (SCREENWIDTH / ZOOM)
elif self.snackDirection == DIRECTION["up"]:
self.head_y -= self.snackVal
self.head_y %= (SCREENHEIGHT / ZOOM)
elif self.snackDirection == DIRECTION["down"]:
self.head_y += self.snackVal
self.head_y %= (SCREENHEIGHT / ZOOM)
self.body[0][0] = self.head_x
self.body[0][1] = self.head_y
if self.isCrash():
font = pygame.font.SysFont("freesansbold.ttf", 30)
text = font.render("Game Over!", True, (8,241,8))
self.SCREEN.blit(text, (200, 200))
pygame.display.update()
time.sleep(5)
self.__init__()
self.eatApple()
self.SCREEN.fill((0,0,0))
self.drawSnack()
pygame.draw.rect(self.SCREEN, (8,241,8), (self.preApple[0]*ZOOM, self.preApple[1]*ZOOM, ZOOM, ZOOM))
pygame.display.update()
self.CLOCKFPS.tick(FPS)
def RandomApple(self):
position_x = random.randint(0, SCREENWIDTH / ZOOM - 1)
position_y = random.randint(0, SCREENHEIGHT / ZOOM - 1)
while not self.AppleLegal(position_x, position_y):
position_x = random.randint(0, SCREENWIDTH / ZOOM -1)
position_y = random.randint(0, SCREENHEIGHT / ZOOM -1)
print("illagle")
return [position_x, position_y]
def AppleLegal(self, x, y):
Legal = True
for it in self.body:
if (it[0] == x and it[1] == y):
Legal = False
break
return Legal
def eatApple(self):
if self.preApple[0] == self.head_x and self.preApple[1] == self.head_y:
self.body.insert(0, [self.head_x, self.head_y])
self.preApple = self.RandomApple()
def drawSnack(self):
for it in self.body:
pygame.draw.rect(self.SCREEN, (255, 0, 0), (it[0] * ZOOM, it[1] * ZOOM, ZOOM, ZOOM))
def isCrash(self):
crash = False
for i in range(self.body.__len__()):
if i>1 and self.head_x==self.body[i][0] and self.head_y == self.body[i][1]:
crash = True
break
return crash
if __name__ == "__main__":
pygame.init()
game = GSgame()
print("游戏初始化完成")
game.start()
pygame.quit()