PyGame:draw.rect()具有无效的参数 [英] PyGame: draw.rect() has invalid parameters

查看:727
本文介绍了PyGame:draw.rect()具有无效的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PyGame学习鼠标事件,并且试图在用户单击的位置绘制一个框.我设置了一个等于pygame.mouse.get_pos()的变量,并根据鼠标的x,y位置调用了各个元组成员.这是代码:

I'm trying to learn mouse events with PyGame, and I'm trying to draw a box wherever the user clicks. I'm setting a variable equal to pygame.mouse.get_pos(), and calling individual tuple members according to the mouse's x, y position. Here's the code:

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((800, 600))

mousepos = pygame.mouse.get_pos()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pygame.draw.rect(mousepos[0], mousepos[1], 20, 20)

游戏启动,但是当我单击鼠标时,它崩溃了,并出现以下错误:

The game starts up, but when I click, it crashes, giving this error:

Traceback (most recent call last):
  File "C:\Users\User\Documents\proj\Python\mouse.py", line 13, in <module>
    pygame.draw.rect(mousepos[0], mousepos[1], 20, 20)
TypeError: must be pygame.Surface, not int

我知道我在做什么错:draw.rect()的参数属于无效类型,但是我不知道如何更改参数,因此它们是适当的.那么我该如何解决呢?

I know what I'm doing wrong: my parameters for draw.rect() are of invalid types, but I don't know how to change the parameters so they're appropriate. So how do I fix this?

推荐答案

让我们看一下函数定义:

Lets take a look at the function definition:

pygame.draw.rect(Surface, Color, Rect, Thickness)

  • 表面是您要绘制的表面
  • 颜色是具有RGB值的小号,用于定义要使用的颜色
  • Rect是一个连音符,格式为:(x,y,width,height)
    • x,y是左上角的坐标
    • 宽度,高度是矩形的宽度和高度
      • Surface is a surface where you want to draw
      • Color is a tupple with RGB values defining the color to be used
      • Rect is a tupple in the format: (x,y,width,height)
        • x,y are the coordinates of the upper left hand corner
        • width, height are the width and height of the rectangle
        • 基于此,您应该执行以下操作:

          Based on this, you shoud do something like:

          redColor = (255,0,0)
          pygame.draw.rect(screen, redColor, (mousepos[0], mousepos[1], 20, 20), 1)
          

          来源:

          python.draw的官方文档可以在这里找到:
          http://www.pygame.org/docs/ref/draw.html

          Sources:

          The official documentation for python.draw can be found here:
          http://www.pygame.org/docs/ref/draw.html

          在每个功能描述下注意有用的Search examples for <function>按钮,这可以带您获得多个实际的用法示例.

          Mind the usefull Search examples for <function> button under every function description, which can lead you to multiple real world examples of usage.

          在官方页面上也可以找到有用的教程: http://www.pygame.org/wiki /tutorials

          Useful tutorials can also be found on the official pages: http://www.pygame.org/wiki/tutorials

          其他一些非官方的教程,例如,可以通过谷歌搜索来找到.

          Other unofficial tutorials, like this one, can be found with a bit of Googling effort.

          这篇关于PyGame:draw.rect()具有无效的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