如何检测类中pygame表面的点击

问题描述 投票:0回答:1

我有一个模拟器,您可以在其中砍树,所以我希望它是这样,以便当我单击树时,它将从3秒开始倒数,然后消失。我将树分类为Tree类,但是现在我不知道如何在if语句中获得它们的位置,因为它们不再是真正的pygame表面。如果有人在课堂上,有人可以告诉我如何获得职位吗?代码如下:

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
    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
            pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
            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_two.rect.collidepoint(pygame.mouse.get_pos()): # This bit doesn't work
            tree_two.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
    if keys[pygame.K_q]:
        tree_one.destroy()

    root.fill(white)
    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()    
pygame.quit()




python python-3.x class pygame position
1个回答
0
投票

您的树类可以更好地组织。您需要做的一件事是将树的矩形放入自己的类变量中,以便您可以访问它们。这是一个如何做的例子。有关我所做的更改,请参见代码注释:

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

    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
            # add trunk & leaves variable to Tree class
            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()

    def is_clicked(self):
        return pygame.mouse.get_pressed()[0] and self.rect.collidepoint(pygame.mouse.get_pos())
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 Tree trunk is clicked
        if pygame.mouse.get_pressed()[0] and tree_two.trunk.collidepoint(pygame.mouse.get_pos()): # This bit doesn't work
            tree_two.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
    if keys[pygame.K_q]:
        tree_one.destroy()

    root.fill(white)
    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()
pygame.quit()
© www.soinside.com 2019 - 2024. All rights reserved.