PYTHON
Welcome to Candyland!
This is a pygame zero game. The original game consists of keep the garden happy by watering the flowers and escaping from the fangflower. but my new modified version consists of stealing 3 candies of the four that appear in handfuls in the landscape so that only one remains. If the police dog shows up, he locks you in a candy bubble
from random import randint
import time
import pgzrun
#import pygame
WIDTH = 900
HEIGHT = 500
CENTER_X = WIDTH / 2
CENTER_Y = HEIGHT / 2
game_over = False
finalized = False
garden_happy = True
fangflower_collision = False
time_elapsed = 0
start_time = time.time()
cow = Actor("raccoon")
cow.pos = 445, 440
flower_list = []
wilted_list = []
fangflower_list = []
fangflower_vy_list = []
fangflower_vx_list = []
def draw():
# screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
global game_over, time_elapsed, finalized
if not game_over:
screen.clear()
screen.blit("cloudd", (0, 0))
cow.draw()
for flower in flower_list:
flower.draw()
for fangflower in fangflower_list:
fangflower.draw()
time_elapsed = int(time.time() - start_time)
screen.draw.text(
"Time to catch candy: "+
str(time_elapsed) + " seconds",
topleft=(10, 10), color="purple"
)
else:
if not finalized:
cow.draw()
screen.draw.text(
"Time to catch candy: " +
str(time_elapsed) + " seconds",
topleft=(10, 10), color="purple"
)
if (not garden_happy):
screen.draw.text(
"CANDIES WITHOUT COLLECTING - GAME OVER!", color="purple",
topleft=(10, 50)
)
finalized = True
else:
screen.draw.text(
"THE POLICE HAS ATTACKED YOU! - GAME OVER!", color="purple", topleft=(10, 50)
)
finalized = True
return
def new_flower():
global flower_list, wilted_list
flower_new = Actor("candyy")
flower_new.pos = randint(90, WIDTH - 90), randint(250, HEIGHT - 250)
flower_list.append(flower_new)
wilted_list.append("happy")
return
def add_flowers():
global game_over
if not game_over:
new_flower()
clock.schedule(add_flowers, 4)
return
def check_wilt_times():
global wilted_list, game_over, garden_happy
if wilted_list:
for wilted_since in wilted_list:
if not wilted_since == "happy":
time_wilted = int(time.time() - wilted_since)
if (time_wilted) > 15.0:
garden_happy = False
game_over = True
break
return
def wilt_flower():
global flower_list, wilted_list, game_over
if not game_over:
if flower_list:
rand_flower = randint(0, len(flower_list) - 1)
if flower_list[rand_flower].image == "candyy":
flower_list[rand_flower].image = "candies"
wilted_list[rand_flower] = time.time()
clock.schedule(wilt_flower, 3)
return
def check_flower_collision():
global cow, flower_list, wilted_list
index = 0
for flower in flower_list:
if flower.colliderect(cow) and flower.image == "candies":
flower.image = "candyy"
wilted_list[index] = "happy"
break
index = index + 1
return
def check_fangflower_collision():
global cow, fangflower_list, fangflower_collision
global game_over
for fangflower in fangflower_list:
if fangflower.colliderect(cow):
cow.image = "bubble"
game_over = True
break
return
def velocity():
random_dir = randint(0, 1)
random_velocity = randint(2, 3)
if random_dir == 0:
return -random_velocity
else:
return random_velocity
def mutate():
global flower_list, fangflower_list, fangflower_vy_list
global fangflower_vx_list, game_over
if not game_over and flower_list:
rand_flower = randint(0, len(flower_list) - 1)
fangflower_pos_x = flower_list[rand_flower].x
fangflower_pos_y = flower_list[rand_flower].y
del flower_list[rand_flower]
fangflower = Actor("policedog")
fangflower.pos = fangflower_pos_x, fangflower_pos_y
fangflower_vx = velocity()
fangflower_vy = velocity()
fangflower = fangflower_list.append(fangflower)
fangflower_vx_list.append(fangflower_vx)
fangflower_vy_list.append(fangflower_vy)
clock.schedule(mutate, 20)
return
def update_fangflowers():
global fangflower_list, game_over
if not game_over:
index = 0
for fangflower in fangflower_list:
fangflower_vx = fangflower_vx_list[index]
fangflower_vy = fangflower_vy_list[index]
fangflower.x = fangflower.x + fangflower_vx
fangflower.y = fangflower.y + fangflower_vy
if fangflower.left < 0:
fangflower_vx_list[index] = -fangflower_vx
if fangflower.right > WIDTH:
fangflower_vx_list[index] = -fangflower_vx
if fangflower.top < 250:
fangflower_vy_list[index] = -fangflower_vy
if fangflower.bottom > HEIGHT:
fangflower_vy_list[index] = -fangflower_vy
index = index + 1
return
def reset_cow():
global game_over
if not game_over:
cow.image = "raccoon"
return
add_flowers()
wilt_flower()
def update():
global score, game_over, fangflower_collision
global flower_list, fangflower_list, time_elapsed
fangflower_collision = check_fangflower_collision()
check_wilt_times()
if not game_over:
if keyboard.space:
cow.image = "raccoon_thief"
clock.schedule(reset_cow, 0.5)
check_flower_collision()
if keyboard.left and cow.x > 0:
cow.x -= 5
elif keyboard.right and cow.x < WIDTH:
cow.x += 5
elif keyboard.up and cow.y > 250:
cow.y -= 5
elif keyboard.down and cow.y < HEIGHT:
cow.y += 5
if time_elapsed > 20 and not fangflower_list:
mutate()
update_fangflowers()
This is a videotutorial showing how to create the code: