The python game development - Snake

#! / usr / bin / Python the env 
# = UTF-Coding. 8
Import the pygame
Import SYS
Import Random

# global definition, screen width and length
SCREEN_X = 600
SCREEN_Y = 600


# snakes
# 25 points in units of
class Snake (object) : # define a class --Snake, default object class, if you have the better you can replace it with
# initialize various properties required [default right / body block at the start x5]
DEF __init __ (Self): #define init method, the first parameter is always self itself, inside __init__ method, you can put all kinds of property is bound to self, because the self points to create an instance of itself.
# With __init__ method, create an instance in time, you can not pass empty parameters, you must pass parameters match the __init__ method, but do not need to wear self
# For example, if it is def __init __ (self, name, score): this type of then self need not, but must have the name and the score
self.dirction = pygame.K_RIGHT
self.body = []
for X in Range (. 5):
self.addnode () # addnode call function, increased body, after five cycles, the initial value of five snake Node

# Snake increasing function of the length of the body, have increased whenever the snake at a front end block
def addnode (self) :
left, Top = (0, 0) is the default # 0
IF self.body:
left, Top = (self.body [0] .left, self.body [0] .top)
Node = pygame.Rect (left, the size of the top, 25, 25) # node of a node
IF self.dirction == pygame.K_LEFT:
node.left - = 25
elif self.dirction == pygame.K_RIGHT:
node.left = + 25
elif self.dirction == pygame.K_UP:
node.top - = 25
elif self.dirction == pygame.K_DOWN:
node.top = + 25
self.body.insert (0, node) the insertion node #

# Delete the last block, pop pop elements from the last default
DEF delnode (Self):
self.body.pop ()

# death judgment
DEF isdead (Self):
# hit the wall, not in the body top part SCREEN in the X or Y value ( 0,600), that the wall of death
IF self.body [0] .x not in the Range (SCREEN_X):
return True
IF self.body [0] .y not in the Range (SCREEN_Y):
return True
# hit own head and any part of the body collisions. body [0] is the first element, body [1:] is taken from the second element, the end of the
IF self.body [0] in self.body [. 1:]:
return True
return False

# mobile!
Move DEF (Self):
self.addnode ()
self.delnode ()

# change direction, but the left and right, up and down reverse can not be changed
def changedirection (self, curkey):
= The LR [pygame.K_LEFT, pygame.K_RIGHT]
the UD = [pygame.K_UP, pygame.K_DOWN]
IF curkey the UD in the LR +:
IF (curkey in the LR) and (self.dirction in the LR):
return
IF (curkey in the UD ) and (self.dirction in the UD):
return
self.dirction = curkey


#-food
# method: placement / removal
# 25 points in units of
class food:
DEF the __init __ (Self):
self.rect pygame.Rect = (- 25, 0, 25, 25)

DEF Remove (Self):
self.rect.x = -25

DEF SET (Self):
IF self.rect.x == -25:
allpos = []
# is not too close to the wall 25 ~ between SCREEN_X-25
Range in POS for (25, SCREEN_X - 25, 25):
allpos.append (POS)
self.rect.left = The random.choice (allpos)
self.rect.top = The random.choice (allpos)
Print (self.rect)


(Screen, POS, text, Color, font_bold = False, FONT_SIZE = 60, font_italic = False) DEF show_text:
# acquisition system font and set the text size
cur_font = pygame.font.SysFont ( "Arial", FONT_SIZE)
# set whether bold attribute
cur_font.set_bold (font_bold)
# set italic attribute
cur_font.set_italic (font_italic)
# set text
text_fmt = cur_font.render (text,. 1, Color)
# draw text
screen.blit (text_fmt, POS)


DEF main ( ):
pygame.init ()
screen_size = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
scores = 0
isdead = False

# 蛇/食物
snake = Snake()
food = Food()

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
snake.changedirection(event.key)
# 死后按space重新
if event.key == pygame.K_SPACE and isdead:
return main()

screen.fill((255, 255, 255))

Videos snake # / + 1 min each step
IF Not isdead:
Scores + =. 1
snake.move ()
for RECT in snake.body:
pygame.draw.rect (Screen, (20 is, 220, 39), RECT, 0)

death # display text
isdead = snake.isdead ()
IF isdead:
show_text (Screen, (100, 200 is), 'YOU of dEAD!', (227, 29, 18 is), False, 100)
show_text (Screen, (150, 260. ), 'to the try Again ... Space Press', (0, 0, 22 is), False, 30)

# food processing / min +50 eat
# snakehead coincides with when the food rect, eat -> Snake increase a Node
== snake.body food.rect IF [0]:
Scores + = 50
food.remove ()
snake.addnode ()

Food # delivery
food.set ()
pygame.draw.rect (Screen, (136, 0, 21 is), food.rect, 0)

# score displayed text
show_text (screen, (50, 500 ), 'Scores:' + str (Scores), (223, 223, 223))

the pygame.display.update ()
clock.tick (10)


IF the __name__ == '__main__':
main ()

Guess you like

Origin www.cnblogs.com/Lynn123/p/11824519.html