我想制作当我的图像(猫)越过广场时背景会改变,但此刻它没有做任何事情,有人可以帮忙。我有一个范围半径,但似乎不起作用。猫是英雄。
矩形有问题吗,我应该把它变成定义而不是变量?
帮助!
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((500,500))
pygame.display.set_caption("A Heros Journy")
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
NAVYBLUE = (0, 0, 128)
hero = pygame.image.load("cat.png")
herox = 10
heroy = 10
DISPLAYSURF.fill(GREEN)
def main():
global herox, hero, heroy
mouseClicked = False
boxx = 225
boxy = 225
DISPLAYSURF.fill(GREEN)
DISPLAYSURF.blit(hero, (herox, heroy))
while True:
box = pygame.draw.rect(DISPLAYSURF, BLUE, (boxx, boxy, 50, 50))
for event in pygame.event.get():
if event.type == MOUSEMOTION:
herox, heroy = event.pos
DISPLAYSURF.fill(GREEN)
DISPLAYSURF.blit(hero, (herox, heroy))
elif event.type == MOUSEBUTTONUP:
herox, heroy = event.pos
mouseClicked = True
if hero in range(boxx, boxy):
DISPLAYSURF.fill(NAVYBLUE)
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
main()
答案 0 :(得分:0)
注意这不是一个非常好的解决方案,但它确实可行,我确信有一种方法可以缩小它。
替换
if hero in range(boxx, boxy):
DISPLAYSURF.fill(NAVYBLUE)
与
if herox > boxx + 50:
DISPLAYSURF.fill(NAVYBLUE)
elif herox < boxx:
DISPLAYSURF.fill(NAVYBLUE)
if heroy > boxy + 50:
DISPLAYSURF.fill(NAVYBLUE)
elif heroy < boxy:
DISPLAYSURF.fill(NAVYBLUE)
这个解决方案应该可行,但绝对不是最好的方法。