Advertisement
Guest User

new

a guest
May 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.01 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*-coding:utf-8-*-
  3. import pygame,sys
  4. import win32api,win32console,win32gui,codecs
  5. import time,random
  6. from pygame.sprite import Sprite
  7.  
  8. pygame.init()
  9.  
  10. win = win32console.GetConsoleWindow()
  11. win32gui.ShowWindow(win,0)
  12.  
  13. white = (255,255,255)
  14. black = (0,0,0)
  15. red = (255,0,0)
  16. green = (0,155,0)
  17.  
  18. display_width = 800
  19. display_height = 600
  20.  
  21. gameDisplay=pygame.display.set_mode((display_width,display_height))
  22. pygame.display.set_caption("Flafel")
  23.  
  24. icon=pygame.image.load("apple.png")
  25. pygame.display.set_icon(icon)
  26.  
  27. img=pygame.image.load("snakehead.png")
  28. appleimg=pygame.image.load("apple.png")
  29.  
  30. clock = pygame.time.Clock()
  31.  
  32. AppleThickness=30
  33. block_size = 20
  34. FPS = 15
  35.  
  36. direction="right"
  37.  
  38. smallfont = pygame.font.SysFont("comicsansms",25)
  39. medfont = pygame.font.SysFont("comicsansms",50)
  40. largefont = pygame.font.SysFont("comicsansms",80)
  41.  
  42. pygame.mixer.init()
  43. intro_sound=pygame.mixer.Sound("intro.wav")
  44. dead_sound=pygame.mixer.Sound("dead.wav")
  45.  
  46. def game_intro():
  47.     intro=True
  48.     while intro:
  49.         for event in pygame.event.get():
  50.             if event.type==pygame.QUIT:
  51.                 pygame.quit()
  52.                 quit()
  53.             if event.type==pygame.KEYDOWN:
  54.                 if event.key==pygame.K_c:
  55.                     intro=False
  56.                 if event.key==pygame.K_q:
  57.                     pygame.quit()
  58.                     quit()
  59.         gameDisplay.fill(white)
  60.        
  61.         message_to_screen("Welcome to Flafel",green,-100,"large")
  62.         message_to_screen("The objective of the game is to eat red apples",black,-30)
  63.         message_to_screen("The more apples you eat,the longer you get",black,10)
  64.         message_to_screen("If you run into yourself, or the edges, you die!",black,50)
  65.         message_to_screen("Press C to play, P to pause or Q to quit",black,180)
  66.         pygame.display.update()
  67.         clock.tick(15)
  68.  
  69. def pause():
  70.  
  71.     paused=True
  72.    
  73.     message_to_screen("Paused",black,-100,size="large")
  74.     message_to_screen("Press C to continue or Q to quit",black,25)
  75.  
  76.     pygame.display.update()
  77.  
  78.     while paused:
  79.         for event in pygame.event.get():
  80.             if event.type==pygame.QUIT:
  81.                 pygame.quit()
  82.                 quit()
  83.             if event.type==pygame.KEYDOWN:
  84.                 if event.key==pygame.K_c:
  85.                     paused=False
  86.                 elif event.key==pygame.K_q:
  87.                     pygame.quit()
  88.                     quit()
  89.        
  90.         clock.tick(5)
  91.    
  92. def score(score):
  93.  
  94.     text=smallfont.render("Score: "+str(score),True,black)
  95.     gameDisplay.blit(text,[0,0])
  96.  
  97.    
  98. def randAppleGen():
  99.  
  100.     randApplex = round(random.randrange(0,display_width-AppleThickness))#/10.0)*10.0
  101.     randAppley = round(random.randrange(0,display_height-AppleThickness))#/10.0)*10.0
  102.     return randApplex,randAppley
  103.  
  104. def snake(block_size,snakeList):
  105.  
  106.     if direction=="right":
  107.         head=pygame.transform.rotate(img,270)
  108.     if direction=="left":
  109.         head=pygame.transform.rotate(img,90)
  110.     if direction=="up":
  111.         head=img
  112.     if direction=="down":
  113.         head=pygame.transform.rotate(img,180)
  114.  
  115.     gameDisplay.blit(head,(snakeList[-1][0],snakeList[-1][1]))
  116.  
  117.     for XnY in snakeList[:-1]:
  118.         pygame.draw.rect(gameDisplay, green, (XnY[0],XnY[1],block_size,block_size))
  119.    
  120. def text_objects(text,color,size):
  121.  
  122.     if size=="small":
  123.         textSurface=smallfont.render(text,True,color)
  124.     elif size=="medium":
  125.         textSurface=medfont.render(text,True,color)
  126.     elif size=="large":
  127.         textSurface=largefont.render(text,True,color)
  128.     return textSurface,textSurface.get_rect()
  129.    
  130. def message_to_screen(msg,color,y_displace=0,size="small"):
  131.  
  132.     textSurf,textRect=text_objects(msg,color,size)
  133.     textRect.center=(display_width/2),(display_height/2)+y_displace
  134.     gameDisplay.blit(textSurf,textRect)
  135.    
  136.  
  137.  
  138. def gameLoop():
  139.  
  140.     global direction
  141.  
  142.     direction="right"
  143.     running = True
  144.     gameOver= False
  145.  
  146.     lead_x = display_width/2
  147.     lead_y = display_height/2
  148.  
  149.     lead_x_change = 10
  150.     lead_y_change = 0
  151.  
  152.     snakeList=[]
  153.     snakeLength=1
  154.  
  155.     randApplex,randAppley=randAppleGen()
  156.    
  157.     while running:
  158.         if gameOver==True:
  159.             message_to_screen("Game over",red,-50,size="large")
  160.             message_to_screen("Press C to play again or Q to quit",black,50,size="medium")
  161.             pygame.display.update()
  162.            
  163.         while gameOver == True:
  164.             #gameDisplay.fill(white)
  165.            
  166.             for event in pygame.event.get():
  167.                 if event.type==pygame.QUIT:
  168.                     gameOver=False
  169.                     running=False
  170.                 if event.type==pygame.KEYDOWN:
  171.                     if event.key==pygame.K_q:
  172.                         running=False
  173.                         gameOver=False
  174.                     if event.key==pygame.K_c:
  175.                         gameLoop()
  176.            
  177.         for event in pygame.event.get():
  178.             if event.type == pygame.QUIT:
  179.                 running = False
  180.             if event.type == pygame.KEYDOWN:
  181.                 if event.key == pygame.K_LEFT:
  182.                     direction="left"
  183.                     lead_x_change = -block_size
  184.                     lead_y_change = 0
  185.                 elif event.key == pygame.K_RIGHT:
  186.                     direction="right"
  187.                     lead_x_change = block_size
  188.                     lead_y_change = 0
  189.                 elif event.key == pygame.K_UP:
  190.                     direction="up"
  191.                     lead_y_change = -block_size
  192.                     lead_x_change = 0
  193.                 elif event.key == pygame.K_DOWN:
  194.                     direction="down"
  195.                     lead_y_change = block_size
  196.                     lead_x_change = 0
  197.                 elif event.key==pygame.K_p:
  198.                     pause()
  199.         if lead_x>=display_width or lead_x<0 or lead_y<0 or lead_y>=display_height:
  200.            gameOver=True
  201.            dead_sound.play()
  202.  
  203.  
  204.         lead_x += lead_x_change
  205.         lead_y += lead_y_change
  206.         gameDisplay.fill(white)
  207.  
  208.         gameDisplay.blit(appleimg,(randApplex,randAppley))
  209.  
  210.        
  211.         snakeHead=[]
  212.         snakeHead.append(lead_x)
  213.         snakeHead.append(lead_y)
  214.         snakeList.append(snakeHead)
  215.  
  216.         if len(snakeList)>snakeLength:
  217.             del snakeList[0]
  218.         for eachSegment in snakeList[:-1]:
  219.             if eachSegment==snakeHead:
  220.                 gameOver=True
  221.                 dead_sound.play()
  222.                
  223.                
  224.         snake(block_size,snakeList)
  225.  
  226.         score(snakeLength-1)
  227.  
  228.        
  229.         pygame.display.update()
  230.  
  231.        
  232.         if lead_x>randApplex and lead_x <randApplex+AppleThickness or lead_x+block_size>randApplex and lead_x+block_size<randApplex+AppleThickness:
  233.             if lead_y>randAppley and lead_y <randAppley+AppleThickness:
  234.                 randApplex,randAppley=randAppleGen()
  235.                 snakeLength+=1
  236.             elif lead_y+block_size > randAppley and lead_y+block_size<randAppley+AppleThickness:
  237.                 randApplex,randAppley=randAppleGen()
  238.                 snakeLength+=1
  239.  
  240.         clock.tick(FPS)
  241.    
  242.     pygame.quit()
  243.     quit()
  244. intro_sound.play()
  245. game_intro()
  246. intro_sound.stop()
  247. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement