【问题标题】:Clicking recs in Pygame在 Pygame 中单击记录
【发布时间】:2020-07-23 01:25:16
【问题描述】:

这几天我正在开始使用python3并开始开发一些小项目,但是我遇到了一些麻烦,如果我不能使用一流的专业编码器语言,请见谅。

如何使 pygame.draw.rect 矩形变得可点击? 我知道 pygame.mouse。的,但代码中可能有问题。 我想要它,这样当我按下红色矩形时,它会降低我的健康并添加一个“燃烧”统计信息(现在只是文本)。

代码如下:

import pygame
import random
import sys

pygame.init()

#Screen Size
screen_width = 600
screen_height = 600

#Screen Settings
screen = pygame.display.set_mode((screen_width, screen_height))
br_color = (0, 0, 0)
pygame.display.set_caption("Type Effect Beta 0.0.1")

#Game Over Bullian
game_over = False

#Other Defenitions
clock = pygame.time.Clock()
myFont = pygame.font.SysFont("arial", 20)

#Basic Recources
health = 50
score = 0
status = "none"

#Colors for the Text
white = (255, 255, 255)
red = (255, 0, 0)

#Mouse Things
mouse_location = pygame.mouse.get_pos()
print(mouse_location)

#Status Text Helpers
burning = "Burning"

#Cards
card_size_x = 45
card_size_y = 60
fire_car_color = (255, 0 ,0)
fire_card_posx = 300
fire_card_posy = 300
card_button_fire = pygame.Rect(fire_card_posx, fire_card_posy, card_size_x, card_size_y)

#Functions

def health_decrease_burn(health, status):
    health -= 1
    status = "burning"
    return health and status                

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if pygame.mouse.get_pressed()[0] and card_button_fire.collidepoint(mouse_location):
            health_decrease_burn()


        if health_decrease_burn(health, status) and health <= 0:
            game_over = True








    text = "Score:" + str(score)    
    lable = myFont.render(text, 1, white)
    screen.blit(lable, (10, 10))

    text = "Health:" + str(health)  
    lable = myFont.render(text, 1, red)
    screen.blit(lable, (10, 30))

    text = "Status:" + str(status)  
    lable = myFont.render(text, 1, white)
    screen.blit(lable, (10, 50))

    pygame.draw.rect(screen, fire_car_color, (fire_card_posx, fire_card_posy, card_size_x, card_size_y))

    clock.tick(30)

    pygame.display.update()

【问题讨论】:

标签: python pygame


【解决方案1】:

您需要在主循环的每次迭代中获取mouse_location,因为鼠标位置/状态会不断变化。当前代码仅在开始时获取一次鼠标位置。

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONUP:         # if mouse button clicked
            mouse_location = pygame.mouse.get_pos()      # <-- HERE
            if pygame.mouse.get_pressed()[0] and card_button_fire.collidepoint(mouse_location):
                health_decrease_burn()

         #[...etc ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多