Advertisement
ES99

fail snake character

Apr 23rd, 2017
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.40 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4.  
  5.  
  6. pygame.init()
  7.  
  8.  
  9. white = (255,250,250)
  10. black = (0,0,0)
  11. red = (255,0,0)
  12. lime_green = (0,200,0)
  13. snakething = (164,120,80)
  14. gray = (124,124,124)
  15. red = (200,0,0)
  16. light_red = (255,0,0)
  17.  
  18. yellow = (200,200,0)
  19. light_yellow = (255,255,0)
  20.  
  21. green = (34,177,76)
  22. light_green = (0,255,0)
  23.  
  24. display_width = 800
  25. display_height = 600
  26.  
  27.  
  28. gameDisplay = pygame.display.set_mode((display_width,display_height))
  29. pygame.display.set_caption('MLG Snake')
  30.  
  31.  
  32.  
  33. charL = pygame.image.load('goomba left.png')
  34. charR = pygame.image.load('goomba right.png')
  35. charU = pygame.image.load('goomba backward.png')
  36. charD = pygame.image.load('goompa forward.png')
  37. loser = pygame.image.load('game over.png')
  38. imggg = pygame.image.load('introo.png')
  39. yum = pygame.image.load('doritos.png')
  40. cont = pygame.image.load('controls.png')
  41. button_1 = pygame.image.load('button 1.png')
  42. button_2 = pygame.image.load('button 2.png')
  43. button_3 = pygame.image.load('button 3.png')
  44. button_4 = pygame.image.load('button 4.png')
  45.  
  46.  
  47.  
  48. icon = pygame.image.load('doritos.png')
  49. pygame.display.set_icon(icon)
  50.  
  51.  
  52.  
  53.  
  54. clock = pygame.time.Clock()
  55.  
  56.  
  57.  
  58. block_size = 20
  59.  
  60. AppleThickness = 30
  61.  
  62. FPS = 15
  63.  
  64. direction = "left"
  65.  
  66. smallfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 25)
  67.  
  68. smallmedfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 38)
  69.  
  70. medfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 50)
  71.  
  72. largefont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 80)
  73.  
  74. tinyfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 18)
  75.  
  76.  
  77.  
  78. def pause():
  79.     paused = True
  80.     message_to_screen("Paused", black, -100, "large")
  81.     message_to_screen("Press U to continue or q to quit.", black, 25)
  82.     pygame.display.update()
  83.  
  84.     while paused:
  85.         for event in pygame.event.get():
  86.             if event.type == pygame.QUIT:
  87.                 pygame.quit()
  88.                 quit()
  89.             if event.type == pygame.KEYDOWN:
  90.                 if event.key == pygame.K_u:
  91.                     paused = False
  92.  
  93.                 elif event.key == pygame.K_q:
  94.                     pygame.quit()
  95.  
  96.                     quit()
  97.  
  98.         #gameDisplay.fill(gray)
  99.        
  100.         clock.tick(15)
  101.  
  102. def score(score):
  103.     text = smallfont.render("Score: "+str(score), True, black)
  104.     gameDisplay.blit(text, [0,-10])
  105.  
  106. def randAppleGen():
  107.     randAppleX = random.randrange(0, display_width - AppleThickness, 10)
  108.     randAppleY = random.randrange(0, display_height - AppleThickness, 10)
  109.  
  110.     return randAppleX, randAppleY
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.        
  120.  
  121.  
  122.  
  123. def snake(block_size, snakeList):
  124.  
  125.  
  126.     if direction == "left":
  127.         gameDisplay.blit(charL, (snakeList[-1][0], snakeList[-1][1]))
  128.         proper = charL
  129.     if direction == "right":
  130.         gameDisplay.blit(charR, (snakeList[-1][0], snakeList[-1][1]))
  131.         proper = charR
  132.     if direction == "up":
  133.         gameDisplay.blit(charU, (snakeList[-1][0], snakeList[-1][1]))
  134.         proper = charU
  135.     if direction == "down":
  136.         gameDisplay.blit(charD, (snakeList[-1][0], snakeList[-1][1]))
  137.         proper = charD
  138.  
  139.        
  140.  
  141.     for XnY in snakeList[:-1]:
  142.         gameDisplay.blit(proper, [XnY[0],XnY[1],block_size,block_size])
  143.        
  144. def text_objects(text,color,size):
  145.     if size == "small":
  146.         textSurface = smallfont.render(text,True,color)
  147.     elif size == "medium":
  148.         textSurface = medfont.render(text,True,color)
  149.     elif size == "tiny":
  150.         textSurface = tinyfont.render(text,True,color)
  151.     elif size == "smallmedium":
  152.         textSurface = smallmedfont.render(text,True,color)
  153.     elif size == "large":
  154.         textSurface = largefont.render(text,True,color)
  155.  
  156.     return textSurface, textSurface.get_rect()
  157.  
  158. def text_to_button(msg,color,buttonx,buttony,buttonwidth,buttonheight,size = "small"):
  159.     textSurf, textRect = text_objects(msg,color,size)
  160.     textRect.center = ((buttonx + buttonwidth/2)), buttony+(buttonheight/2)
  161.     gameDisplay.blit(textSurf, textRect)
  162.    
  163.    
  164.  
  165. def message_to_screen(msg,color, y_displace=0, size = "small"):
  166.     textSurf, textRect = text_objects(msg,color,size)
  167.     textRect.center = (display_width/2), (display_height/2)+ y_displace
  168.     gameDisplay.blit(textSurf, textRect)
  169.  
  170.  
  171. def button(text,x,y,width,height,inactive_color,active_color,size = "small",action = None):
  172.     cur = pygame.mouse.get_pos()
  173.     click = pygame.mouse.get_pressed()
  174.     #print(click)
  175.     if x + width > cur[0] > x and y + height > cur[1] > y:
  176.         pygame.draw.rect(gameDisplay, active_color,(x,y,width,height))
  177.         if click[0] == 1 and action != None:
  178.             if action == "quit":
  179.                 pygame.quit()
  180.                 quit()
  181.             if action == "controls":
  182.                 game_controls()
  183.        
  184.             elif action == "play":
  185.                 gameLoop()
  186.             elif action == "main":
  187.                 game_intro()
  188.             elif action == "char":
  189.                 game_char()
  190.             elif action == "mario":
  191.                 currentchar = "mario"
  192.                
  193.                 if currentchar == "mario":
  194.                     charU = pygame.image.load('mario up')
  195.                     charD = pygame.image.load('mario down')
  196.                     charL = pygame.image.load('mario left')
  197.                     charR = pygame.image.load('mario right')
  198.                     gameLoop()
  199.  
  200.  
  201.        
  202.  
  203.                
  204.                
  205.                
  206.            
  207.     else:
  208.         pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
  209.     text_to_button(text,black,x,y,width,height,size)
  210.    
  211.  
  212.  
  213.  
  214.  
  215. def game_char():
  216.     char = True
  217.     while char:
  218.        
  219.         for event in pygame.event.get():
  220.             if event.type == pygame.QUIT:
  221.                 pygame.quit()
  222.                 quit()
  223.        
  224.         gameDisplay.fill(black)      
  225.  
  226.         button("Mario",50,100,150,50,white,gray,"small",action = "goomba")
  227.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  228.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  229.        
  230.  
  231.        
  232.        
  233.         pygame.display.update()
  234.         clock.tick(60)  
  235.  
  236.        
  237.  
  238.  
  239.  
  240. def game_controls():
  241.     gcont = True
  242.     while gcont:
  243.        
  244.         for event in pygame.event.get():
  245.             if event.type == pygame.QUIT:
  246.                 pygame.quit()
  247.                 quit()
  248.        
  249.         gameDisplay.fill(black)      
  250.         gameDisplay.blit(cont,(0,0))
  251.  
  252.         button("Play",150,500,100,50,white,gray,"small",action = "play")
  253.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  254.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  255.  
  256.        
  257.         pygame.display.update()
  258.         clock.tick(60)
  259.  
  260.  
  261.  
  262. def game_intro():
  263.     intro = True
  264.     while intro:
  265.         for event in pygame.event.get():
  266.             if event.type == pygame.QUIT:
  267.                 pygame.quit()
  268.                 quit()
  269.             elif event.type == pygame.KEYDOWN:
  270.                     if event.key == pygame.K_p:
  271.                         intro = False
  272.         gameDisplay.fill(black)
  273.         gameDisplay.blit(imggg,(0,0))
  274.        
  275.    
  276.         button("Play",150,500,100,50,white,gray,"small",action = "play")
  277.         button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
  278.         button("Characters",295,425,200,50,white,gray,"tiny", action = "char")
  279.         button(" ",0,0,0,0,white,gray,"tiny", action = "controls")        
  280.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  281.  
  282.        
  283.  
  284.        
  285.        
  286.                
  287.  
  288.        
  289.         pygame.display.update()
  290.         clock.tick(60)
  291.        
  292.        
  293.        
  294.  
  295.        
  296.  
  297.  
  298.  
  299. def gameLoop():
  300.     global direction
  301.     direction = "left"
  302.     gameExit = False
  303.     gameOver= False        
  304.     lead_y = display_height/2
  305.     lead_x = display_width/2
  306.  
  307.     lead_x_change = -10
  308.     lead_y_change = 0
  309.  
  310.     snakeList = []
  311.     snakeLength = 1
  312.  
  313.     randAppleX, randAppleY = randAppleGen()
  314.    
  315.     while not gameExit:
  316.  
  317.         while gameOver == True:
  318. ##            gameDisplay.blit(loser,(0,0))
  319.             message_to_screen("Game over",
  320.                               red,y_displace=-50,size="large")
  321.            
  322.             message_to_screen("Press P to play again or Q to quit",
  323.                               black,50,size="small")
  324.             pygame.display.update()
  325.            
  326.  
  327.             for event in pygame.event.get():
  328.                 if event.type == pygame.QUIT:
  329.                    
  330.                     gameExit = True
  331.                     gameOver = False
  332.                    
  333.                 if event.type == pygame.KEYDOWN:
  334.                     if event.key == pygame.K_q:
  335.                         gameExit = True
  336.                         gameOver = False
  337.                     if event.key == pygame.K_p:
  338.                         gameLoop()
  339.  
  340.        
  341.         for event in pygame.event.get():
  342.             if event.type == pygame.QUIT:
  343.                         gameExit = True
  344.             if event.type == pygame.KEYDOWN:
  345.                     if event.key == pygame.K_a:
  346.  
  347.                             direction = "left"
  348.                             lead_x_change = -block_size
  349.                             lead_y_change = 0
  350.                     elif event.key == pygame.K_d:
  351.                             direction = "right"
  352.                             lead_x_change = block_size
  353.                             lead_y_change = 0
  354.                     elif event.key == pygame.K_w:
  355.                             direction = "up"
  356.                             lead_y_change = -block_size
  357.                             lead_x_change = 0
  358.                     elif event.key == pygame.K_s:
  359.                             direction = "down"
  360.                             lead_y_change = block_size
  361.                             lead_x_change = 0
  362.                     elif event.key == pygame.K_UP:
  363.                             direction = "up"
  364.                             lead_x_change = 0
  365.                             lead_y_change = -block_size
  366.                     elif event.key == pygame.K_DOWN:
  367.                             direction = "down"
  368.                             lead_y_change = block_size
  369.                             lead_x_change = 0
  370.                     elif event.key == pygame.K_LEFT:
  371.                             direction = "left"
  372.                             lead_x_change = -block_size
  373.                             lead_y_change = 0
  374.                     elif event.key == pygame.K_RIGHT:
  375.                             direction = "right"
  376.                             lead_x_change = block_size
  377.                             lead_y_change = 0
  378.                     elif event.key == pygame.K_ESCAPE:
  379.                         pause()
  380.                        
  381.         if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
  382.             gameOver = True
  383.  
  384.        
  385.         '''lead_x = (lead_x + lead_x_change) % 800 lead_y = (lead_y + lead_y_change) % 600'''
  386.  
  387.         lead_x += lead_x_change
  388.         lead_y += lead_y_change
  389.        
  390.        
  391.    
  392.         gameDisplay.fill(gray)
  393.  
  394.         gameDisplay.blit(yum,(randAppleX,randAppleY))
  395.        
  396.        
  397.         snakeHead = []
  398.         snakeHead.append(lead_x)
  399.         snakeHead.append(lead_y)
  400.         snakeList.append(snakeHead)
  401.  
  402.         if len(snakeList)> snakeLength:
  403.             del snakeList [0]
  404.        
  405.         snake(block_size, snakeList)
  406.        
  407.         for eachSegment in snakeList [:-1]:
  408.             if eachSegment == snakeHead:
  409.                 gameOver = True
  410.        
  411.         snake(block_size, snakeList)
  412.  
  413.         score(snakeLength-1)
  414.        
  415.         pygame.display.update()
  416.  
  417.  
  418.  
  419.         if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
  420.             if lead_y > randAppleY and lead_y < randAppleY + AppleThickness or lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  421.                 randAppleX, randAppleY = randAppleGen()
  422.                 snakeLength += 1
  423.  
  424.             elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  425.                 randAppleX, randAppleY = randAppleGen()
  426.                 snakeLength += 1
  427.  
  428.        
  429.  
  430.         clock.tick(FPS)
  431.  
  432.  
  433.  
  434.     pygame.quit()
  435.     quit()
  436. game_intro()
  437. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement