最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Rect not working with screen.draw.filled_rect in pygame zero - Stack Overflow

programmeradmin0浏览0评论

Right now i am coding a pygame zero program It is at its first steps and i am trying to make the play button why isn't it drawing? I am using screen.draw.filled_rect(text, rect, kwaargs**)

Here is my code

import pgzrun
def draw():
    global x, play_button
    screen.clear()
    screen.blit('actualbg', (0, 0))
    #screen.fill((128, 0, 0))
    if first_screen:
        screen.draw.text('Marvel run', center=(370, 100), fontname='impact', fontsize = 100, color="orange", owidth=1)
        screen.draw.filled_rect(play_button, 'yellow')
        screen.draw.textbox('Play', play_button, fo1ntname='impact', color='black')
   
    #to be continued

WIDTH = 800
HEIGHT = 590
first_screen = True
play_screen = False
option_screen = False
play_button = Rect(100, 50)

play_button.midtop=(0, 0)
play_button.move_ip(383, 266)
first_screen = True
play_screen = False
option_screen = False

pgzrun.go()

I have looked at so many websites but they are no help:(

I ran it and it came up with


  File "marvel.py", line 21, in <module>
    play_button = Rect(100, 50)
                  ^^^^^^^^^^^^^
TypeError: Argument must be rect style object

It was supposed to create a yellow rectangle with text inside it and the rectangle is filled Thanks

Right now i am coding a pygame zero program It is at its first steps and i am trying to make the play button why isn't it drawing? I am using screen.draw.filled_rect(text, rect, kwaargs**)

Here is my code

import pgzrun
def draw():
    global x, play_button
    screen.clear()
    screen.blit('actualbg', (0, 0))
    #screen.fill((128, 0, 0))
    if first_screen:
        screen.draw.text('Marvel run', center=(370, 100), fontname='impact', fontsize = 100, color="orange", owidth=1)
        screen.draw.filled_rect(play_button, 'yellow')
        screen.draw.textbox('Play', play_button, fo1ntname='impact', color='black')
   
    #to be continued

WIDTH = 800
HEIGHT = 590
first_screen = True
play_screen = False
option_screen = False
play_button = Rect(100, 50)

play_button.midtop=(0, 0)
play_button.move_ip(383, 266)
first_screen = True
play_screen = False
option_screen = False

pgzrun.go()

I have looked at so many websites but they are no help:(

I ran it and it came up with


  File "marvel.py", line 21, in <module>
    play_button = Rect(100, 50)
                  ^^^^^^^^^^^^^
TypeError: Argument must be rect style object

It was supposed to create a yellow rectangle with text inside it and the rectangle is filled Thanks

Share Improve this question edited Mar 2 at 6:55 Rabbid76 212k30 gold badges160 silver badges200 bronze badges asked Mar 2 at 6:35 user29854913user29854913 12 bronze badges 2
  • Rect needs 4 arguments, left, top, width, height – Rabbid76 Commented Mar 2 at 6:56
  • Thank you! Turns out you do need to make the width and height a tuple! Same for the left and top as well. the websites were pretty vague on whether you NEED them or not – user29854913 Commented Mar 2 at 7:39
Add a comment  | 

1 Answer 1

Reset to default 1

A Rect consists of a position and a size. The position is the left top corner of the rectangle and the size is the with and height e.g.:

play_button = Rect((0, 0), (100, 50))

You can find more information in the Pygame documentation: pygame.Rect

发布评论

评论列表(0)

  1. 暂无评论