0% found this document useful (0 votes)
153 views6 pages

Import, From Import Import Import: Redrawwindow

The document contains code for a farm game created with Pygame. It sets up images, buttons, and variables to track scores for adding animals and crops. The game's main loop displays buttons to add cows, hens, pigs, cabbage, corn, and carrots in random locations. Clicking buttons increases the corresponding counter and score. The goal is to earn $200,000 by adding items. If the player wins, congratulatory text is displayed.

Uploaded by

api-512738790
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views6 pages

Import, From Import Import Import: Redrawwindow

The document contains code for a farm game created with Pygame. It sets up images, buttons, and variables to track scores for adding animals and crops. The game's main loop displays buttons to add cows, hens, pigs, cabbage, corn, and carrots in random locations. Clicking buttons increases the corresponding counter and score. The goal is to earn $200,000 by adding items. If the player wins, congratulatory text is displayed.

Uploaded by

api-512738790
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#set the picture

cow1='cow1.png'
cow2='cow2.png'
hen1='hen1.png'
hen2='hen2.png'
pig1='pig1.png'
pig2='pig2.png'
corn1='corn1.png'
corn2='corn2.png'
corn3='corn3.png'
cabbage1='cabbage1.png'
cabbage2='cabbage2.png'
cabbage3='cabbage3.png'
carrot1='carrot1.png'
carrot2='carrot2.png'
carrot3='carrot3.png'

#import important thing


import pygame,sys
from pygame.locals import*
import random
import time
# let the game prepared
pygame.init()
#make the screen
display = pygame.display.set_mode((900, 590))
#put the background
background = pygame.image.load('bactext.jpg').convert()
#show the background
display.blit(background, (-50, 0))
#update the game screen
pygame.display.update()
#build all the thing in second page, which is the main screen for player to play
def redrawwindow():
background=pygame.image.load('farmb.png').convert()
display.blit(background,(0,0))
field=pygame.image.load('fifi.png').convert_alpha()
display.blit(field,(20,200))
field=pygame.image.load('fence.png').convert_alpha()
display.blit(field,(450,200))
fiel=pygame.image.load('fenc.png').convert_alpha()
display.blit(field,(350,375))
fie=pygame.image.load('fen.png').convert_alpha()
display.blit(field,(625,375))
dog1=pygame.image.load("dog1.png").convert_alpha()
display.blit(dog1,(50,375))
pygame.display.update()
return background
# give the name
pygame.display.set_caption("farm")
# put music and repeat the music
pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1,0.0)
#make text font and text
my_font = pygame.font.SysFont("arial", 78)
myfont = pygame.font.SysFont("arial", 40)
#make color
a=(0,0,0,)
b=(255,255,255)
text_surface = my_font.render(
" Welcome to the farm my friend! Last week, an hurricane came and
destroyed my farm. I just finished repairing my farm, and I am 100000$ in debt. Can u
help me earn them back? If you do not accept, I will kill you. :)
",
True, a, b)
x = 0
y = 175

#build the clock and FPS, how many FPS in each sec
clock = pygame.time.Clock()
FPS = 40 # This variable will define how many frames we update per second.

#the class for all the button


class button():
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text

def draw(self, display, outline=None):


# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(display, outline, (self.x - 2, self.y - 2, self.width +
4, self.height + 4), 0)

pygame.draw.rect(display, self.color, (self.x, self.y, self.width,


self.height), 0)

if self.text != '':
font = pygame.font.SysFont('comicsans', 40)
text = font.render(self.text, 1, (0, 0, 0))
display.blit(text, (
self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height /
2 - text.get_height() / 2)))

def isOver(self, pos):


# Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True

return False

#build the button for the first page, let the player play first
greenButton=button((0,255,0),350,350,150,50,'Accept')
greenButton.draw(display, (0, 0, 0))
cowButton = button((0, 255, 0), 550, 100, 200, 50, 'add cow')
cowButton.draw(display, (0, 0, 0))
henButton = button((0, 255, 0), 325, 100, 200, 50, 'add hen')
henButton.draw(display, (0, 0, 0))
pigButton = button((0, 255, 0), 550, 50, 200, 50, 'add pig')
pigButton.draw(display, (0, 0, 0))
cabbageButton = button((0, 255, 0), 100, 100, 200, 50, 'add cabbage')
cabbageButton.draw(display, (0, 0, 0))

cornButton = button((0, 255, 0), 100, 50, 200, 50, 'add corn')
cornButton.draw(display, (0, 0, 0))

carrotButton = button((0, 255, 0), 325, 50, 200, 50, 'add carrot')
carrotButton.draw(display, (0, 0, 0))

# the actrual game button


def buttonofadding():

cabbageButton=button((0,255,0),100,100,200,50,'add cabbage')
cabbageButton.draw(display, (0, 0, 0))

cornButton=button((0,255,0),100,50,200,50,'add corn')
cornButton.draw(display, (0, 0, 0))

