Guest User

Untitled

a guest
Feb 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3.  
  4. import sys
  5. import os
  6. import pygame
  7.  
  8. RIGHT = 1
  9. STOP = 0
  10. LEFT = -1
  11.  
  12. def main():
  13.     screen = pygame.display.set_mode((640, 480))
  14.     farmer = pygame.image.load('farmer.bmp').convert()
  15.     background = pygame.image.load('Background.bmp').convert()
  16.     screen.blit(background, (0, 0)) #draw the background screen
  17.     farmerPosition = pygame.Rect(100,50,75,60)
  18.     screen.blit(farmer, farmerPosition) #draw the farmer
  19.  
  20.     wolf = pygame.image.load('wolf.bmp').convert()
  21.     wolfPosition = pygame.Rect(100,150,75,60)
  22.     screen.blit(wolf, wolfPosition) #draw the wolf
  23.  
  24.     goat = pygame.image.load('goat.bmp').convert()
  25.     goatPosition = pygame.Rect(100,250,75,60)
  26.     screen.blit(goat, goatPosition) #draw the goat
  27.  
  28.     cabbage = pygame.image.load('cabbage.bmp').convert()
  29.     cabbagePosition = pygame.Rect(100,350,75,60)
  30.     screen.blit(cabbage, cabbagePosition) #draw the cabbage
  31.  
  32.  
  33.     pygame.display.update() #display all elements
  34.  
  35. #---------------------------------------------------------------
  36.     movefarmer = LEFT
  37.     movewolf = LEFT
  38.     movegoat = LEFT
  39.     movecabbage = LEFT
  40.     while -1:
  41.        for eventfarmer in pygame.event.get():
  42.             if eventfarmer.type == pygame.QUIT:
  43.                 sys.exit()
  44.             elif eventfarmer.type == pygame.KEYDOWN:
  45.                 # Check for key press
  46.                 if eventfarmer.key == pygame.K_f:
  47.                   # if key pressed is 'f' key, move farmer (check if moving from left to right or other way)
  48.                     if movefarmer == STOP and farmerPosition.left > 300:
  49.                         movefarmer = LEFT
  50.                     elif movefarmer == STOP and farmerPosition.left < 300:
  51.                         movefarmer = RIGHT
  52.  
  53.        screen.blit(background, farmerPosition, farmerPosition) # erase
  54.        if (movefarmer == RIGHT):
  55.            if (farmerPosition.left <= (540 - 60)):
  56.                farmerPosition = farmerPosition.move(5, 0) # move farmer
  57.            else:
  58.                movefarmer = STOP
  59.        if (movefarmer == LEFT):
  60.            if (farmerPosition.left >= (100)):
  61.                farmerPosition = farmerPosition.move(-5, 0) # move farmer
  62.            else:
  63.                movefarmer = STOP
  64.  
  65.        screen.blit(farmer, farmerPosition) # draw new farmer
  66.        pygame.display.update()  # and show it all
  67.        pygame.time.delay(10)  # stop the program for 1/100 second
  68.  
  69. #------------------------------------------------------------------    
  70.        for eventwolf in pygame.event.get():
  71.             if eventwolf.type == pygame.QUIT:
  72.                 sys.exit()
  73.             elif eventwolf.type == pygame.KEYDOWN:
  74.                 # Check for key press
  75.                 if eventwolf.key == pygame.K_w:
  76.                   # if key pressed is 'w' key, move wolf (check if moving from left to right or other way)
  77.                     if movewolf == STOP and wolfPosition.left > 300:
  78.                         movewolf = LEFT
  79.                     elif movewolf == STOP and wolfPosition.left < 300:
  80.                         movewolf = RIGHT
  81.            
  82.        screen.blit(background, wolfPosition, wolfPosition) # erase
  83.        if (movewolf == RIGHT):
  84.            if (wolfPosition.left <= (540 - 60)):
  85.                wolfPosition = wolfPosition.move(5, 0) # move wolf
  86.            else:
  87.                movewolf = STOP
  88.        if (movewolf == LEFT):
  89.            if (wolfPosition.left >= (100)):
  90.                wolfPosition = wolfPosition.move(-5, 0) # move wolf
  91.            else:
  92.                movewolf = STOP
  93.  
  94.        screen.blit(wolf, wolfPosition) # draw new wolf
  95.        pygame.display.update()  # and show it all
  96.        pygame.time.delay(10)  # stop the program for 1/100 second
  97.  
  98. #------------------------------------------------------------------    
  99.        for eventgoat in pygame.event.get():
  100.             if eventgoat.type == pygame.QUIT:
  101.                 sys.exit()
  102.             elif eventgoat.type == pygame.KEYDOWN:
  103.                 # Check for key press
  104.                 if eventgoat.key == pygame.K_g:
  105.                   # if key pressed is 'g' key, move goat (check if moving from left to right or other way)
  106.                     if movegoat == STOP and goatPosition.left > 300:
  107.                         movegoat = LEFT
  108.                     elif movegoat == STOP and goatPosition.left < 300:
  109.                         movegoat = RIGHT
  110.            
  111.        screen.blit(background, goatPosition, goatPosition) # erase
  112.        if (movegoat == RIGHT):
  113.            if (goatPosition.left <= (540 - 60)):
  114.                goatPosition = goatPosition.move(5, 0) # move goat
  115.            else:
  116.                movegoat = STOP
  117.        if (movegoat == LEFT):
  118.            if (goatPosition.left >= (100)):
  119.                goatPosition = goatPosition.move(-5, 0) # move goat
  120.            else:
  121.                movegoat = STOP
  122.  
  123.        screen.blit(goat, goatPosition) # draw new goat
  124.        pygame.display.update()  # and show it all
  125.        pygame.time.delay(10)  # stop the program for 1/100 second
  126.  
  127. #------------------------------------------------------------------    
  128.        for eventcabbage in pygame.event.get():
  129.             if eventcabbage.type == pygame.QUIT:
  130.                 sys.exit()
  131.             elif eventcabbage.type == pygame.KEYDOWN:
  132.                 # Check for key press
  133.                 if eventcabbage.key == pygame.K_c:
  134.                   # if key pressed is 'c' key, move cabbage (check if moving from left to right or other way)
  135.                     if movecabbage == STOP and cabbagePosition.left > 300:
  136.                         movecabbage = LEFT
  137.                     elif movecabbage == STOP and cabbagePosition.left < 300:
  138.                         movecabbage = RIGHT
  139.            
  140.        screen.blit(background, cabbagePosition, cabbagePosition) # erase
  141.        if (movecabbage == RIGHT):
  142.            if (cabbagePosition.left <= (540 - 60)):
  143.                cabbagePosition = cabbagePosition.move(5, 0) # move cabbage
  144.            else:
  145.                movecabbage = STOP
  146.        if (movecabbage == LEFT):
  147.            if (cabbagePosition.left >= (100)):
  148.                cabbagePosition = cabbagePosition.move(-5, 0) # move cabbage
  149.            else:
  150.                movecabbage = STOP
  151.  
  152.        screen.blit(cabbage, cabbagePosition) # draw new cabbage
  153.        pygame.display.update()  # and show it all
  154.        pygame.time.delay(10)  # stop the program for 1/100 second
  155.  
  156.  
  157.  
  158.  
  159. if __name__ == '__main__':
  160.     main()
Add Comment
Please, Sign In to add comment