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
- # 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(-1)
- 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
- # 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