Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- import sys
- import os
- import pygame
- RIGHT = 1
- STOP = 0
- LEFT = -1
- def main():
- screen = pygame.display.set_mode((640, 480))
- farmer = pygame.image.load('farmer.bmp').convert()
- background = pygame.image.load('Background.bmp').convert()
- screen.blit(background, (0, 0)) #draw the background screen
- farmerPosition = pygame.Rect(100,50,75,60)
- screen.blit(farmer, farmerPosition) #draw the farmer
- wolf = pygame.image.load('wolf.bmp').convert()
- wolfPosition = pygame.Rect(100,150,75,60)
- screen.blit(wolf, wolfPosition) #draw the wolf
- goat = pygame.image.load('goat.bmp').convert()
- goatPosition = pygame.Rect(100,250,75,60)
- screen.blit(goat, goatPosition) #draw the goat
- cabbage = pygame.image.load('cabbage.bmp').convert()
- cabbagePosition = pygame.Rect(100,350,75,60)
- screen.blit(cabbage, cabbagePosition) #draw the cabbage
- pygame.display.update() #display all elements
- #---------------------------------------------------------------
- movefarmer = LEFT
- movewolf = LEFT
- movegoat = LEFT
- movecabbage = LEFT
- while -1:
- for eventfarmer in pygame.event.get():
- if eventfarmer.type == pygame.QUIT:
- sys.exit()
- elif eventfarmer.type == pygame.KEYDOWN:
- # Check for key press
- if eventfarmer.key == pygame.K_f:
- # if key pressed is 'f' key, move farmer (check if moving from left to right or other way)
- if movefarmer == STOP and farmerPosition.left > 300:
- movefarmer = LEFT
- elif movefarmer == STOP and farmerPosition.left < 300:
- movefarmer = RIGHT
- screen.blit(background, farmerPosition, farmerPosition) # erase
- if (movefarmer == RIGHT):
- if (farmerPosition.left <= (540 - 60)):
- farmerPosition = farmerPosition.move(5, 0) # move farmer
- else:
- movefarmer = STOP
- if (movefarmer == LEFT):
- if (farmerPosition.left >= (100)):
- farmerPosition = farmerPosition.move(-5, 0) # move farmer
- else:
- movefarmer = STOP
- screen.blit(farmer, farmerPosition) # draw new farmer
- pygame.display.update() # and show it all
- pygame.time.delay(10) # stop the program for 1/100 second
- #------------------------------------------------------------------
- for eventwolf in pygame.event.get():
- if eventwolf.type == pygame.QUIT:
- sys.exit()
- elif eventwolf.type == pygame.KEYDOWN:
- # Check for key press
- if eventwolf.key == pygame.K_w:
- # if key pressed is 'w' key, move wolf (check if moving from left to right or other way)
- if movewolf == STOP and wolfPosition.left > 300:
- movewolf = LEFT
- elif movewolf == STOP and wolfPosition.left < 300:
- movewolf = RIGHT
- screen.blit(background, wolfPosition, wolfPosition) # erase
- if (movewolf == RIGHT):
- if (wolfPosition.left <= (540 - 60)):
- wolfPosition = wolfPosition.move(5, 0) # move wolf
- else:
- movewolf = STOP
- if (movewolf == LEFT):
- if (wolfPosition.left >= (100)):
- wolfPosition = wolfPosition.move(-5, 0) # move wolf
- else:
- movewolf = STOP
- screen.blit(wolf, wolfPosition) # draw new wolf
- pygame.display.update() # and show it all
- pygame.time.delay(10) # stop the program for 1/100 second
- #------------------------------------------------------------------
- for eventgoat in pygame.event.get():
- if eventgoat.type == pygame.QUIT:
- sys.exit()
- elif eventgoat.type == pygame.KEYDOWN:
- # Check for key press
- if eventgoat.key == pygame.K_g:
- # if key pressed is 'g' key, move goat (check if moving from left to right or other way)
- if movegoat == STOP and goatPosition.left > 300:
- movegoat = LEFT
- elif movegoat == STOP and goatPosition.left < 300:
- movegoat = RIGHT
- screen.blit(background, goatPosition, goatPosition) # erase
- if (movegoat == RIGHT):
- if (goatPosition.left <= (540 - 60)):
- goatPosition = goatPosition.move(5, 0) # move goat
- else:
- movegoat = STOP
- if (movegoat == LEFT):
- if (goatPosition.left >= (100)):
- goatPosition = goatPosition.move(-5, 0) # move goat
- else:
- movegoat = STOP
- screen.blit(goat, goatPosition) # draw new goat
- pygame.display.update() # and show it all
- pygame.time.delay(10) # stop the program for 1/100 second
- #------------------------------------------------------------------
- for eventcabbage in pygame.event.get():
- if eventcabbage.type == pygame.QUIT:
- sys.exit()
- elif eventcabbage.type == pygame.KEYDOWN:
- # Check for key press
- if eventcabbage.key == pygame.K_c:
- # if key pressed is 'c' key, move cabbage (check if moving from left to right or other way)
- if movecabbage == STOP and cabbagePosition.left > 300:
- movecabbage = LEFT
- elif movecabbage == STOP and cabbagePosition.left < 300:
- movecabbage = RIGHT
- screen.blit(background, cabbagePosition, cabbagePosition) # erase
- if (movecabbage == RIGHT):
- if (cabbagePosition.left <= (540 - 60)):
- cabbagePosition = cabbagePosition.move(5, 0) # move cabbage
- else:
- movecabbage = STOP
- if (movecabbage == LEFT):
- if (cabbagePosition.left >= (100)):
- cabbagePosition = cabbagePosition.move(-5, 0) # move cabbage
- else:
- movecabbage = STOP
- screen.blit(cabbage, cabbagePosition) # draw new cabbage
- pygame.display.update() # and show it all
- pygame.time.delay(10) # stop the program for 1/100 second
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment