Advertisement
Guest User

This Is a Farming Game

a guest
Apr 22nd, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.09 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.  
  46. textx = 750/2
  47. texty = 600
  48.  
  49. dialoguetime = 10000
  50.  
  51. white = (255,255,255)
  52. black = (0,0,0)
  53.  
  54. # Initialise Pygame
  55. pygame.init()
  56.  
  57. # Background song
  58. # Main song to use - Watering a flower Haruomi Hosono 1984 cassette
  59. bgsong = "backgroundsong.mp3"
  60. pygame.mixer.init()
  61. pygame.mixer.music.load(bgsong)
  62. pygame.mixer.music.play()
  63. pygame.event.wait()
  64.  
  65. # Initialise fonts
  66. pygame.font.init()
  67. pygame.font.SysFont("comicsans.ttf",11,False,False,None)
  68.  
  69. # Create window for game
  70. screen = pygame.display.set_mode((750, 750))
  71. pygame.display.flip()
  72. run = True
  73.  
  74. # Classes
  75. def dialogueWait():
  76.     pygame.time.wait(dialoguetime)  # Insert amount of time
  77.     pygame.draw.rect(screen,black,(890,215,textx,texty))
  78.  
  79.  
  80. def invalidInput():
  81.     invalidinputText = font.render("Please try again and enter a valid number.",True,(white),False)
  82.     screen.blit(invalidinputText,(textx,texty))
  83.     dialogueWait()
  84. ##
  85. ##
  86. class Plant:
  87.     # A generic food crop.
  88.  
  89.     # Constructor
  90.     def __init__(self, plType, plWaterNeeded, plgrowthRate, plStatus, plHarvested):
  91.         self.type = plType
  92.         self.waterNeeded = plWaterNeeded
  93.         self.growthRate = 0
  94.         self.status = plStatus
  95.         self.harvested = plHarvested
  96.  
  97.     def updateStaus(self):
  98.         if self.growth > 15:
  99.             self.status = "Old"
  100.         elif self.growth > 10:
  101.             self.status = "Mature"
  102.         elif self.growth > 5:
  103.             self.status = "Young"
  104.         elif self.growth > 0:
  105.             self.status = "Seedling"
  106.  
  107.     def grow(self, water):
  108.         if water >= self.waterNeeded:
  109.             self.growth += self.growthRate
  110.  
  111.     def interactPlant(self):
  112.         interactscreen = font.render("What will you do? Press 1 to water, or 2 to harvest.",True,(white),False)
  113.         screen.blit(interactscreen,(textx,texty))
  114.         dialogueWait()
  115.         if pressedOne == True:
  116.             intOnePlant = font.render("You water the plant.",True,(white),False)
  117.             screen.blit(intOnePlant,(textx,texty))
  118.             dialogueWait()
  119.             water = water + 1
  120.         if pressedTwo == True:
  121.             intTwoPlant = font.render("You harvest the plant.",True,(white),False)
  122.             screen.blit(intTwoPlant,(textx,texty))
  123.             dialogueWait()
  124.             pygame.sprite.remove()
  125.             self.harvested = True
  126.         else:
  127.             invalidInput()
  128.  
  129.     def inspectPlant(self):
  130.         inspectscreen = font.render("You look at the plant. Press 1 for details about the plant, or 2 to know what it may need.",True,(white),False)
  131.         screen.blit(inspectscreen,(textx,texty))
  132.         dialogueWait()
  133.         if pressedOne == True:
  134.             inspOnePlant = font.render("The plant is a "+str.self.name+". It is "+str.self.status+".",True,(white),False)
  135.             screen.blit(inspOnePlant,(textx,texty))
  136.             dialogueWait()
  137.         elif pressedTwo == True:
  138.             inspTwoPlant = font.render("Currently, the plant's need for water is "+str.self.waterNeeded+".",True,(white),False)
  139.             screen.blit(inspTwoPlant,(textx,texty))
  140.             dialogueWait()
  141.         else:
  142.             invalidInput()
  143.  
  144. ###
  145. ###
  146.  
  147. class Animal:
  148.     # A generic animal crop.
  149.  
  150.     # Constructor
  151.     def __init__(self, anName, anType, anFoodNeeded, anHappiness, anStatus, anHarvested):
  152.         self.name = anName
  153.         self.type = anType
  154.         self.foodNeeded = anFoodNeeded
  155.         self.happiness = 0
  156.         self.status = anStatus
  157.         self.harvested = anHarvested
  158.  
  159.     def updateStatus(self):
  160.         if self.growth == 15:
  161.             self.status = "Adult"
  162.         elif self.growth > 10:
  163.             self.status = "Young"
  164.         elif self.growth < 5:
  165.             self.status = "Baby"
  166.  
  167.     def happinessLevel(self):
  168.         if self.animalmood == 15:
  169.             self.happiness = "Happy"
  170.         elif self.animalmood > 10:
  171.             self.happiness = "Content"
  172.         elif self.animalmood > 5:
  173.             self.happiness = "Displeased"
  174.         elif self.animalmood < 5:
  175.             self.happiness = "Miserable"
  176.  
  177.     def inspectAnimal(self):
  178.         inspectscreenAnimal = font.render("The creature stares at you. Press 1 for details about the animal, or 2 to know what it may need.",True,(white),False)
  179.         screen.blit(lookatText,(textx,texty))
  180.         dialogueWait()
  181.         if pressedOne == True:
  182.             inspOneAnimal = font.render("The animal is a "+str.self.name+". It is "+str.self.status+".",True,(white),False)
  183.             screen.blit(inspOneAnimal,(textx,texty))
  184.             dialogueWait()
  185.         elif pressedTwo == True:
  186.             inspTwoAnimal = font.render("The animal's level of hunger is "+str.self.foodNeeded+". It appears to be "+str.self.happiness+".",True,(white),False)
  187.             screen.blit(inspTwoanimal,(textx,texty))
  188.             dialogueWait()
  189.         else:
  190.             invalidInput()
  191.  
  192.     def interactAnimal(self):
  193.         interactscreen = font.render("What will you do? Press 1 to feed, or 2 to harvest.", True, (white), False)
  194.         screen.blit(interactscreen, (textx,texty))
  195.         dialogueWait()
  196.         if pressedOne == True:
  197.             intOneAnimal = font.render("You feed the "+str.self.name+".", True, (white), False)
  198.             screen.blit(intOneAnimal, (textx,texty))
  199.             dialogueWait()
  200.             foodNeeded = foodNeeded + 1
  201.         if pressedTwo == True:
  202.             self.harvested = False
  203.             intTwoAnimal = font.render("You harvest the "+str.self.name+".", True, (white), False)
  204.             screen.blit(intTwoAnimal, (textx,texty))
  205.             dialogueWait()
  206.             pygame.sprite.remove()
  207.             self.harvested = True
  208.         else:
  209.             invalidInput()
  210.  
  211.  
  212.     def grow(self, food):
  213.         if food >= self.foodNeeded:
  214.             self.growth += self.happiness
  215.  
  216. ###
  217. ###
  218.  
  219. class NPC:
  220.     # A generic NPC.
  221.  
  222.     # Constructor
  223.     def __init__(self, npcName, npcFriendLevel, npcDialogueFile):
  224.         self.name = npcName
  225.         self.friendlevel = npcFriendLevel
  226.         self.dialoguetxt = npcDialogueFile
  227.  
  228.     def npcFriendLevel(self,friend):
  229.         if self.friendlevel == 15:
  230.             self.friend = "Best friend, somehow"
  231.         elif self.friendlevel > 10:
  232.             self.friend = "Friend"
  233.         elif self.friendlevel > 5:
  234.             self.friend = "Acquaintance"
  235.         elif self.friendlevel < 5:
  236.             self.friend = "Stranger"
  237.  
  238.     def npcMoods(self, mood):
  239.         moodList = ["happy","annoyed","tired","ashamed","excited","joyous","peckish"]
  240.         mood = random.choice(moodList) # Picks a random item from moodList.
  241.  
  242.     def inspectNPC(self):
  243.         inspectscreenNPC = font.render("You notice "+str.self.name+". Press 1 to re-evaluate how close you are to them, or 2 to see their mood.",True,(255,255,255))
  244.         screen.blit(inspectscreenNPC,(textx,texty))
  245.         dialogueWait()
  246.         if pressedOne == True:
  247.             inspOneNPC = font.render(str.self.name+" would certainly consider you a "+str.self.friendlevel+".",True,(255,255,255))
  248.             screen.blit(inspOneNPC,(textx,texty))
  249.             dialogueWait()
  250.         if pressedTwo == True:
  251.             inspTwoNPC = font.render(str.self.name+" seems quite "+str.self.mood+".",True,(255,255,255))
  252.             screen.blit(inspTwoNPC,(textx,texty))
  253.             dialogueWait()
  254.         else:
  255.             invalidInput()
  256.  
  257.     def interactNPC(self):
  258.         interactscreenNPC = font.render("You see "+str.self.name+". Press 1 if you wish to talk to them.",True,(255,255,255))
  259.         screen.blit(interactscreenNPC,textx,texty)
  260.         dialogueWait()
  261.         if pressedOne == True:
  262.             textfile = self.dialoguetxt  # In instantiation, use .txt at the end of a file name.
  263.             f = open(textfile, "r") # Opens text file in 'read'
  264.             lines = textfile.read().split(",") # Imports each line into an array. Splits them with a ','.
  265.             dialogueline = random.choice(lines) # Selects random item from lines.
  266.             intOneNPC = font.render(str.self.name+" says: "+str.dialogueline,True,(255,255,255))
  267.             screen.blit(intOneNPC,(textx,texty))
  268.             dialogueWait()
  269.             textfile.close()
  270.             self.friendlevel = self.friendlevel + 1
  271.         else:
  272.             invalidInput()
  273.  
  274. # Load images
  275. # Player image
  276. playerImage = pygame.image.load("idle1.png") # Loads file for player image
  277. playerImage = pygame.transform.scale(playerImage, (40,60)) # 40x60 sprite
  278. playerImage = playerImage.convert_alpha()
  279.  
  280. # Background for day
  281. backgroundImage = pygame.image.load("protobackgroundD.png")  # Protoype background will be changed during other iterations
  282. backgroundImage = pygame.transform.scale(backgroundImage, (750, 750))
  283. screen.blit(backgroundImage, (0,0))
  284. # Background for night
  285. backgroundImageN = pygame.image.load("protobackgroundN.png")
  286. backgroundImageN = pygame.transform.scale(backgroundImageN, (750, 750))
  287.  
  288. # Plant sprites
  289. # Instantiation - Plant("Type",water needed/int,growthrate/int,"Status")
  290. plantimageOne = Plant("Taproot",5,3,"Seedling",False)
  291. plantimageOne = pygame.image.load("plant1.png")
  292. plantimageOne = pygame.transform.scale(plantimageOne, (30, 30))
  293.  
  294. plantimageTwo = Plant("Witchrot",3,2,"Seedling",False)
  295. plantimageTwo = pygame.image.load("plant2.png")
  296. plantimageTwo = pygame.transform.scale(plantimageTwo, (30,30))
  297.  
  298. # Animal sprites
  299. # Instnatiation - Animal("Name","Type",food needed/int,happiness/int,"Status")
  300. aniamalOne = Animal("Christopher","Amyglamate",10,4,"Baby",False)
  301. animalOne = pygame.image.load("animal1.png")
  302. animalOne = pygame.transform.scale(animalOne, (50, 50))
  303.  
  304. animalTwo = Animal("Dony","Rowe",5,3,"Adult",False)
  305. animalTwo = pygame.image.load("animal2.png")
  306. animalTwo = pygame.transform.scale(animalTwo, (50,50))
  307.  
  308. # NPC sprites
  309. #npcOne - Limbo
  310. npcOne = NPC("Limbo",5,"npc1text.txt")
  311. npcOne = pygame.image.load("npc1.png")
  312. npcOne = pygame.transform.scale(npcOne, (40,60))
  313.  
  314. #npcTwo - Handy
  315. npcTwo = NPC("Handy,",10,"npc2text.txt")
  316. npcTwo = pygame.image.load("npc2.png")
  317. npcTwo = pygame.transform.scale(npcTwo, (40,60))
  318.  
  319.  
  320. # Run game
  321. frame = pygame.time.Clock()
  322. while run:
  323.     # This code does not process whilst game is not running
  324.     pygame.time.delay(50)
  325.     for event in pygame.event.get():
  326.         if event.type == pygame.QUIT:  # If someone presses the X to close the game, this code ensures that the game closes.
  327.             run = False  # Run is set to false. No code will be run once this is set to false.
  328.  
  329.         # In-game Clock
  330.         inGameDays = 0
  331.         seconds = 0
  332.  
  333.         def gameTime(seconds):
  334.             day = True
  335.             night = False
  336.             start = time.time()  # Returns number of seconds since unix epoch (aka 00:00:00)
  337.             time.perf_counter()  # Counts process time
  338.             elapsed = 0  # Elapsed time
  339.  
  340.             while elapsed > seconds:  # Already running whilst the program is running
  341.                 elapsed = time.time() - start
  342.                 time.sleep(1)
  343.         # Update screen for day version of map.
  344.  
  345. # Controls
  346. # Event loop
  347.         pressedDown = None
  348.         pressedLeft = None
  349.         pressedRight = None
  350.         pressedUp = None
  351.         nearPlant = False
  352.         nearAnimal = False
  353.         nearNPC = False
  354.  
  355.  
  356.         # Checks collision between player and item
  357.         def checkCollision(x,y,planty,plantx,plantyB, plantxB):
  358.             # \/ Checks collision for plants \/
  359.             nearPlant = False
  360.             nearAnimal = False
  361.             nearNPC = False
  362.  
  363.             if y >= planty and y <= planty + 10:
  364.                 if x >= plantx and x <= plantx + 10:
  365.                     nearPlant = True
  366.                     return nearPlant
  367.             if y >= plantyB and y <= plantyB + 10:
  368.                 if x >= plantxB and x <= plantxB + 10:
  369.                     nearPlant = Trues
  370.                     return nearPlant
  371.             # \/ Checks collision for animals \/
  372.             if y >= animaly and y <= animaly + 10:
  373.                 if x >= animalx and x <= animalx + 10:
  374.                     nearAnimal = True
  375.                     return nearAnimal
  376.             if y >= animalyB and y <= animalyB + 10:
  377.                 if x >= animalxB and x <= animalxB + 10:
  378.                     nearAnimal = True
  379.                     return nearAnimal
  380.             # \/ Checks collision for NPCs \/
  381.             if y >= npc1y and y <= npc1y + 10:
  382.                 if x >= npc1x and x <= npc1x + 10:
  383.                     nearNPC = True
  384.                     return nearNPC
  385.             if y >= npc2y and y <= npc2y + 10:
  386.                 if x >= npc2x and x <= npc2x + 10:
  387.                     nearNPC = True
  388.                     return nearNPC
  389.  
  390.         # True - Key is pressed
  391.         keys = pygame.key.get_pressed()
  392.         if event.type == pygame.KEYDOWN:
  393.             if event.key == pygame.K_a:
  394.                 pressedLeft = True
  395.             if event.key == pygame.K_d:
  396.                 pressedRight = True
  397.             if event.key == pygame.K_w:
  398.                 pressedUp = True
  399.             if event.key == pygame.K_s:
  400.                 pressedDown = True
  401.             if event.key == pygame.K_e:
  402.                 pressedE = True
  403.             if event.key == pygame.K_f:
  404.                 pressedF = True
  405.             if event.key == pygame.K_1:
  406.                 pressedOne = True
  407.             if event.key == pygame.K_2:
  408.                 pressedTwo = True
  409.         # False - Key is no longer pressed
  410.         elif event.type == pygame.KEYUP:
  411.             if event.key == pygame.K_a:
  412.                 pressedLeft = False
  413.             if event.key == pygame.K_d:
  414.                 pressedRight = False
  415.             if event.key == pygame.K_w:
  416.                 pressedUp = False
  417.             if event.key == pygame.K_s:
  418.                 pressedDown = False
  419.             if event.key == pygame.K_e:
  420.                 pressedE = False
  421.             if event.key == pygame.K_f:
  422.                 pressedF = False
  423.             if event.key == pygame.K_1:
  424.                 pressedOne = False
  425.             if event.key == pygame.K_2:
  426.                 pressedTwo = False
  427.  
  428.  
  429.         # Sprite movement
  430.         movingRightA1 = True
  431.         movingDownA2 = True
  432.         movingRightN1 = True
  433.         movingLeftN2 = True
  434.  
  435.         # Animal one
  436.         if animalx >= 400:
  437.             movingRightA1 = True
  438.         elif animalx <= 550:
  439.             movingRightA1 = False
  440.         if movingRightA1 == True:
  441.             animalx += 6
  442.         else:
  443.             animalx -= 6
  444.  
  445.         # Animal two
  446.         if animalyB >= 350:
  447.             movingDownA2 = True
  448.         elif animalyB <= 150:
  449.             movingDownA2 = True
  450.         if movingDownA2 == True:
  451.             animalyB -= 4
  452.         else:
  453.             animalyB += 4
  454.  
  455.         # NPC one
  456.         if npc1x >= 100:
  457.             movingRightN1 = True
  458.         elif npc1x <= 175:
  459.             movingRightN1 = False
  460.         if movingRightN1 == True:
  461.             npc1x += 5
  462.         else:
  463.             npc1x -= 5
  464.  
  465.         # NPC two
  466.         if npc2x >= 375:
  467.             movingLeftN2 = True
  468.         elif npc2x <= 300:
  469.             movingLeftN2 = False
  470.         if movingLeftN2 == True:
  471.             npc2x -= 6
  472.         else:
  473.             npc2x += 6
  474.  
  475.     # Character Movement
  476.     if pressedLeft == True:
  477.         x -= vel
  478.     elif pressedRight == True:
  479.         x += vel
  480.     elif pressedUp == True:
  481.         y -= vel
  482.     elif pressedDown == True:
  483.         y += vel
  484.  
  485.  
  486. # Update screen with sprites
  487.     gameTime(seconds)
  488.     screen.blit(backgroundImage, (0, 0))
  489.     screen.blit(playerImage, (x, y))
  490.     screen.blit(plantimageOne, (plantx, planty))
  491.     screen.blit(animalOne, (animalx, animaly))
  492.     screen.blit(plantimageTwo, (plantxB, plantyB))
  493.     screen.blit(animalTwo, (animalxB, animalyB))
  494.     screen.blit(npcOne, (npc1x, npc1y))
  495.     screen.blit(npcTwo, (npc2x, npc2y))
  496. # Interactions
  497.     if nearNPC == True:
  498.         if pressedE == True:
  499.             NPC.inspectnpc()
  500.         elif pressedF == True:
  501.             NPC.interactnpc()
  502.  
  503.     # Animal interactions
  504.     if nearAnimal == True:
  505.         if pressedE == True:
  506.             Animal.inspectAnimal()
  507.         elif pressedF == True:
  508.             Animal.interactAnimal()
  509.  
  510.     # Plant interactions
  511.     if nearPlant == True:
  512.         if pressedE == True:
  513.             Plant.inspectPlant()
  514.         elif pressedF == True:
  515.             Plant.interactPlant()
  516.  
  517.     pygame.display.update()
  518.     frame.tick(30)
  519.  
  520. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement