Advertisement
Guest User

farminggame.py

a guest
Apr 23rd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.36 KB | None | 0 0
  1. # -------------------------------------------------------------------------------
  2. # Name:        This Is a Farming Game
  3. # Purpose:     First prototype of TIaFG.
  4. #
  5. # Author:      Emily
  6. #
  7. # Created:     06/11/2018
  8. # Copyright:   (c) Emily 2018
  9. # -------------------------------------------------------------------------------
  10. # Import needed modules
  11. import pygame
  12. import time
  13. import random
  14.  
  15. # Import classes for plants, animals, and npcs
  16. from gameclasses import Plant
  17. from gameclasses import Animal
  18. from gameclasses import NPC
  19.  
  20. # Player coordinates, speed
  21. x = 50
  22. y = 50
  23. width = 40
  24. height = 60
  25. vel = 7 # Velocity - speed
  26.  
  27. # Plant + Animal coordinates
  28. plantx = 250
  29. planty = 250
  30.  
  31. animalx = 400
  32. animaly = 400
  33.  
  34. plantxB = 150
  35. plantyB = 150
  36.  
  37. animalxB = 350
  38. animalyB = 350
  39.  
  40. npc1x = 100
  41. npc1y = 50
  42.  
  43. npc2x = 375
  44. npc2y = 100
  45. # Initialise Pygame
  46. pygame.init()
  47.  
  48. # Background song
  49. # Main song to use - Watering a flower Haruomi Hosono 1984 cassette
  50. bgsong = "backgroundsong.mp3"
  51. pygame.mixer.init()
  52. pygame.mixer.music.load(bgsong)
  53. pygame.mixer.music.play(-1)
  54. pygame.event.wait()
  55.  
  56. # Initialise fonts
  57. pygame.font.init()
  58. pygame.font.SysFont("comicsans.ttf",11,False,False,None)
  59.  
  60. # Create window for game
  61. screen = pygame.display.set_mode((750, 750))
  62. pygame.display.flip()
  63. run = True
  64.  
  65. # Load images
  66. # Player image
  67. playerImage = pygame.image.load("idle1.png") # Loads file for player image
  68. playerImage = pygame.transform.scale(playerImage, (40,60)) # 40x60 sprite
  69. playerImage = playerImage.convert_alpha()
  70.  
  71. # Background for day
  72. backgroundImage = pygame.image.load("protobackgroundD.png")  # Protoype background will be changed during other iterations
  73. backgroundImage = pygame.transform.scale(backgroundImage, (750, 750))
  74. screen.blit(backgroundImage, (0,0))
  75. # Background for night
  76. backgroundImageN = pygame.image.load("protobackgroundN.png")
  77. backgroundImageN = pygame.transform.scale(backgroundImageN, (750, 750))
  78.  
  79. # Plant sprites
  80. # Instantiation - Plant("Type",water needed/int,growthrate/int,"Status")
  81. plantimageOne = Plant("Taproot",5,3,"Seedling",False)
  82. plantimageOne = pygame.image.load("plant1.png")
  83. plantimageOne = pygame.transform.scale(plantimageOne, (30, 30))
  84.  
  85. plantimageTwo = Plant("Witchrot",3,2,"Seedling",False)
  86. plantimageTwo = pygame.image.load("plant2.png")
  87. plantimageTwo = pygame.transform.scale(plantimageTwo, (30,30))
  88.  
  89. # Animal sprites
  90. # Instnatiation - Animal("Name","Type",food needed/int,happiness/int,"Status")
  91. aniamalOne = Animal("Christopher","Amyglamate",10,4,"Baby",False)
  92. animalOne = pygame.image.load("animal1.png")
  93. animalOne = pygame.transform.scale(animalOne, (50, 50))
  94.  
  95. animalTwo = Animal("Dony","Rowe",5,3,"Adult",False)
  96. animalTwo = pygame.image.load("animal2.png")
  97. animalTwo = pygame.transform.scale(animalTwo, (50,50))
  98.  
  99. # NPC sprites
  100. #npcOne - Limbo
  101. npcOne = NPC("Limbo",5,"npc1text.txt")
  102. npcOne = pygame.image.load("npc1.png")
  103. npcOne = pygame.transform.scale(npcOne, (40,60))
  104.  
  105. #npcTwo - Handy
  106. npcTwo = NPC("Handy,",10,"npc2text.txt")
  107. npcTwo = pygame.image.load("npc2.png")
  108. npcTwo = pygame.transform.scale(npcTwo, (40,60))
  109.  
  110.  
  111. # Run game
  112. frame = pygame.time.Clock()
  113. while run:
  114.     # This code does not process whilst game is not running
  115.     pygame.time.delay(50)
  116.     for event in pygame.event.get():
  117.         if event.type == pygame.QUIT:  # If someone presses the X to close the game, this code ensures that the game closes.
  118.             run = False  # Run is set to false. No code will be run once this is set to false.
  119.  
  120.         # In-game Clock
  121.         inGameDays = 0
  122.         seconds = 0
  123.  
  124.         def gameTime(seconds):
  125.             day = True
  126.             night = False
  127.             start = time.time()  # Returns number of seconds since unix epoch (aka 00:00:00)
  128.             time.perf_counter()  # Counts process time
  129.             elapsed = 0  # Elapsed time
  130.  
  131.             while elapsed > seconds:  # Already running whilst the program is running
  132.                 elapsed = time.time() - start
  133.                 time.sleep(1)
  134.         # Update screen for day version of map.
  135.  
  136. # Controls
  137. # Event loop
  138.         pressedDown = None
  139.         pressedLeft = None
  140.         pressedRight = None
  141.         pressedUp = None
  142.         nearPlant = False
  143.         nearAnimal = False
  144.         nearNPC = False
  145.  
  146.  
  147.         # Checks collision between player and item
  148.         def checkCollision(x,y,planty,plantx,plantyB, plantxB):
  149.             # \/ Checks collision for plants \/
  150.             nearPlant = False
  151.             nearAnimal = False
  152.             nearNPC = False
  153.  
  154.             if y >= planty and y <= planty + 10:
  155.                 if x >= plantx and x <= plantx + 10:
  156.                     nearPlant = True
  157.                     return nearPlant
  158.             if y >= plantyB and y <= plantyB + 10:
  159.                 if x >= plantxB and x <= plantxB + 10:
  160.                     nearPlant = Trues
  161.                     return nearPlant
  162.             # \/ Checks collision for animals \/
  163.             if y >= animaly and y <= animaly + 10:
  164.                 if x >= animalx and x <= animalx + 10:
  165.                     nearAnimal = True
  166.                     return nearAnimal
  167.             if y >= animalyB and y <= animalyB + 10:
  168.                 if x >= animalxB and x <= animalxB + 10:
  169.                     nearAnimal = True
  170.                     return nearAnimal
  171.             # \/ Checks collision for NPCs \/
  172.             if y >= npc1y and y <= npc1y + 10:
  173.                 if x >= npc1x and x <= npc1x + 10:
  174.                     nearNPC = True
  175.                     return nearNPC
  176.             if y >= npc2y and y <= npc2y + 10:
  177.                 if x >= npc2x and x <= npc2x + 10:
  178.                     nearNPC = True
  179.                     return nearNPC
  180.  
  181.         # True - Key is pressed
  182.         keys = pygame.key.get_pressed()
  183.         if event.type == pygame.KEYDOWN:
  184.             if event.key == pygame.K_a:
  185.                 pressedLeft = True
  186.             if event.key == pygame.K_d:
  187.                 pressedRight = True
  188.             if event.key == pygame.K_w:
  189.                 pressedUp = True
  190.             if event.key == pygame.K_s:
  191.                 pressedDown = True
  192.             if event.key == pygame.K_e:
  193.                 pressedE = True
  194.             if event.key == pygame.K_f:
  195.                 pressedF = True
  196.             if event.key == pygame.K_1:
  197.                 pressedOne = True
  198.             if event.key == pygame.K_2:
  199.                 pressedTwo = True
  200.         # False - Key is no longer pressed
  201.         elif event.type == pygame.KEYUP:
  202.             if event.key == pygame.K_a:
  203.                 pressedLeft = False
  204.             if event.key == pygame.K_d:
  205.                 pressedRight = False
  206.             if event.key == pygame.K_w:
  207.                 pressedUp = False
  208.             if event.key == pygame.K_s:
  209.                 pressedDown = False
  210.             if event.key == pygame.K_e:
  211.                 pressedE = False
  212.             if event.key == pygame.K_f:
  213.                 pressedF = False
  214.             if event.key == pygame.K_1:
  215.                 pressedOne = False
  216.             if event.key == pygame.K_2:
  217.                 pressedTwo = False
  218.  
  219.  
  220.         # Sprite movement
  221.         movingRightA1 = True
  222.         movingDownA2 = True
  223.         movingRightN1 = True
  224.         movingLeftN2 = True
  225.  
  226.         # Animal one
  227.         if animalx >= 400:
  228.             movingRightA1 = True
  229.         elif animalx <= 550:
  230.             movingRightA1 = False
  231.         if movingRightA1 == True:
  232.             animalx += 6
  233.         else:
  234.             animalx -= 6
  235.  
  236.         # Animal two
  237.         if animalyB >= 350:
  238.             movingDownA2 = True
  239.         elif animalyB <= 150:
  240.             movingDownA2 = True
  241.         if movingDownA2 == True:
  242.             animalyB -= 4
  243.         else:
  244.             animalyB += 4
  245.  
  246.         # NPC one
  247.         if npc1x >= 100:
  248.             movingRightN1 = True
  249.         elif npc1x <= 175:
  250.             movingRightN1 = False
  251.         if movingRightN1 == True:
  252.             npc1x += 5
  253.         else:
  254.             npc1x -= 5
  255.  
  256.         # NPC two
  257.         if npc2x >= 375:
  258.             movingLeftN2 = True
  259.         elif npc2x <= 300:
  260.             movingLeftN2 = False
  261.         if movingLeftN2 == True:
  262.             npc2x -= 6
  263.         else:
  264.             npc2x += 6
  265.  
  266.     # Character Movement
  267.     if pressedLeft == True:
  268.         x -= vel
  269.     elif pressedRight == True:
  270.         x += vel
  271.     elif pressedUp == True:
  272.         y -= vel
  273.     elif pressedDown == True:
  274.         y += vel
  275.  
  276.  
  277. # Update screen with sprites
  278.     gameTime(seconds)
  279.     screen.blit(backgroundImage, (0, 0))
  280.     screen.blit(playerImage, (x, y))
  281.     screen.blit(plantimageOne, (plantx, planty))
  282.     screen.blit(animalOne, (animalx, animaly))
  283.     screen.blit(plantimageTwo, (plantxB, plantyB))
  284.     screen.blit(animalTwo, (animalxB, animalyB))
  285.     screen.blit(npcOne, (npc1x, npc1y))
  286.     screen.blit(npcTwo, (npc2x, npc2y))
  287. # Interactions
  288.     if nearNPC == True:
  289.         if pressedE == True:
  290.             NPC.inspectnpc()
  291.         elif pressedF == True:
  292.             NPC.interactnpc()
  293.  
  294.     # Animal interactions
  295.     if nearAnimal == True:
  296.         if pressedE == True:
  297.             Animal.inspectAnimal()
  298.         elif pressedF == True:
  299.             Animal.interactAnimal()
  300.  
  301.     # Plant interactions
  302.     if nearPlant == True:
  303.         if pressedE == True:
  304.             Plant.inspectPlant()
  305.         elif pressedF == True:
  306.             Plant.interactPlant()
  307.  
  308.     pygame.display.update()
  309.     frame.tick(30)
  310.  
  311. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement