Advertisement
Guest User

Snake change characters fail

a guest
Apr 23rd, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.49 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.             elif action == "controls":
  182.                 game_intro = False
  183.                 game_controls()
  184.        
  185.             elif action == "play":
  186.                 game_intro = False
  187.                 gameLoop()
  188.             elif action == "main":
  189.                 game_controls = False
  190.                 game_intro()
  191.             elif action == "char":
  192.                 game_char()
  193.             elif action == "mario":
  194.                 currentchar == "mario"
  195.                
  196.             elif currentchar == "mario":
  197.                 charU = pygame.image.load('mario up')
  198.                 charD = pygame.image.load('mario down')
  199.                 charL = pygame.image.load('mario left')
  200.                 charR = pygame.image.load('mario right')
  201.                 gameLoop()
  202.  
  203.  
  204.        
  205.  
  206.                
  207.                
  208.                
  209.            
  210.     else:
  211.         pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
  212.     text_to_button(text,black,x,y,width,height,size)
  213.    
  214.  
  215.  
  216.  
  217.  
  218. def game_char():
  219.     char = True
  220.     while char:
  221.        
  222.         for event in pygame.event.get():
  223.             if event.type == pygame.QUIT:
  224.                 pygame.quit()
  225.                 quit()
  226.        
  227.         gameDisplay.fill(black)      
  228.  
  229.         button("Mario",50,100,150,50,white,gray,"small",action = "goomba")
  230.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  231.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  232.        
  233.  
  234.        
  235.        
  236.         pygame.display.update()
  237.         clock.tick(60)  
  238.  
  239.        
  240.  
  241.  
  242.  
  243. def game_controls():
  244.     gcont = True
  245.     while gcont:
  246.        
  247.         for event in pygame.event.get():
  248.             if event.type == pygame.QUIT:
  249.                 pygame.quit()
  250.                 quit()
  251.        
  252.         gameDisplay.fill(black)      
  253.         gameDisplay.blit(cont,(0,0))
  254.  
  255.         button("Play",150,500,100,50,white,gray,"small",action = "play")
  256.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  257.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  258.  
  259.        
  260.         pygame.display.update()
  261.         clock.tick(60)
  262.  
  263.  
  264.  
  265. def game_intro():
  266.     intro = True
  267.     while intro:
  268.         for event in pygame.event.get():
  269.             if event.type == pygame.QUIT:
  270.                 pygame.quit()
  271.                 quit()
  272.             elif event.type == pygame.KEYDOWN:
  273.                     if event.key == pygame.K_p:
  274.                         intro = False
  275.         gameDisplay.fill(black)
  276.         gameDisplay.blit(imggg,(0,0))
  277.        
  278.    
  279.         button("Play",150,500,100,50,white,gray,"small",action = "play")
  280.         button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
  281.         button("Characters",295,425,200,50,white,gray,"tiny", action = "char")
  282.         button(" ",0,0,0,0,white,gray,"tiny", action = "controls")        
  283.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  284.  
  285.        
  286.  
  287.        
  288.        
  289.                
  290.  
  291.        
  292.         pygame.display.update()
  293.         clock.tick(60)
  294.        
  295.        
  296.        
  297.  
  298.        
  299.  
  300.  
  301.  
  302. def gameLoop():
  303.     global direction
  304.     direction = "left"
  305.     gameExit = False
  306.     gameOver= False        
  307.     lead_y = display_height/2
  308.     lead_x = display_width/2
  309.  
  310.     lead_x_change = -10
  311.     lead_y_change = 0
  312.  
  313.     snakeList = []
  314.     snakeLength = 1
  315.  
  316.     randAppleX, randAppleY = randAppleGen()
  317.    
  318.     while not gameExit:
  319.  
  320.         while gameOver == True:
  321. ##            gameDisplay.blit(loser,(0,0))
  322.             message_to_screen("Game over",
  323.                               red,y_displace=-50,size="large")
  324.            
  325.             message_to_screen("Press P to play again or Q to quit",
  326.                               black,50,size="small")
  327.             pygame.display.update()
  328.            
  329.  
  330.             for event in pygame.event.get():
  331.                 if event.type == pygame.QUIT:
  332.                    
  333.                     gameExit = True
  334.                     gameOver = False
  335.                    
  336.                 if event.type == pygame.KEYDOWN:
  337.                     if event.key == pygame.K_q:
  338.                         gameExit = True
  339.                         gameOver = False
  340.                     if event.key == pygame.K_p:
  341.                         gameLoop()
  342.  
  343.        
  344.         for event in pygame.event.get():
  345.             if event.type == pygame.QUIT:
  346.                         gameExit = True
  347.             if event.type == pygame.KEYDOWN:
  348.                     if event.key == pygame.K_a:
  349.  
  350.                             direction = "left"
  351.                             lead_x_change = -block_size
  352.                             lead_y_change = 0
  353.                     elif event.key == pygame.K_d:
  354.                             direction = "right"
  355.                             lead_x_change = block_size
  356.                             lead_y_change = 0
  357.                     elif event.key == pygame.K_w:
  358.                             direction = "up"
  359.                             lead_y_change = -block_size
  360.                             lead_x_change = 0
  361.                     elif event.key == pygame.K_s:
  362.                             direction = "down"
  363.                             lead_y_change = block_size
  364.                             lead_x_change = 0
  365.                     elif event.key == pygame.K_UP:
  366.                             direction = "up"
  367.                             lead_x_change = 0
  368.                             lead_y_change = -block_size
  369.                     elif event.key == pygame.K_DOWN:
  370.                             direction = "down"
  371.                             lead_y_change = block_size
  372.                             lead_x_change = 0
  373.                     elif event.key == pygame.K_LEFT:
  374.                             direction = "left"
  375.                             lead_x_change = -block_size
  376.                             lead_y_change = 0
  377.                     elif event.key == pygame.K_RIGHT:
  378.                             direction = "right"
  379.                             lead_x_change = block_size
  380.                             lead_y_change = 0
  381.                     elif event.key == pygame.K_ESCAPE:
  382.                         pause()
  383.                        
  384.         if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
  385.             gameOver = True
  386.  
  387.        
  388.         '''lead_x = (lead_x + lead_x_change) % 800 lead_y = (lead_y + lead_y_change) % 600'''
  389.  
  390.         lead_x += lead_x_change
  391.         lead_y += lead_y_change
  392.        
  393.        
  394.    
  395.         gameDisplay.fill(gray)
  396.  
  397.         gameDisplay.blit(yum,(randAppleX,randAppleY))
  398.        
  399.        
  400.         snakeHead = []
  401.         snakeHead.append(lead_x)
  402.         snakeHead.append(lead_y)
  403.         snakeList.append(snakeHead)
  404.  
  405.         if len(snakeList)> snakeLength:
  406.             del snakeList [0]
  407.        
  408.         snake(block_size, snakeList)
  409.        
  410.         for eachSegment in snakeList [:-1]:
  411.             if eachSegment == snakeHead:
  412.                 gameOver = True
  413.        
  414.         snake(block_size, snakeList)
  415.  
  416.         score(snakeLength-1)
  417.        
  418.         pygame.display.update()
  419.  
  420.  
  421.  
  422.         if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
  423.             if lead_y > randAppleY and lead_y < randAppleY + AppleThickness or lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  424.                 randAppleX, randAppleY = randAppleGen()
  425.                 snakeLength += 1
  426.  
  427.             elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  428.                 randAppleX, randAppleY = randAppleGen()
  429.                 snakeLength += 1
  430.  
  431.        
  432.  
  433.         clock.tick(FPS)
  434.  
  435.  
  436.  
  437.     pygame.quit()
  438.     quit()
  439. game_intro()
  440. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement