所以我的想法是我有一个windowSurface.blit矩形,当我用鼠标左键单击它时,它应该执行这些命令MOVE_SPEED = 9 end_it_levels = True
但是我无法绕过如何做到这一点。鼠标功能可在此处找到http://www.pygame.org/docs/ref/mouse.html#comment_pygame_mouse_get_pos
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
end_it_levels = False
while (end_it_levels == False):
windowSurface.fill(WHITE)
textlevelFont = pygame.font.SysFont("impact", 26)
level = textlevelFont.render("LEVEL:", True, (BLACK))
level_levels = textlevelFont.render("Easy = 1, Medium = 2 and Hard = 3"
, True, (BLACK))
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_1:
MOVE_SPEED = 9
end_it_levels = True
if event.type == KEYDOWN and event.key == K_2:
MOVE_SPEED = 7
end_it_levels = True
if event.type == KEYDOWN and event.key == K_3:
MOVE_SPEED = 5
end_it_levels = True
windowSurface.blit (level, (290, 115))
#windowSurface.blit (level_levels, (140, 150))
windowSurface.blit(buttonEasyImage, buttonEasy)
windowSurface.blit(buttonNormalImage, buttonNormal)
windowSurface.blit(buttonHardImage, buttonHard)
pygame.display.flip()
这里的代码基本上是你点击鼠标左键并在blit的坐标内[buttonEasy = pygame.Rect(10,150,200,90)]
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_1:
MOVE_SPEED = 9
end_it_levels = True
同样的新困境
end_it = False
while (end_it == False):
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonStart.collidepoint(mouse_coordinates):
end_it_levels = True
windowSurface.blit(buttonStartImage, buttonStart)
pygame.display.flip()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 直到特定循环的代码:
import pygame, sys, random, math
from pygame.locals import *
from threading import Timer
pygame.init()
mainClock = pygame.time.Clock()
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 400
windowSurface = pygame.display.set_mode ((WINDOW_WIDTH,
WINDOW_HEIGHT), 0)
pygame.display.set_caption('Catch the rabbits!')
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
textFont = pygame.font.SysFont("impact", 60)
text = textFont.render("YOU WIN!", True, (193, 0, 0))
rabbitCounter = 0
NEW_RABBIT = 40
RABBIT_SIZE = 64
buttonStartImage = pygame.image.load('buttonStart.png')
background_image = pygame.image.load('bg.jpg').convert()
playerImage = pygame.image.load('Fox.png')
playerImageTwo = pygame.image.load('Fox2.png')
rabbitImage = pygame.image.load('topic_rabbit.png')
rabbitImageTwo = pygame.image.load('topic_rabbit2.png')
buttonEasyImage = pygame.image.load('buttonEasy.png')
buttonNormalImage = pygame.image.load('buttonNormal.png')
buttonHardImage = pygame.image.load('buttonHard.png')
player = pygame.Rect(420, 100, 40, 40)
buttonStart = pygame.Rect(220, 150, 200, 90)
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
rabbits = []
for i in range (20):
rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
RABBIT_SIZE, RABBIT_SIZE))
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVE_SPEED = 0
end_it = False
while (end_it == False):
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonStart.collidepoint(mouse_coordinates):
end_it_levels = True
windowSurface.blit(buttonStartImage, buttonStart)
pygame.display.flip()
答案 0 :(得分:1)
您必须添加额外的事件才能进行检查。有一种Rect
方法只适用于collidepoint
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
end_it_levels = False
while (end_it_levels == False):
windowSurface.fill(WHITE)
textlevelFont = pygame.font.SysFont("impact", 26)
level = textlevelFont.render("LEVEL:", True, (BLACK))
level_levels = textlevelFont.render("Easy = 1, Medium = 2 and Hard = 3"
, True, (BLACK))
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_1:
MOVE_SPEED = 9
end_it_levels = True
if event.type == KEYDOWN and event.key == K_2:
MOVE_SPEED = 7
end_it_levels = True
if event.type == KEYDOWN and event.key == K_3:
MOVE_SPEED = 5
end_it_levels = True
# Is the left mousebutton being clicked?
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if Your_Rect.collidepoint(mouse_coordinates):
# do stuff...
windowSurface.blit (level, (290, 115))
#windowSurface.blit (level_levels, (140, 150))
windowSurface.blit(buttonEasyImage, buttonEasy)
windowSurface.blit(buttonNormalImage, buttonNormal)
windowSurface.blit(buttonHardImage, buttonHard)
pygame.display.flip()
我不确定这是否是您想要做的,但我希望这会有所帮助。