我正在用python和pygame制作一个选择题问答游戏。我有圆,你想要的是当你点击圆时,它会改变宽度,使圆充满。但我不知道有哪种python函数可以做到这一点
以下是我目前的代码:
# Controls the width of the circles
width_1 = 2
width_2 = 2
width_3 = 2
width_4 = 2
# Circles
pygame.draw.circle(screen, BLACK, [250, 230], 7, width_1)
pygame.draw.circle(screen, BLACK, [250, 260], 7, width_2)
pygame.draw.circle(screen, BLACK, [250, 290], 7, width_3)
pygame.draw.circle(screen, BLACK, [250, 320], 7, width_4)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
我想有这样的东西,如果鼠标悬停在圆圈和鼠标按钮向下和event.button==1然后改变宽度为0(填写)
提前谢谢!
P. S.这不是我所有的代码
首先,你需要鼠标坐标。你可以这样做:
mousex, mousey = pygame.mouse.get_pos()
然后,您需要一个函数来检查鼠标是否在圆圈内。要做到这一点,你需要一个圆圈的hitbox。圆形hitbox很难制作,所以你可以通过以下方法使用方形hitbox:
def check_pressed ():
mousex, mousey = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if click != (0, 0, 0):
if mousex >= buttonx and mousex <= buttonx + button_width and
mousey >= buttony and mousey <= buttony + button_height:
#run function to make button bigger and run a clicked event
将此check_pressed函数添加到主循环中。希望这有帮助!
您可以使用pygame。老鼠获取位置()
(文档)
pygame。老鼠get_pos()
获取鼠标光标位置get_pos()-
你可以做x,y=pygame。老鼠获取位置()
然后打印(x,y)
假设我们想从一个位置飞到另一个位置。我们可以这样做
import pygame
from pygame import *
import sys, random, math, fractions
pygame.init()
Screen_Width = 800
Screen_Height = 600
Total_Display = pygame.display.set_mode((Screen_Width, Screen_Height))
clock = pygame.time.Clock()
class Blocks(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
all_sprite_list.add(self)
block_list.add(self)
self.image = pygame.Surface((32,32))
self.rect = self.image.get_rect()
self.change_y = 0
self.change_x = 0
def blockMove (self, cursor_pos_x, cursor_pos_y, player_pos_x, player_pos_y):
block_vec_x = cursor_pos_x - player_pos_x
block_vec_y = cursor_pos_y - player_pos_y
vec_length = math.sqrt(block_vec_x ** 2 + block_vec_y ** 2)
block_vec_y = (block_vec_y / vec_length) * 5
block_vec_x = (block_vec_x / vec_length) * 5
self.change_y += block_vec_y
self.change_x += block_vec_x
def update(self):
self.rect.y += self.change_y
self.rect.x += self.change_x
现在做一个简单的玩家类只是为了一个位置,以火块到鼠标位置,把这个权利在窗口的中间!
class Player(pygame.sprite.Sprite):
def __init__(self):
player_list.add(self)
self.rect = Rect(400,300,16,16)
self.image = pygame.surface((16,16))
all_sprite_list = pygame.sprite.Group()
player_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
block = Blocks(16,16)
player = Player()
running = True
while running:
clock.tick(60)
for e in pygame.event.get()
if e == pygame.Quit:
running = False
if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
running = False
这里是我们得到鼠标位置的地方,我们设置mouse_X和mouse_Y=mouse。get_pos(),它将输出(x,y)或Mouse_x=Mouse pos x和Mouse_y=Mouse pos y。您还可以通过添加打印(Mouse_x)输出鼠标的位置
Mouse_x, Mouse_y = pygame.mouse.get_pos()
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
block = Blocks(16,16)
block.blockMove(Mouse_x, Mouse_y, player.rect.x, player.rect.y)
block.rect.x = player.rect.x
block.rect.y = player.rect.y
block.update()
Total_Display.fill((255,0,0))
for sprite in all_sprite_list:
pygame.draw.rect(Total_Display,(0,0,0), sprite)
for blocks in block_list:
pygame.draw.rect(Total_Display, (0,0,0), block)
pygame.display.flip()
import pygame
from pygame import *
import sys, random, math, fractions
pygame.init()
Screen_Width = 800
Screen_Height = 600
Total_Display = pygame.display.set_mode((Screen_Width, Screen_Height))
clock = pygame.time.Clock()
class Blocks(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
all_sprite_list.add(self)
block_list.add(self)
self.image = pygame.Surface((32,32))
self.rect = self.image.get_rect()
self.change_y = 0
self.change_x = 0
def blockMove (self, cursor_pos_x, cursor_pos_y, player_pos_x, player_pos_y):
block_vec_x = cursor_pos_x - player_pos_x
block_vec_y = cursor_pos_y - player_pos_y
vec_length = math.sqrt(block_vec_x ** 2 + block_vec_y ** 2)
block_vec_y = (block_vec_y / vec_length) * 5
block_vec_x = (block_vec_x / vec_length) * 5
self.change_y += block_vec_y
self.change_x += block_vec_x
def update(self):
self.rect.y += self.change_y
self.rect.x += self.change_x
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
player_list.add(self)
all_sprite_list.add(self)
self.rect = Rect(400,300,16,16)
self.image = pygame.Surface((16,16))
all_sprite_list = pygame.sprite.Group()
player_list = pygame.sprite.GroupSingle()
block_list = pygame.sprite.Group()
block = Blocks(16,16)
player = Player()
running = True
while running:
clock.tick(60)
for e in pygame.event.get():
if e == pygame.QUIT:
Running = False
if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
running = False
Mouse_x, Mouse_y = pygame.mouse.get_pos()
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
block = Blocks(16,16)
block.update()
block.blockMove(Mouse_x, Mouse_y, player.rect.x, player.rect.y)
block.rect.x = player.rect.x
block.rect.y = player.rect.y
block.update()
Total_Display.fill((255,0,0))
for sprite in all_sprite_list:
pygame.draw.rect(Total_Display,(0,0,0), sprite)
for blocks in block_list:
pygame.draw.rect(Total_Display, (0,0,0), block)
pygame.display.flip()
我想知道如何编写代码来检测鼠标点击精灵。例如:
我在pygame中运行一个游戏,发现了一个漏洞。按照我的编码方式,如果鼠标出了窗口,游戏开始循环。当你得到,它将返回上次在窗口中检测到鼠标的值,但从其他方面看,没有鼠标离开窗口的指示。那么,有没有办法检查鼠标是否已经停止在pygame窗口上悬停,并离开了它?
问题内容: 我正在学习Pygame制作带有Python的游戏。但是,我遇到了一个问题。我正在尝试检测播放器当前何时单击屏幕,但是我的代码无法正常工作。我的代码实际上是搞砸了,还是我正在使用的在线Pygame编译器? 当我运行此代码时,输出控制台在每秒几千次内向文本“您正在单击”发送垃圾邮件。即使我没有单击屏幕,它仍然会显示此信息。即使我的鼠标是不是 在 屏幕上。相同的文字。一遍又一遍。Pyga
问题内容: JFreeChart中是否有一种方法可以从ChartMouseEvent中确定鼠标悬停在绘图空间中的x,y坐标?我尝试使用域十字线值,但这似乎不准确并且滞后于实际的鼠标事件。 谢谢, 杰夫 问题答案: 鼠标坐标相对于ChartPanel,因此您需要对其进行转换:
本文向大家介绍使用js获取鼠标坐标相关面试题,主要包含被问及使用js获取鼠标坐标时的应答技巧和注意事项,需要的朋友参考一下
问题内容: 斯威夫特新手。 我在执行一项琐碎的任务时遇到了麻烦。我要做的就是 按需 获取鼠标光标的x,y坐标。我宁可 不要 等到鼠标移动事件触发后再抓住指针的坐标。 将不胜感激! 问题答案: 您应该看看NSEvent方法mouseLocation 编辑/更新: Xcode 11•Swift 5.1 如果您希望在应用程序处于活动状态时监视任何窗口上的事件,则可以添加与mouseMoved掩码匹配的L