问题
I've been looking for a solution for this error for about 15 minutes now and no posts or other websites covered the specific error i've been having. Here's the code snippet in question:
import pygame
win = pygame.display.set_mode((100,100))
width = 10
height = 40
pipex = 10
pipe1y = 10
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pygame.win.blit(pipe1, pipex, pipe1y)
pygame.display.update()
And here's the full code:
import pygame
import time
import random
from bird import brd
pygame.init()
win = pygame.display.set_mode((400,600))
pygame.display.set_caption('Flappy Bird')
pipex = 345
pipe1y = -270
pipe2y = 420
width = 65
height1 = 400
height2 = 3000
vel = 5
bird_x = 20
bird_y = 300
isJump = 0
jumpCount = 0
yy = 0
class fb:
def move():
global pipex
global yy
global pipe1y
global pipe2y
global bird_x
global bird_y
pipex -= 1
if pipex < -60:
pipex = 345
yy = random.randint(-350,0)
pipe1y = yy
pipe2y = pipe1y + 555
pygame.draw.rect(win,(0,0,0), (0,0,1000,1000))
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pipe2 = pygame.draw.rect(win,(0, 255, 0), (width, height2))
bird = pygame.draw.rect(win,(255,0,0),(30, 30))
pygame.win.blit(pipe1, pipex, pipe1y)
pygame.win.blit(pipe2, pipex, pipe2y)
pygame.win.blit(bird, bird_x, bird_y)
pygame.display.update()
bird_y += 2
def gameover():
pygame.quit()
def checks():
global yy
global bird_x
global bird_y
global pipex
if bird_y > 600:
fb.gameover()
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
bird_y -= 4
if keys[pygame.K_DOWN]:
bird_y += 1
fb.move()
time.sleep(0.005)
fb.checks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
I need the rects to be objects or variables so i can check for collision later down the track.
回答1:
The 3rd argument of pygame.draw.rect() defines the rectangular which is filled by the color. It is a tuple with 4 components (x, y, width, height). (x, y) defien the top left corner of the rectangle and (width, height) defines the size of the rectangle.
In your case (pipex, pipe1y) seem to be the position of the rectnagle:
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pipe1 = pygame.draw.rect(win, (0, 255, 100), (pipex, pipe1y, width, height1))
来源:https://stackoverflow.com/questions/63954352/my-pygame-rects-are-giving-a-rect-argument-is-invalid-error