carrotButton=button((0,255,0),325,50,200,50,'add carrot')
carrotButton.draw(display, (0, 0, 0))

henButton=button((0,255,0),325,100,200,50,'add hen')
henButton.draw(display, (0, 0, 0))

cowButton=button((0,255,0),550,100,200,50,'add cow')
cowButton.draw(display, (0, 0, 0))

pigButton=button((0,255,0),550,50,200,50,'add pig')
pigButton.draw(display, (0, 0, 0))

# make the init parpare for scoreboard and for calculate score
clickhen=0
clickcow=0
clickpig=0
clickcorn=0
clickcabbage=0
clickcarrot=0
score=0

#while
running=True
while running:
#for the first text go on the screen and movement
x -= 0.5
display.blit(text_surface, (x, y))
pygame.display.update()
#the random place for animal
xforplacing = random.randint(50, 850)
yforplacing = random.randint(200, 500)
#the ramdom place for plant
xforplant = random.randint(20, 400)
yforplant = random.randint(200, 325)
#get the user position
pos = pygame.mouse.get_pos()
#event
for event in pygame.event.get():
#game quit
if event.type==pygame.QUIT:
running=False
#if the user click
if event.type == pygame.MOUSEBUTTONDOWN:
#if the user accept, make everything to init and draw the window again
if greenButton.isOver(pos):
y=-100
display.fill(pygame.Color("black"))
redrawwindow()
buttonofadding()
score=0
clickhen = 0
clickcow = 0
clickpig = 0
clickcorn = 0
clickcabbage = 0
clickcarrot = 0
pygame.mixer.music.load('dogsound.mp3')
pygame.mixer.music.play(0)
color==(255,255,255)

#if user click any of the button, random place and show the animal/plant
elif cabbageButton.isOver(pos):
clickcabbage+=1
pygame.mixer.music.load('plantsound.wav')
pygame.mixer.music.play(0)
cabbage1 = pygame.image.load("cabbage3.png").convert_alpha()
display.blit(cabbage1, (xforplant, yforplant))
pygame.display.update()
elif carrotButton.isOver(pos):
clickcarrot+=1
pygame.mixer.music.load('plantsound.wav')
pygame.mixer.music.play(0)
carrot1 = pygame.image.load("carrot3.png").convert_alpha()
display.blit(carrot1, (xforplant, yforplant))
pygame.display.update()
elif cornButton.isOver(pos):
clickcorn+=1
pygame.mixer.music.load('plantsound.wav')
pygame.mixer.music.play(0)
corn1 = pygame.image.load("corn3.png").convert_alpha()
display.blit(corn1, (xforplant, yforplant))
pygame.display.update()
elif cowButton.isOver(pos):
clickcow+=1
pygame.mixer.music.load('cowsound.wav')
pygame.mixer.music.play(0)
cow1 = pygame.image.load("cow1.png").convert_alpha()
display.blit(cow1, (xforplacing, yforplacing))
pygame.display.update()
elif pigButton.isOver(pos):
pygame.mixer.music.load('pigsound.wav')
pygame.mixer.music.play(0)
clickpig+=1
pig1 = pygame.image.load("pig1.png").convert_alpha()
display.blit(pig1, (xforplacing, yforplacing))
pygame.display.update()
elif henButton.isOver(pos):
clickhen+=1
pygame.mixer.music.load('hensound.wav')
pygame.mixer.music.play(0)
hen1 = pygame.image.load("hen1.png").convert_alpha()
display.blit(hen1, (xforplacing, yforplacing))
pygame.display.update()
pygame.display.update()
clock.tick(FPS)
#calculate the score

score=score+clickcabbage*20+clickcarrot*40+clickcorn*60+clickcow*20+clickhen*40+click
pig*60
scoretext =my_font.render("score="+str(score),True, (0,0,0), (255,255,255))
display.blit(scoretext, (500, 500))
pygame.display.update()

#check if win
if score>=200000:
win = my_font.render("You helped me gain them back!" , True, (0, 0, 0),
(255, 255, 255))
display.blit(win, (0,300))
pygame.display.update()
time.sleep(3)
running=False

'''
https://www.pythoncentral.io/pythons-time-sleep-pause-wait-sleep-stop-your-code/
https://eyehere.net/2011/python-pygame-novice-professional-21/
https://blog.csdn.net/mingzznet/article/details/22660991
https://picsart.com/u/sherrylu8
https://eyehere.net/2011/python-pygame-novice-professional-1/
https://eyehere.net/2011/python-pygame-novice-professional-2/
https://www.pygame.org/
https://stackoverflow.com/questions/20842801/how-to-display-text-in-pygame
https://nerdparadise.com/programming/pygame/part5
https://www.bing.com/videos/search?
q=how+to+build+a+button+in+pygame&view=detail&mid=3A489557B45871F8154E3A489557B45871F
8154E&FORM=VIRE
'''

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy