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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| import pygame import random import sys import time from pygame.locals import *
black_colour = pygame.Color(0, 0, 0) white_colour = pygame.Color(255, 255, 255) red_colour = pygame.Color(255, 0, 0) grey_colour = pygame.Color(150, 150, 150)
def GameOver(gamesurface):
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)
GameOver_colour = GameOver_font.render('Game Over', True, grey_colour)
GameOver_location = GameOver_colour.get_rect() GameOver_location.midtop = (320, 10) gamesurface.blit(GameOver_colour, GameOver_location)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
def main():
pygame.init() pygame.time.Clock() ftpsClock = pygame.time.Clock()
gamesurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('贪吃蛇~')
snakeposition = [100, 100]
snakelength = [[100, 100], [80, 100], [60, 100]]
square_purpose = [300, 300]
square_position = 1
derection = "right" change_derection = derection
while True:
for event in pygame.event.get(): if event.type == QUIT:
pygame.quit() sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'): change_derection = "right" if event.key == K_LEFT or event.key == ord('a'): change_derection = "left" if event.key == K_UP or event.key == ord('w'): change_derection = "up" if event.key == K_DOWN or event.key == ord('s'): change_derection = "down" if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT))
if change_derection == 'left' and not derection == 'right': derection = change_derection if change_derection == 'right' and not derection == 'left': derection = change_derection if change_derection == 'up' and not derection == 'down': derection = change_derection if change_derection == 'down' and not derection == 'up': derection = change_derection
if derection == 'left': snakeposition[0] -= 20 if derection == 'right': snakeposition[0] += 20 if derection == 'up': snakeposition[1] -= 20 if derection == 'down': snakeposition[1] += 20
snakelength.insert(0, list(snakeposition))
if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]: square_position = 0 else: snakelength.pop()
if square_position == 0:
x = random.randrange(1, 32) y = random.randrange(1, 24) square_purpose = [int(x * 20), int(y * 20)] square_position = 1
gamesurface.fill(black_colour) for position in snakelength: pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20)) pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))
pygame.display.flip()
if snakeposition[0] < 0 or snakeposition[0] > 620: GameOver(gamesurface) if snakeposition[1] < 0 or snakeposition[1] > 460: GameOver(gamesurface) for snakebody in snakelength[1:]: if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]: GameOver(gamesurface)
ftpsClock.tick(5)
if __name__ == "__main__": main()
|