Pygame window 会突然填充白色
Pygame window will suddenly fill with white
我正在制作一个砍树游戏,当你砍倒一棵树时,它会从 3 开始倒计时,直到砍倒它。然而,在倒计时过程中,window 将全部填充为白色,除了屏幕底部的倒计时文本外,什么也看不到。倒计时结束后,一切都会return恢复正常。有人能告诉我为什么会这样吗?完整代码如下:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Foraging Simulator")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
class Tree:
def __init__(self, tree_x, tree_y, tree_property_name):
self.tree_x = tree_x
self.tree_y = tree_y
self.tree_property_name = tree_property_name
self.trunk = None
self.leaves = None
def destroy(self):
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('3', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
pygame.display.update()
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('2', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
pygame.display.update()
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('1', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
pygame.display.update()
self.tree_property_name = False
def create_tree(self):
if self.tree_property_name:
trunk_x = self.tree_x + 10
trunk_y = self.tree_y + 10
self.trunk = pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
self.leaves = pygame.draw.rect(root, green, (self.tree_x, self.tree_y, leaves_width, leaves_height))
def redraw(self):
self.create_tree()
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
tree_one = Tree(0, 0, tree_one_property)
tree_two = Tree(50, 0, tree_two_property)
tree_three = Tree(100, 0, tree_three_property)
tree_four = Tree(150, 0, tree_four_property)
tree_five = Tree(200, 0, tree_five_property)
tree_six = Tree(0, 50, tree_six_property)
tree_eight = Tree(100, 50, tree_eight_property)
tree_seven = Tree(50, 50, tree_seven_property)
tree_nine = Tree(150, 50, tree_nine_property)
tree_ten = Tree(200, 50, tree_ten_property)
tree_eleven = Tree(0, 100, tree_eleven_property)
tree_twelve = Tree(50, 100, tree_twelve_property)
tree_thirteen = Tree(100, 100, tree_thirteen_property)
tree_fourteen = Tree(150, 100, tree_fourteen_property)
tree_fifteen = Tree(200, 100, tree_fifteen_property)
tree_sixteen = Tree(0, 150, tree_sixteen_property)
tree_seventeen = Tree(50, 150, tree_seventeen_property)
tree_eighteen = Tree(100, 150, tree_eighteen_property)
tree_nineteen = Tree(150, 150, tree_nineteen_property)
tree_twenty = Tree(200, 150, tree_twenty_property)
tree_twenty_one = Tree(0, 200, tree_twenty_one_property)
tree_twenty_two = Tree(50, 200, tree_twenty_two_property)
tree_twenty_three = Tree(100, 200, tree_twenty_three_property)
tree_twenty_four = Tree(150, 200, tree_twenty_four_property)
tree_twenty_five = Tree(200, 200, tree_twenty_five_property)
root.fill(white)
while window_is_open:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
if pygame.mouse.get_pressed()[0] and tree_one.trunk.collidepoint(pygame.mouse.get_pos()):
tree_one.destroy()
if pygame.mouse.get_pressed()[0] and tree_two.trunk.collidepoint(pygame.mouse.get_pos()):
tree_two.destroy()
if pygame.mouse.get_pressed()[0] and tree_three.trunk.collidepoint(pygame.mouse.get_pos()):
tree_three.destroy()
if pygame.mouse.get_pressed()[0] and tree_four.trunk.collidepoint(pygame.mouse.get_pos()):
tree_four.destroy()
if pygame.mouse.get_pressed()[0] and tree_five.trunk.collidepoint(pygame.mouse.get_pos()):
tree_five.destroy()
if pygame.mouse.get_pressed()[0] and tree_six.trunk.collidepoint(pygame.mouse.get_pos()):
tree_six.destroy()
if pygame.mouse.get_pressed()[0] and tree_seven.trunk.collidepoint(pygame.mouse.get_pos()):
tree_seven.destroy()
if pygame.mouse.get_pressed()[0] and tree_eight.trunk.collidepoint(pygame.mouse.get_pos()):
tree_eight.destroy()
if pygame.mouse.get_pressed()[0] and tree_nine.trunk.collidepoint(pygame.mouse.get_pos()):
tree_nine.destroy()
if pygame.mouse.get_pressed()[0] and tree_ten.trunk.collidepoint(pygame.mouse.get_pos()):
tree_ten.destroy()
if pygame.mouse.get_pressed()[0] and tree_eleven.trunk.collidepoint(pygame.mouse.get_pos()):
tree_eleven.destroy()
if pygame.mouse.get_pressed()[0] and tree_twelve.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twelve.destroy()
if pygame.mouse.get_pressed()[0] and tree_thirteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_thirteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_fourteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_fourteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_fifteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_fifteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_sixteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_sixteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_seventeen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_seventeen.destroy()
if pygame.mouse.get_pressed()[0] and tree_eighteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_eighteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_nineteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_nineteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_one.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_one.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_two.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_two.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_three.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_three.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_four.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_four.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_five.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_five.destroy()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
root.blit(score, (410, 0))
rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10))
tree_one.redraw()
tree_two.redraw()
tree_three.redraw()
tree_four.redraw()
tree_five.redraw()
tree_six.redraw()
tree_seven.redraw()
tree_eight.redraw()
tree_nine.redraw()
tree_ten.redraw()
tree_eleven.redraw()
tree_twelve.redraw()
tree_thirteen.redraw()
tree_fourteen.redraw()
tree_fifteen.redraw()
tree_sixteen.redraw()
tree_seventeen.redraw()
tree_eighteen.redraw()
tree_nineteen.redraw()
tree_twenty.redraw()
tree_twenty_one.redraw()
tree_twenty_two.redraw()
tree_twenty_three.redraw()
tree_twenty_four.redraw()
tree_twenty_five.redraw()
pygame.display.update()
root.fill(white)
pygame.quit()
当树被摧毁时属性self.tree_property_name
就足够了:
class Tree:
def __init__(self, tree_x, tree_y):
self.tree_x = tree_x
self.tree_y = tree_y
self.tree_property_name = True
self.trunk = None
self.leaves = None
# [...]
def destroy(self):
self.tree_property_name = False
创建一个树列表而不是单独的变量:
trees = []
for x in range(4):
for y in range (4):
trees.append(Tree(x*50, y*50))
在列表中画树:
while window_is_open:
# [...]
for tree in trees:
tree.redraw()
评估是否循环单击树并在几秒内启动 count_down
:
count_down = 0
destroy_tree = None
while window_is_open:
delta_time = pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if destroy_tree == None and event.button == 1:
for tree in trees:
if tree.tree_property_name:
if tree.trunk.collidepoint(event.pos) or tree.leaves.collidepoint(event.pos):
count_down = 3
destroy_tree = tree
当计数到期时销毁密钥:
while window_is_open:
# [...]
number = 0
if destroy_tree != None:
count_down -= delta_time / 1000
if count_down > 0:
number = int(count_down) +1
else:
destroy_tree.destroy()
destroy_tree = None
看例子:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Foraging Simulator")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
class Tree:
def __init__(self, tree_x, tree_y):
self.tree_x = tree_x
self.tree_y = tree_y
self.tree_property_name = True
self.trunk = None
self.leaves = None
def create_tree(self):
if self.tree_property_name:
trunk_x = self.tree_x + 10
trunk_y = self.tree_y + 10
self.trunk = pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
self.leaves = pygame.draw.rect(root, green, (self.tree_x, self.tree_y, leaves_width, leaves_height))
def destroy(self):
self.tree_property_name = False
def redraw(self):
self.create_tree()
trees = []
for x in range(4):
for y in range (4):
trees.append(Tree(x*50, y*50))
count_down = 0
destroy_tree = None
while window_is_open:
delta_time = pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if destroy_tree == None and event.button == 1:
for tree in trees:
if tree.tree_property_name:
if tree.trunk.collidepoint(event.pos) or tree.leaves.collidepoint(event.pos):
count_down = 3
destroy_tree = tree
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
number = 0
if destroy_tree != None:
count_down -= delta_time / 1000
if count_down > 0:
number = int(count_down) + 1
else:
destroy_tree.destroy()
destroy_tree = None
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
countdown = font.render(str(number), True, (0, 0, 0))
root.fill(white)
root.blit(score, (410, 0))
root.blit(countdown, (583, 553))
for tree in trees:
tree.redraw()
pygame.display.update()
pygame.quit()
我正在制作一个砍树游戏,当你砍倒一棵树时,它会从 3 开始倒计时,直到砍倒它。然而,在倒计时过程中,window 将全部填充为白色,除了屏幕底部的倒计时文本外,什么也看不到。倒计时结束后,一切都会return恢复正常。有人能告诉我为什么会这样吗?完整代码如下:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Foraging Simulator")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
class Tree:
def __init__(self, tree_x, tree_y, tree_property_name):
self.tree_x = tree_x
self.tree_y = tree_y
self.tree_property_name = tree_property_name
self.trunk = None
self.leaves = None
def destroy(self):
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('3', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
pygame.display.update()
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('2', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
pygame.display.update()
count = pygame.font.SysFont('Tahoma', 18, True, False)
countdown = count.render('1', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
pygame.display.update()
self.tree_property_name = False
def create_tree(self):
if self.tree_property_name:
trunk_x = self.tree_x + 10
trunk_y = self.tree_y + 10
self.trunk = pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
self.leaves = pygame.draw.rect(root, green, (self.tree_x, self.tree_y, leaves_width, leaves_height))
def redraw(self):
self.create_tree()
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
tree_one = Tree(0, 0, tree_one_property)
tree_two = Tree(50, 0, tree_two_property)
tree_three = Tree(100, 0, tree_three_property)
tree_four = Tree(150, 0, tree_four_property)
tree_five = Tree(200, 0, tree_five_property)
tree_six = Tree(0, 50, tree_six_property)
tree_eight = Tree(100, 50, tree_eight_property)
tree_seven = Tree(50, 50, tree_seven_property)
tree_nine = Tree(150, 50, tree_nine_property)
tree_ten = Tree(200, 50, tree_ten_property)
tree_eleven = Tree(0, 100, tree_eleven_property)
tree_twelve = Tree(50, 100, tree_twelve_property)
tree_thirteen = Tree(100, 100, tree_thirteen_property)
tree_fourteen = Tree(150, 100, tree_fourteen_property)
tree_fifteen = Tree(200, 100, tree_fifteen_property)
tree_sixteen = Tree(0, 150, tree_sixteen_property)
tree_seventeen = Tree(50, 150, tree_seventeen_property)
tree_eighteen = Tree(100, 150, tree_eighteen_property)
tree_nineteen = Tree(150, 150, tree_nineteen_property)
tree_twenty = Tree(200, 150, tree_twenty_property)
tree_twenty_one = Tree(0, 200, tree_twenty_one_property)
tree_twenty_two = Tree(50, 200, tree_twenty_two_property)
tree_twenty_three = Tree(100, 200, tree_twenty_three_property)
tree_twenty_four = Tree(150, 200, tree_twenty_four_property)
tree_twenty_five = Tree(200, 200, tree_twenty_five_property)
root.fill(white)
while window_is_open:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
if pygame.mouse.get_pressed()[0] and tree_one.trunk.collidepoint(pygame.mouse.get_pos()):
tree_one.destroy()
if pygame.mouse.get_pressed()[0] and tree_two.trunk.collidepoint(pygame.mouse.get_pos()):
tree_two.destroy()
if pygame.mouse.get_pressed()[0] and tree_three.trunk.collidepoint(pygame.mouse.get_pos()):
tree_three.destroy()
if pygame.mouse.get_pressed()[0] and tree_four.trunk.collidepoint(pygame.mouse.get_pos()):
tree_four.destroy()
if pygame.mouse.get_pressed()[0] and tree_five.trunk.collidepoint(pygame.mouse.get_pos()):
tree_five.destroy()
if pygame.mouse.get_pressed()[0] and tree_six.trunk.collidepoint(pygame.mouse.get_pos()):
tree_six.destroy()
if pygame.mouse.get_pressed()[0] and tree_seven.trunk.collidepoint(pygame.mouse.get_pos()):
tree_seven.destroy()
if pygame.mouse.get_pressed()[0] and tree_eight.trunk.collidepoint(pygame.mouse.get_pos()):
tree_eight.destroy()
if pygame.mouse.get_pressed()[0] and tree_nine.trunk.collidepoint(pygame.mouse.get_pos()):
tree_nine.destroy()
if pygame.mouse.get_pressed()[0] and tree_ten.trunk.collidepoint(pygame.mouse.get_pos()):
tree_ten.destroy()
if pygame.mouse.get_pressed()[0] and tree_eleven.trunk.collidepoint(pygame.mouse.get_pos()):
tree_eleven.destroy()
if pygame.mouse.get_pressed()[0] and tree_twelve.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twelve.destroy()
if pygame.mouse.get_pressed()[0] and tree_thirteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_thirteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_fourteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_fourteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_fifteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_fifteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_sixteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_sixteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_seventeen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_seventeen.destroy()
if pygame.mouse.get_pressed()[0] and tree_eighteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_eighteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_nineteen.trunk.collidepoint(pygame.mouse.get_pos()):
tree_nineteen.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_one.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_one.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_two.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_two.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_three.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_three.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_four.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_four.destroy()
if pygame.mouse.get_pressed()[0] and tree_twenty_five.trunk.collidepoint(pygame.mouse.get_pos()):
tree_twenty_five.destroy()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
root.blit(score, (410, 0))
rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10))
tree_one.redraw()
tree_two.redraw()
tree_three.redraw()
tree_four.redraw()
tree_five.redraw()
tree_six.redraw()
tree_seven.redraw()
tree_eight.redraw()
tree_nine.redraw()
tree_ten.redraw()
tree_eleven.redraw()
tree_twelve.redraw()
tree_thirteen.redraw()
tree_fourteen.redraw()
tree_fifteen.redraw()
tree_sixteen.redraw()
tree_seventeen.redraw()
tree_eighteen.redraw()
tree_nineteen.redraw()
tree_twenty.redraw()
tree_twenty_one.redraw()
tree_twenty_two.redraw()
tree_twenty_three.redraw()
tree_twenty_four.redraw()
tree_twenty_five.redraw()
pygame.display.update()
root.fill(white)
pygame.quit()
当树被摧毁时属性self.tree_property_name
就足够了:
class Tree:
def __init__(self, tree_x, tree_y):
self.tree_x = tree_x
self.tree_y = tree_y
self.tree_property_name = True
self.trunk = None
self.leaves = None
# [...]
def destroy(self):
self.tree_property_name = False
创建一个树列表而不是单独的变量:
trees = []
for x in range(4):
for y in range (4):
trees.append(Tree(x*50, y*50))
在列表中画树:
while window_is_open:
# [...]
for tree in trees:
tree.redraw()
评估是否循环单击树并在几秒内启动 count_down
:
count_down = 0
destroy_tree = None
while window_is_open:
delta_time = pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if destroy_tree == None and event.button == 1:
for tree in trees:
if tree.tree_property_name:
if tree.trunk.collidepoint(event.pos) or tree.leaves.collidepoint(event.pos):
count_down = 3
destroy_tree = tree
当计数到期时销毁密钥:
while window_is_open:
# [...]
number = 0
if destroy_tree != None:
count_down -= delta_time / 1000
if count_down > 0:
number = int(count_down) +1
else:
destroy_tree.destroy()
destroy_tree = None
看例子:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Foraging Simulator")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
class Tree:
def __init__(self, tree_x, tree_y):
self.tree_x = tree_x
self.tree_y = tree_y
self.tree_property_name = True
self.trunk = None
self.leaves = None
def create_tree(self):
if self.tree_property_name:
trunk_x = self.tree_x + 10
trunk_y = self.tree_y + 10
self.trunk = pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
self.leaves = pygame.draw.rect(root, green, (self.tree_x, self.tree_y, leaves_width, leaves_height))
def destroy(self):
self.tree_property_name = False
def redraw(self):
self.create_tree()
trees = []
for x in range(4):
for y in range (4):
trees.append(Tree(x*50, y*50))
count_down = 0
destroy_tree = None
while window_is_open:
delta_time = pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if destroy_tree == None and event.button == 1:
for tree in trees:
if tree.tree_property_name:
if tree.trunk.collidepoint(event.pos) or tree.leaves.collidepoint(event.pos):
count_down = 3
destroy_tree = tree
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
number = 0
if destroy_tree != None:
count_down -= delta_time / 1000
if count_down > 0:
number = int(count_down) + 1
else:
destroy_tree.destroy()
destroy_tree = None
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
countdown = font.render(str(number), True, (0, 0, 0))
root.fill(white)
root.blit(score, (410, 0))
root.blit(countdown, (583, 553))
for tree in trees:
tree.redraw()
pygame.display.update()
pygame.quit()