简体   繁体   English

如何将光标放在图像中||| pygame错误|||

[英]How can I place the cursor inside of my image ||| Pygame error |||

Here is the code I am having issues with. 这是我遇到问题的代码。 I am creating a small basic mouse tracking "game" in pygame however, though I have added /2 next to the width and height... the mouse will not go to the center. 我在pygame中创建了一个小的基本鼠标跟踪“游戏”,尽管我在宽度和高度旁边添加了/ 2 ...鼠标不会移到中心。 Here is the code. 这是代码。 What am I doing wrong 我究竟做错了什么

import pygame, sys
from pygame.locals import *

bg_image = "bg.jpg"
mouse_image = "superball.jpg"

pygame.init()
screen = pygame.display.set_mode((1000, 1000),0,32)

background = pygame.image.load(bg_image).convert()
mouse_c = pygame.image.load(mouse_image).convert_alpha()

while True:
  for event in pygame.event.get():
        if event.type == QUIT:
              pygame.quit()
              sys.exit()
  screen.blit(background, (0,0))

  x,y = pygame.mouse.get_pos()
  x != mouse_c.get_width()/2 
  y != mouse_c.get_height()/2

  screen.blit(mouse_c,(x,y))

  pygame.display.update() 

Ignore the indent mistakes. 忽略缩进错误。 That was a copy and paste error 那是复制和粘贴错误

It seems to me that you have written x != mouse_c.get_width()/2 instead of x = mouse_c.get_width()/2 Same with the y. 在我看来,您已经编写了x!= mouse_c.get_width()/ 2而不是x = mouse_c.get_width()/ 2与y相同。

Simply remove the not '!' 只需删除不是'!' symbol from those two lines and it should work. 这两行的符号,它应该可以工作。 x != mouse_c.get_width()/2 is a comparison statement, the whole thing will evaluate to true or false. x!= mouse_c.get_width()/ 2是一个比较语句,整个结果将为true或false。 x and y will remain unchanged, so when you use them later they are still the original values. x和y将保持不变,因此以后使用它们时,它们仍然是原始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM