Professional Documents
Culture Documents
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'
#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.
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)))
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))
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
'''