[C++] 纯文本查看 复制代码
import pygame
import sys
import game
# 初始化
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("贪吃蛇")
Snaker = game.Snake(screen)
clock = pygame.time.Clock()
direction = 'up' # 初始运动方向
# 加载贪吃蛇图片
snake = pygame.image.load("image\贪吃蛇图片.png")
# 初始化 [蛇身体列表] 的代码
snake_list = [[400, 400], [420, 400], [440, 400],
[460, 400], [480, 400], [500, 400], [520, 400], [540, 400], [560, 400], [580, 400], [600, 400], [620, 400], [640, 400], [660, 400], [680, 400]
, [700, 400], [720, 400], [740, 400], [760, 400], [780, 400], [800, 400], [820, 400], [840, 400], [860, 400], [880, 400]
, [900, 400], [920, 400], [940, 400], [960, 400], [980, 400], [1000, 400], [1020, 400], [1040, 400], [1060, 400], [1080, 400]
, [1100, 400], [1120, 400], [1140, 400], [1160, 400], [1180, 400], [1200, 400], [1220, 400], [1240, 400], [1260, 400], [1280, 400]
, [1300, 400], [1320, 400], [1340, 400], [1360, 400], [1380, 400], [1400, 400], [1420, 400], [1440, 400], [1460, 400], [1480, 400],
[1500,400],[1520,400],[1540,400],[1560,400],[1580,400],[1600,400],[1620,400],[1640,400],[1660,400],[1680,400],[1700,400],[1720,400],
[1740,400],[1760,400],[1780,400],[1800,400],[1820,400],[1840,400],[1860,400],[1880,400],[1900,400],[1920,400],[1940,400],[1960,400],
[1980,400],[2000,400],[2020,400],[2040,400],[2060,400],[2080,400],[2100,400],[2120,400],[2140,400],[2160,400],[2180,400],[2200,400],
[2220,400],[2240,400],[2260,400],[2280,400],[2300,400],[2320,400],[2340,400],[2360,400],[2380,400],[2400,400],[2420,400],[2440,400],
[2460,400],[2480,400],[2500,400],[2520,400],[2540,400],[2560,400],[2580,400],[2600,400],[2620,400],[2640,400],[2660,400],[2680,400],
[2700,400],[2720,400],[2740,400],[2760,400],[2780,400],[2800,400],[2820,400],[2840,400],[2860,400],[2880,400],[2900,400],[2920,400],
[2940,400],[2960,400],[2980,400],[3000,400],[3020,400],[3040,400],[3060,400],[3080,400],[3100,400],[3120,400],[3140,400],[3160,400],
[3180,400],[3200,400],[3220,400],[3240,400],[3260,400],[3280,400],[3300,400],[3320,400],[3340,400],[3360,400],[3380,400],[3400,400],
[3420,400],[3440,400],[3460,400],[3480,400],[3500,400],[3520,400],[3540,400],[3560,400],[3580,400],[3600,400],[3620,400],[3640,400],
[3660,400],[3680,400],[3700,400],[3720,400],[3740,400],[3760,400],[3780,400],[3800,400],[3820,400],[3840,400],[3860,400],[3880,400],
[3900,400],[3920,400],[3940,400],[3960,400],[3980,400],[4000,400],[4020,400],[4040,400],[4060,400],[4080,400],[4100,400],[4120,400],
[4140,400],[4160,400],[4180,400],[4200,400],[4220,400],[4240,400],[4260,400],[4280,400],[4300,400],[4320,400],[4340,400],[4360,400],
[4380,400],[4400,400],[4420,400],[4440,400],[4460,400],[4480,400],[4500,400],[4520,400],[4540,400],[4560,400],[4580,400],[4600,400],
[4620,400],[4640,400],[4660,400],[4680,400],[4700,400],[4720,400],[4740,400],[4760,400],[4780,400],[4800,400],[4820,400],[4840,400],
[4860,400],[4880,400],[4900,400],[4920,400],[4940,400],[4960,400],[4980,400],[5000,400],[5020,400],[5040,400],[5060,400],[5080,400],
[5100,400],[5120,400],[5140,400],[5160,400],[5180,400],[5200,400],[5220,400],[5240,400],[5260,400],[5280,400],[5300,400],[5320,400],
[5340,400],[5360,400],[5380,400],[5400,400],[5420,400],[5440,400],[5460,400],[5480,400],[5500,400],[5520,400],[5540,400],[5560,400],
[5580,400],[5600,400],[5620,400],[5640,400],[5660,400],[5680,400],[5700,400],[5720,400],[5740,400],[5760,400],[5780,400],[5800,400],
[5820,400],[5840,400],[5860,400],[5880,400],[5900,400],[5920,400],[5940,400],[5960,400],[5980,400],[6000,400],[6020,400],[6040,400],
[6060,400],[6080,400],[6100,400],[6120,400],[6140,400],[6160,400],[6180,400],[6200,400],[6220,400],[6240,400],[6260,400],[6280,400],
[6300,400],[6320,400],[6340,400],[6360,400],[6380,400],[6400,400],[6420,400],[6440,400],[6460,400],[6480,400],[6500,400],[6520,400],
[6540,400],[6560,400],[6580,400],[6600,400],[6620,400],[6640,400],[6660,400],[6680,400],[6700,400],[6720,400],[6740,400],[6760,400],
[6780, 400], [6800, 400], [6820, 400], [6840, 400], [6860, 400], [6880, 400], [6900, 400], [6920, 400], [6940, 400], [6960, 400],
[6980, 400], [7000, 400], [7020, 400], [7040, 400], [7060, 400], [7080, 400], [7100, 400], [7120, 400], [7140, 400], [7160, 400],
[7180, 400], [7200, 400], [7220, 400], [7240, 400], [7260, 400], [7280, 400], [7300, 400], [7320, 400], [7340, 400], [7360, 400],
[7380, 400], [7400, 400], [7420, 400], [7440, 400], [7460, 400], [7480, 400], [7500, 400], [7520, 400], [7540, 400], [7560, 400],
[7580, 400], [7600, 400], [7620, 400], [7640, 400], [7660, 400], [7680, 400], [7700, 400], [7720, 400], [7740, 400], [7760, 400],
[7780, 400], [7800, 400], [7820, 400], [7840, 400], [7860, 400], [7880, 400], [7900, 400], [7920, 400], [7940, 400], [7960, 400],
[7980, 400], [8000, 400], [8020, 400], [8040, 400], [8060, 400], [8080, 400], [8100, 400], [8120, 400], [8140, 400], [8160, 400],
[8180, 400], [8200, 400], [8220, 400], [8240, 400], [8260, 400], [8280, 400], [8300, 400], [8320, 400], [8340, 400], [8360, 400],
[8380, 400], [8400, 400], [8420, 400], [8440, 400], [8460, 400], [8480, 400], [8500, 400], [8520, 400], [8540, 400], [8560, 400],
[8580, 400], [8600, 400], [8620, 400], [8640, 400], [8660, 400], [8680, 400],[8700,400],[8720,400],[8740,400],[8760,400],[8780,400],
[8800,400],[8820,400],[8840,400],[8860,400],[8880,400],[8900,400],[8920,400],[8940,400],[8960,400],[8980,400],[9000,400],[9020,400],
[9040,400],[9060,400],[9080,400],[9100,400],[9120,400],[9140,400],[9160,400],[9180,400],[9200,400],[9220,400],[9240,400],[9260,400],
[9280,400],[9300,400],[9320,400],[9340,400],[9360,400],[9380,400],[9400,400],[9420,400],[9440,400],[9460,400],[9480,400],[9500,400],
[9520,400],[9540,400],[9560,400],[9580,400],[9600,400],[9620,400],[9640,400],[9660,400],[9680,400],[9700,400],[9720,400],[9740,400],
[9760,400],[9780,400],[9800,400],[9820,400],[9840,400],[9860,400],[9880,400],[9900,400],[9920,400],[9940,400],[9960,400],[9980,400],
[10000,400],[10020,400],[10040,400],[10060,400],[10080,400],[10100,400],[10120,400],[10140,400],[10160,400],[10180,400],[10200,400],
[10220,400],[10240,400],[10260,400],[10280,400],[10300,400],[10320,400],[10340,400],[10360,400],[10380,400],[10400,400],[10420,400],[10440,400]]
# 主循环
while True:
# 事件检测
for event in pygame.event.get():
# 退出事件
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 控制方向功能
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = 'up'
if event.key == pygame.K_DOWN:
direction = 'down'
if event.key == pygame.K_LEFT:
direction = 'left'
if event.key == pygame.K_RIGHT:
direction = 'right'
# 不能直接改变为相反的方向
direction = Snaker.change_direction(direction)
# 【你需要在这里编写 [移动功能] 的代码】
if direction == 'up':
head_x = snake_list[0][0]
head_y = snake_list[0][1] - 20
head = [head_x, head_y]
snake_list.insert(0, head)
snake_list.pop()
if direction == 'down':
head_x = snake_list[0][0]
head_y = snake_list[0][1] + 20
head = [head_x, head_y]
snake_list.insert(0, head)
snake_list.pop()
if direction == 'left':
newhead_x = snake_list[0][0] - 20
newhead_y = snake_list[0][1]
newhead = [newhead_x, newhead_y]
snake_list.insert(0, newhead)
snake_list.pop()
if direction == 'right':
newhead_x = snake_list[0][0] + 20
newhead_y = snake_list[0][1]
newhead = [newhead_x, newhead_y]
snake_list.insert(0, newhead)
snake_list.pop()
# 绘制背景
screen.fill((226, 233, 254))
# 绘制方格网
Snaker.draw_grid()
# 绘制食物
snake_list = Snaker.eat(snake_list)
# [绘制贪吃蛇身体]
for position in snake_list:
screen.blit(snake, (position[0], position[1]))
# 规则一:吃到自己死亡
# 规则二:碰到墙死亡
Snaker.fail(snake_list[0])
# 显示游戏结果
Snaker.dreaw_score()
# 点击退出
Snaker.click_quit()
# 更新屏幕
pygame.display.update()
# 设置游戏速度
clock.tick(5)