Python realizes greedy snake __python

Source: Internet
Author: User
Tags ord
 
#!/usr/bin/env python #__ *__ coding:utf-8 __*__ import pygame,sys,time,random from pygame.locals import * Redcolour = Pygame. Color (255,0,0) Blackcolour = Pygame. Color (0,0,0) Whitecolour = Pygame. Color (255,255,255) Greycolour = Pygame. Color (150,150,150) def gameover (playsurface): Gameoverfont = Pygame.font.Font (' Arial.ttf ',) Gameoversurf = Gameoverf Ont.render (' Game over ', true,greycolour) Gameoverrect = Gameoversurf.get_rect () Gameoverrect.midtop = (320,10) playSurf Ace.blit (Gameoversurf,gameoverrect) Pygame.display.flip () Time.sleep (5) Pygame.quit () Sys.exit () def main (): Pygame. init () Playsurface = Pygame.display.set_mode ((640,480)) Fpsclock = Pygame.time.Clock () pygame.display.set_caption (' Snake Liu ') snakeposition = [100,100] snakesegments = [[100,100],[80,100],[60,100]] raspberryposition = [300,300] ras pberryspawned = 1 Direction = ' right ' changedirection = direction while true:for event in Pygame.event.get (): If
	Event.type = = QUIT:sys.exit ()		elif Event.type = = Keydown:if Event.key = = K_right or Event.key = Ord (' d '): Changedirection = ' right ' if Event.key = = K_left or Event.key = Ord (' A '): Changedirection = ' Left ' if event.key = = k_up or Event.key = = Ord ('  W '): changedirection = ' up ' if event.key = = k_down/Event.key = = Ord (' s '): changedirection = ' down ' if Event.key = = K_ESCAPE:pygame.event.post (pygame.event.Event (QUIT)) # To determine whether to enter the opposite direction, with the original changes if changedirection = = ' Ri Ght ': direction = changedirection if changedirection = = ' Left ': direction = changedirection if changedirection =  = ' up ': direction = changedirection if changedirection = ' down ': direction = changedirection # Moving the coordinates of the snake head according to the direction if Direction = = ' right ': snakeposition[0] + = if direction = ' Left ': snakeposition[0]-= if direction = ' Up ': snakeposition[1] = Direction = = ' down ': snakeposition[1] + = 20 # Increase the length of the Snake Snakesegments.insert (0,list ( snakeposition)) # to determine whetherAte the raspberry if snakeposition[0] = = Raspberryposition[0] and snakeposition[1] = = Raspberryposition[1]: raspberryspawned = 0 Else:snakeSegments.pop () # If you eat raspberries, regenerate the raspberry if raspberryspawned = = 0:x = Random.randrange (1,32) y = random. Randrange (1,24) raspberryposition = [Int (x*20), int (y*20)] raspberryspawned = 1 # refresh Pygame display layer Playsurface.fill (b Lackcolour) for position in SnakeSegments:pygame.draw.rect (Playsurface,whitecolour,rect (position[0],position[1],20
		)) Pygame.draw.rect (Playsurface,redcolour,rect (raspberryposition[0], raspberryposition[1],20,20)) Pygame.display.flip () # To determine whether death, the following lines and original changes if SNAKEPOSITION[0] > 620 or snakeposition[0] < 0:gameover (PLAYSU Rface) if snakeposition[1] > 460 or snakeposition[1] < 0:gameover (playsurface) for snakebody in Snakesegment S[1:]: if snakeposition[0] = = Snakebody[0] and snakeposition[1] = = Snakebody[1]: Gameover (playsurface) fpsclock.t
Ick (5) if __name__ = = "__main__": Main () 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.