Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -------------------------------------------------------------------------------
- # Name: This Is a Farming Game
- # Purpose: First prototype of TIaFG.
- #
- # Author: Emily
- #
- # Created: 06/11/2018
- # Copyright: (c) Emily 2018
- # -------------------------------------------------------------------------------
- # Import needed modules
- import pygame
- import time
- import random
- # Import classes for plants, animals, and npcs
- from gameclasses import Plant
- from gameclasses import Animal
- from gameclasses import NPC
- # Player coordinates, speed
- x = 50
- y = 50
- width = 40
- height = 60
- vel = 7 # Velocity - speed
- # Plant + Animal coordinates
- plantx = 250
- planty = 250
- animalx = 400
- animaly = 400
- plantxB = 150
- plantyB = 150
- animalxB = 350
- animalyB = 350
- npc1x = 100
- npc1y = 50
- npc2x = 375
- npc2y = 100
- textx = 750/2
- texty = 600
- dialoguetime = 10000
- white = (255,255,255)
- black = (0,0,0)
- # Initialise Pygame
- pygame.init()
- # Background song
- # Main song to use - Watering a flower Haruomi Hosono 1984 cassette
- bgsong = "backgroundsong.mp3"
- pygame.mixer.init()
- pygame.mixer.music.load(bgsong)
- pygame.mixer.music.play()
- pygame.event.wait()
- # Initialise fonts
- pygame.font.init()
- pygame.font.SysFont("comicsans.ttf",11,False,False,None)
- # Create window for game
- screen = pygame.display.set_mode((750, 750))
- pygame.display.flip()
- run = True
- # Classes
- def dialogueWait():
- pygame.time.wait(dialoguetime) # Insert amount of time
- pygame.draw.rect(screen,black,(890,215,textx,texty))
- def invalidInput():
- invalidinputText = font.render("Please try again and enter a valid number.",True,(white),False)
- screen.blit(invalidinputText,(textx,texty))
- dialogueWait()
- ##
- ##
- class Plant:
- # A generic food crop.
- # Constructor
- def __init__(self, plType, plWaterNeeded, plgrowthRate, plStatus, plHarvested):
- self.type = plType
- self.waterNeeded = plWaterNeeded
- self.growthRate = 0
- self.status = plStatus
- self.harvested = plHarvested
- def updateStaus(self):
- if self.growth > 15:
- self.status = "Old"
- elif self.growth > 10:
- self.status = "Mature"
- elif self.growth > 5:
- self.status = "Young"
- elif self.growth > 0:
- self.status = "Seedling"
- def grow(self, water):
- if water >= self.waterNeeded:
- self.growth += self.growthRate
- def interactPlant(self):
- interactscreen = font.render("What will you do? Press 1 to water, or 2 to harvest.",True,(white),False)
- screen.blit(interactscreen,(textx,texty))
- dialogueWait()
- if pressedOne == True:
- intOnePlant = font.render("You water the plant.",True,(white),False)
- screen.blit(intOnePlant,(textx,texty))
- dialogueWait()
- water = water + 1
- if pressedTwo == True:
- intTwoPlant = font.render("You harvest the plant.",True,(white),False)
- screen.blit(intTwoPlant,(textx,texty))
- dialogueWait()
- pygame.sprite.remove()
- self.harvested = True
- else:
- invalidInput()
- def inspectPlant(self):
- 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)
- screen.blit(inspectscreen,(textx,texty))
- dialogueWait()
- if pressedOne == True:
- inspOnePlant = font.render("The plant is a "+str.self.name+". It is "+str.self.status+".",True,(white),False)
- screen.blit(inspOnePlant,(textx,texty))
- dialogueWait()
- elif pressedTwo == True:
- inspTwoPlant = font.render("Currently, the plant's need for water is "+str.self.waterNeeded+".",True,(white),False)
- screen.blit(inspTwoPlant,(textx,texty))
- dialogueWait()
- else:
- invalidInput()
- ###
- ###
- class Animal:
- # A generic animal crop.
- # Constructor
- def __init__(self, anName, anType, anFoodNeeded, anHappiness, anStatus, anHarvested):
- self.name = anName
- self.type = anType
- self.foodNeeded = anFoodNeeded
- self.happiness = 0
- self.status = anStatus
- self.harvested = anHarvested
- def updateStatus(self):
- if self.growth == 15:
- self.status = "Adult"
- elif self.growth > 10:
- self.status = "Young"
- elif self.growth < 5:
- self.status = "Baby"
- def happinessLevel(self):
- if self.animalmood == 15:
- self.happiness = "Happy"
- elif self.animalmood > 10:
- self.happiness = "Content"
- elif self.animalmood > 5:
- self.happiness = "Displeased"
- elif self.animalmood < 5:
- self.happiness = "Miserable"
- def inspectAnimal(self):
- 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)
- screen.blit(lookatText,(textx,texty))
- dialogueWait()
- if pressedOne == True:
- inspOneAnimal = font.render("The animal is a "+str.self.name+". It is "+str.self.status+".",True,(white),False)
- screen.blit(inspOneAnimal,(textx,texty))
- dialogueWait()
- elif pressedTwo == True:
- inspTwoAnimal = font.render("The animal's level of hunger is "+str.self.foodNeeded+". It appears to be "+str.self.happiness+".",True,(white),False)
- screen.blit(inspTwoanimal,(textx,texty))
- dialogueWait()
- else:
- invalidInput()
- def interactAnimal(self):
- interactscreen = font.render("What will you do? Press 1 to feed, or 2 to harvest.", True, (white), False)
- screen.blit(interactscreen, (textx,texty))
- dialogueWait()
- if pressedOne == True:
- intOneAnimal = font.render("You feed the "+str.self.name+".", True, (white), False)
- screen.blit(intOneAnimal, (textx,texty))
- dialogueWait()
- foodNeeded = foodNeeded + 1
- if pressedTwo == True:
- self.harvested = False
- intTwoAnimal = font.render("You harvest the "+str.self.name+".", True, (white), False)
- screen.blit(intTwoAnimal, (textx,texty))
- dialogueWait()
- pygame.sprite.remove()
- self.harvested = True
- else:
- invalidInput()
- def grow(self, food):
- if food >= self.foodNeeded:
- self.growth += self.happiness
- ###
- ###
- class NPC:
- # A generic NPC.
- # Constructor
- def __init__(self, npcName, npcFriendLevel, npcDialogueFile):
- self.name = npcName
- self.friendlevel = npcFriendLevel
- self.dialoguetxt = npcDialogueFile
- def npcFriendLevel(self,friend):
- if self.friendlevel == 15:
- self.friend = "Best friend, somehow"
- elif self.friendlevel > 10:
- self.friend = "Friend"
- elif self.friendlevel > 5:
- self.friend = "Acquaintance"
- elif self.friendlevel < 5:
- self.friend = "Stranger"
- def npcMoods(self, mood):
- moodList = ["happy","annoyed","tired","ashamed","excited","joyous","peckish"]
- mood = random.choice(moodList) # Picks a random item from moodList.
- def inspectNPC(self):
- 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))
- screen.blit(inspectscreenNPC,(textx,texty))
- dialogueWait()
- if pressedOne == True:
- inspOneNPC = font.render(str.self.name+" would certainly consider you a "+str.self.friendlevel+".",True,(255,255,255))
- screen.blit(inspOneNPC,(textx,texty))
- dialogueWait()
- if pressedTwo == True:
- inspTwoNPC = font.render(str.self.name+" seems quite "+str.self.mood+".",True,(255,255,255))
- screen.blit(inspTwoNPC,(textx,texty))
- dialogueWait()
- else:
- invalidInput()
- def interactNPC(self):
- interactscreenNPC = font.render("You see "+str.self.name+". Press 1 if you wish to talk to them.",True,(255,255,255))
- screen.blit(interactscreenNPC,textx,texty)
- dialogueWait()
- if pressedOne == True:
- textfile = self.dialoguetxt # In instantiation, use .txt at the end of a file name.
- f = open(textfile, "r") # Opens text file in 'read'
- lines = textfile.read().split(",") # Imports each line into an array. Splits them with a ','.
- dialogueline = random.choice(lines) # Selects random item from lines.
- intOneNPC = font.render(str.self.name+" says: "+str.dialogueline,True,(255,255,255))
- screen.blit(intOneNPC,(textx,texty))
- dialogueWait()
- textfile.close()
- self.friendlevel = self.friendlevel + 1
- else:
- invalidInput()
- # Load images
- # Player image
- playerImage = pygame.image.load("idle1.png") # Loads file for player image
- playerImage = pygame.transform.scale(playerImage, (40,60)) # 40x60 sprite
- playerImage = playerImage.convert_alpha()
- # Background for day
- backgroundImage = pygame.image.load("protobackgroundD.png") # Protoype background will be changed during other iterations
- backgroundImage = pygame.transform.scale(backgroundImage, (750, 750))
- screen.blit(backgroundImage, (0,0))
- # Background for night
- backgroundImageN = pygame.image.load("protobackgroundN.png")
- backgroundImageN = pygame.transform.scale(backgroundImageN, (750, 750))
- # Plant sprites
- # Instantiation - Plant("Type",water needed/int,growthrate/int,"Status")
- plantimageOne = Plant("Taproot",5,3,"Seedling",False)
- plantimageOne = pygame.image.load("plant1.png")
- plantimageOne = pygame.transform.scale(plantimageOne, (30, 30))
- plantimageTwo = Plant("Witchrot",3,2,"Seedling",False)
- plantimageTwo = pygame.image.load("plant2.png")
- plantimageTwo = pygame.transform.scale(plantimageTwo, (30,30))
- # Animal sprites
- # Instnatiation - Animal("Name","Type",food needed/int,happiness/int,"Status")
- aniamalOne = Animal("Christopher","Amyglamate",10,4,"Baby",False)
- animalOne = pygame.image.load("animal1.png")
- animalOne = pygame.transform.scale(animalOne, (50, 50))
- animalTwo = Animal("Dony","Rowe",5,3,"Adult",False)
- animalTwo = pygame.image.load("animal2.png")
- animalTwo = pygame.transform.scale(animalTwo, (50,50))
- # NPC sprites
- #npcOne - Limbo
- npcOne = NPC("Limbo",5,"npc1text.txt")
- npcOne = pygame.image.load("npc1.png")
- npcOne = pygame.transform.scale(npcOne, (40,60))
- #npcTwo - Handy
- npcTwo = NPC("Handy,",10,"npc2text.txt")
- npcTwo = pygame.image.load("npc2.png")
- npcTwo = pygame.transform.scale(npcTwo, (40,60))
- # Run game
- frame = pygame.time.Clock()
- while run:
- # This code does not process whilst game is not running
- pygame.time.delay(50)
- for event in pygame.event.get():
- if event.type == pygame.QUIT: # If someone presses the X to close the game, this code ensures that the game closes.
- run = False # Run is set to false. No code will be run once this is set to false.
- # In-game Clock
- inGameDays = 0
- seconds = 0
- def gameTime(seconds):
- day = True
- night = False
- start = time.time() # Returns number of seconds since unix epoch (aka 00:00:00)
- time.perf_counter() # Counts process time
- elapsed = 0 # Elapsed time
- while elapsed > seconds: # Already running whilst the program is running
- elapsed = time.time() - start
- time.sleep(1)
- # Update screen for day version of map.
- # Controls
- # Event loop
- pressedDown = None
- pressedLeft = None
- pressedRight = None
- pressedUp = None
- nearPlant = False
- nearAnimal = False
- nearNPC = False
- # Checks collision between player and item
- def checkCollision(x,y,planty,plantx,plantyB, plantxB):
- # \/ Checks collision for plants \/
- nearPlant = False
- nearAnimal = False
- nearNPC = False
- if y >= planty and y <= planty + 10:
- if x >= plantx and x <= plantx + 10:
- nearPlant = True
- return nearPlant
- if y >= plantyB and y <= plantyB + 10:
- if x >= plantxB and x <= plantxB + 10:
- nearPlant = Trues
- return nearPlant
- # \/ Checks collision for animals \/
- if y >= animaly and y <= animaly + 10:
- if x >= animalx and x <= animalx + 10:
- nearAnimal = True
- return nearAnimal
- if y >= animalyB and y <= animalyB + 10:
- if x >= animalxB and x <= animalxB + 10:
- nearAnimal = True
- return nearAnimal
- # \/ Checks collision for NPCs \/
- if y >= npc1y and y <= npc1y + 10:
- if x >= npc1x and x <= npc1x + 10:
- nearNPC = True
- return nearNPC
- if y >= npc2y and y <= npc2y + 10:
- if x >= npc2x and x <= npc2x + 10:
- nearNPC = True
- return nearNPC
- # True - Key is pressed
- keys = pygame.key.get_pressed()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_a:
- pressedLeft = True
- if event.key == pygame.K_d:
- pressedRight = True
- if event.key == pygame.K_w:
- pressedUp = True
- if event.key == pygame.K_s:
- pressedDown = True
- if event.key == pygame.K_e:
- pressedE = True
- if event.key == pygame.K_f:
- pressedF = True
- if event.key == pygame.K_1:
- pressedOne = True
- if event.key == pygame.K_2:
- pressedTwo = True
- # False - Key is no longer pressed
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_a:
- pressedLeft = False
- if event.key == pygame.K_d:
- pressedRight = False
- if event.key == pygame.K_w:
- pressedUp = False
- if event.key == pygame.K_s:
- pressedDown = False
- if event.key == pygame.K_e:
- pressedE = False
- if event.key == pygame.K_f:
- pressedF = False
- if event.key == pygame.K_1:
- pressedOne = False
- if event.key == pygame.K_2:
- pressedTwo = False
- # Sprite movement
- movingRightA1 = True
- movingDownA2 = True
- movingRightN1 = True
- movingLeftN2 = True
- # Animal one
- if animalx >= 400:
- movingRightA1 = True
- elif animalx <= 550:
- movingRightA1 = False
- if movingRightA1 == True:
- animalx += 6
- else:
- animalx -= 6
- # Animal two
- if animalyB >= 350:
- movingDownA2 = True
- elif animalyB <= 150:
- movingDownA2 = True
- if movingDownA2 == True:
- animalyB -= 4
- else:
- animalyB += 4
- # NPC one
- if npc1x >= 100:
- movingRightN1 = True
- elif npc1x <= 175:
- movingRightN1 = False
- if movingRightN1 == True:
- npc1x += 5
- else:
- npc1x -= 5
- # NPC two
- if npc2x >= 375:
- movingLeftN2 = True
- elif npc2x <= 300:
- movingLeftN2 = False
- if movingLeftN2 == True:
- npc2x -= 6
- else:
- npc2x += 6
- # Character Movement
- if pressedLeft == True:
- x -= vel
- elif pressedRight == True:
- x += vel
- elif pressedUp == True:
- y -= vel
- elif pressedDown == True:
- y += vel
- # Update screen with sprites
- gameTime(seconds)
- screen.blit(backgroundImage, (0, 0))
- screen.blit(playerImage, (x, y))
- screen.blit(plantimageOne, (plantx, planty))
- screen.blit(animalOne, (animalx, animaly))
- screen.blit(plantimageTwo, (plantxB, plantyB))
- screen.blit(animalTwo, (animalxB, animalyB))
- screen.blit(npcOne, (npc1x, npc1y))
- screen.blit(npcTwo, (npc2x, npc2y))
- # Interactions
- if nearNPC == True:
- if pressedE == True:
- NPC.inspectnpc()
- elif pressedF == True:
- NPC.interactnpc()
- # Animal interactions
- if nearAnimal == True:
- if pressedE == True:
- Animal.inspectAnimal()
- elif pressedF == True:
- Animal.interactAnimal()
- # Plant interactions
- if nearPlant == True:
- if pressedE == True:
- Plant.inspectPlant()
- elif pressedF == True:
- Plant.interactPlant()
- pygame.display.update()
- frame.tick(30)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement