|
<
- import pygame, sys, random, time
- from pygame.locals import *
- pygame.init()
- Clock = pygame.time.Clock()
- screen = pygame.display.set_mode((1200, 900))
- pygame.display.set_caption('贪吃蛇游戏')
- direct = 'right' ; GameOver = False
- food = [10, 4] ; head = [4, 4] ; snake= [ [4, 4], [3, 4], [2, 4]]
- font = pygame.font.SysFont("MicrosoftYaHei", 200).render('游戏完毕', True, (10, 255, 10))
- while True:
- Clock.tick(10)
- screen.fill((28, 56, 20))
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == KEYDOWN:
- if event.key == K_UP or event.key == K_w:
- if direct != 'bottom': direct = 'top'
- if event.key == K_DOWN or event.key == K_s:
- if direct != 'top' : direct = 'bottom'
- if event.key == K_LEFT or event.key == K_a:
- if direct != 'right' : direct = 'left'
- if event.key == K_RIGHT or event.key == K_d:
- if direct != 'left': direct = 'right'
- if direct == 'left': head[0] -= 1
- if direct == 'right': head[0] += 1
- if direct == 'top': head[1] -= 1
- if direct == 'bottom': head[1] += 1
- if head[0] < 0 or head[0] > 40 or head[1] < 0 or head[1] > 30:
- GameOver = True
- if head in snake[1:]: GameOver = True
- if GameOver:
- screen.blit(font, (200,300))
- pygame.display.flip()
- time.sleep(2)
- pygame.quit()
- sys.exit()
- snake.insert(0, list(head))
- if head == food:
- while True:
- food = [int(random.randrange(1,39)), int(random.randrange(1,29))]
- if food not in snake:
- break
- else:
- snake.pop()
- pygame.draw.rect(screen, (255,34 , 20), Rect(food[0]*30, food[1]*30, 30, 30))
- for body in snake:
- pygame.draw.rect(screen, (255, 144, 20), Rect(body[0]*30, body[1]*30, 29, 29))
- pygame.display.flip()
复造代码
免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作! |
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
|