Guest User

Untitled

a guest
Feb 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 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.     while -1:
  39.        for eventfarmer in pygame.event.get():
  40.             if eventfarmer.type == pygame.QUIT:
  41.                 sys.exit()
  42.             elif eventfarmer.type == pygame.KEYDOWN:
  43.                 # Check for key press
  44.                 if eventfarmer.key == pygame.K_f:
  45.                   # if key pressed is 'f' key, move farmer (check if moving from left to right or other way)
  46.                     if movefarmer == STOP and farmerPosition.left > 300:
  47.                         movefarmer = LEFT
  48.                     elif movefarmer == STOP and farmerPosition.left < 300:
  49.                         movefarmer = RIGHT
  50.  
  51.        screen.blit(background, farmerPosition, farmerPosition) # erase
  52.        if (movefarmer == RIGHT):
  53.            if (farmerPosition.left <= (540 - 60)):
  54.                farmerPosition = farmerPosition.move(5, 0) # move farmer
  55.            else:
  56.                movefarmer = STOP
  57.        if (movefarmer == LEFT):
  58.            if (farmerPosition.left >= (100)):
  59.                farmerPosition = farmerPosition.move(-5, 0) # move farmer
  60.            else:
  61.                movefarmer = STOP
  62.        screen.blit(farmer, farmerPosition) # draw new farmer
  63.        pygame.display.update()  # and show it all
  64.        pygame.time.delay(10)  # stop the program for 1/100 second
  65.  
  66.    
  67.        for eventwolf in pygame.event.get():
  68.             if eventwolf.type == pygame.QUIT:
  69.                 sys.exit()
  70.             elif eventwolf.type == pygame.KEYDOWN:
  71.                 # Check for key press
  72.                 if eventwolf.key == pygame.K_w:
  73.                   # if key pressed is 'w' key, move farmer (check if moving from left to right or other way)
  74.                     if movewolf == STOP and wolfPosition.left > 300:
  75.                         movewolf = LEFT
  76.                     elif movewolf == STOP and wolfPosition.left < 300:
  77.                         movewolf = RIGHT
  78.            
  79.        screen.blit(background, wolfPosition, wolfPosition) # erase
  80.        if (movewolf == RIGHT):
  81.            if (wolfPosition.left <= (540 - 60)):
  82.                wolfPosition = wolfPosition.move(5, 0) # move farmer
  83.            else:
  84.                movewolf = STOP
  85.        if (movewolf == LEFT):
  86.            if (wolfPosition.left >= (100)):
  87.                wolfPosition = wolfPosition.move(-5, 0) # move farmer
  88.            else:
  89.                movewolf = STOP
  90.        screen.blit(wolf, wolfPosition) # draw new farmer
  91.        pygame.display.update()  # and show it all
  92.        pygame.time.delay(10)  # stop the program for 1/100 second
  93.  
  94.  
  95.  
  96.  
  97.  
  98. if __name__ == '__main__':
  99.     main()
Add Comment
Please, Sign In to add comment