0
# DONT CHANGE THIS
import pygame, os
pygame.init()
WIDTH, HEIGHT = 450, 250
WIN = pygame.display.set_mode((WIDTH, HEIGHT))   
pygame.display.set_caption("game")

# VARIABLES
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 5, HEIGHT)
FPS = 60
VEL = 10
SHIP_WIDTH, SHIP_HEIGHT, = 25, 20
BULLETS_VEL = 3.5
MAX_BULLETS = 1000
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', "spaceship_yellow.png"))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(YELLOW_SPACESHIP_IMAGE, (SHIP_HEIGHT, SHIP_WIDTH)), 90)
RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', "spaceship_red.png"))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE, (SHIP_WIDTH, SHIP_HEIGHT)), 270)

# PLACE ANYTHING ON THE WINDOW HERE
# +y = down from top left 
# +x = right from top left
def draw_window(red, yellow, red_bullets, yellow_bullets):
  WIN.fill(BLUE)
  pygame.draw.rect(WIN, BLACK, BORDER)
  WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
  WIN.blit(RED_SPACESHIP, (red.x, red.y))
  for bullet in red_bullets:
    pygame.draw.rect(WIN, red, bullet)
  for bullet in yellow_bullets:
    pygame.draw.rect(WIN, yellow, bullet)

  
  pygame.display.update() 

# MOVEMENT
def yellow_movement(keys_pressed, yellow):
  if keys_pressed[pygame.K_a] and yellow.x - VEL > 0:  #left
      yellow.x -= VEL
  if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x:  #right
      yellow.x += VEL
  if keys_pressed[pygame.K_w] and yellow.y - VEL > 0:  #up
      yellow.y -= VEL
  if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT:  #dowm
      yellow.y += VEL

def red_movement(keys_pressed, red):
  if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width:  #left
      red.x -= VEL
  if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH:  #right
      red.x += VEL
  if keys_pressed[pygame.K_UP] and red.y - VEL > 0:  #up
      red.y -= VEL
  if keys_pressed[pygame.K_DOWN]  and red.y + VEL + red.height < HEIGHT:  #dowm
      red.y += VEL

def handle_bullets(yellow_bullets, red_bullets, yellow, red):
  for bullet in yellow_bullets:
    bullet.x += BULLETS_VEL
    if red.colliderect(bullet):
      pygame.event.post(pygame.event.Event(RED_HIT))
      yellow_bullets.remove(bullet)
      
  for bullet in red_bullets:
    bullet.x += BULLETS_VEL
    if yellow.colliderect(bullet):
      pygame.event.post(pygame.event.Event(YELLOW_HIT))
      red_bullets.remove(bullet)
    
    
    
# MAIN LOOP 
def main():
  red = pygame.Rect(350, 150,SHIP_WIDTH, SHIP_HEIGHT)
  yellow = pygame.Rect(50, 150, SHIP_WIDTH, SHIP_HEIGHT)

  red_bullets = []
  yellow_bullets = []
  
  clock = pygame.time.Clock()
  run = True
  while run:
    clock.tick(FPS)
    for event in pygame.event.get():    
      if event.type == pygame.QUIT:
        run = False 
      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE and len(yellow_bullets) < MAX_BULLETS:
          bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 1, 5, 2.5)
          yellow_bullets.append(bullet)

        if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
          bullet = pygame.Rect(red.x, red.y + red.height//2 - 1, 5, 2.5)
          red_bullets.append(bullet)


    keys_pressed = pygame.key.get_pressed()
    yellow_movement(keys_pressed, yellow)
    red_movement(keys_pressed, red)

    handle_bullets(yellow_bullets, red_bullets, yellow, red)
    
    draw_window(red, yellow, red_bullets, yellow_bullets)
  pygame.quit()

# this is to make sure its opening the right thing blah blaho
if __name__ == "__main__":
   

Error message

I dont understand why I keep getting these errors. is it because "red" is a color so its trying to place a color where it shouldnt be? Also sorry if this is so much to skim over, Im new to pygame so I dont want to leave anything out that would help someone find and resolve the issue.

red = pygame.Rect(350, 150,SHIP_WIDTH, SHIP_HEIGHT)

Line 78 is where it defines red.

4 Answers4

1

Look what value you've assigned to red:

red = pygame.Rect(350, 150,SHIP_WIDTH, SHIP_HEIGHT)

That doesn't quite look like a color to me.

On line 33 you have written this:

pygame.draw.rect(WIN, red, bullet)

Make sure you pass a valid "color" value, not a Rect.

This is what I found here:

color (Color or int or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])

at54321
  • 8,726
  • 26
  • 46
0

In your code, red is defined to be a rectangle. However, according to the pygame documentation, pygame.draw.rect's second argument must be a color.

Nonlinear
  • 684
  • 1
  • 12
0

Perhaps you should define the red and yellow colours you need:

# VARIABLES
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
...
# You seem to be ok using a colour here:
pygame.draw.rect(WIN, BLACK, BORDER)
...
  for bullet in red_bullets:
    pygame.draw.rect(WIN, RED, bullet)
  for bullet in yellow_bullets:
    pygame.draw.rect(WIN, YELLOW, bullet)
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

You defined red to be a Rect and in your draw_window() function you ask for red as a parameter (since red is a constant colour, having it as a parameter is rather superfluous). Instead, do it like so:

def draw_window(red, yellow, red_bullets, yellow_bullets):
    RED = (255, 0, 0)
    YELLOW = (255, 233, 0)
    WIN.fill(BLUE)
    pygame.draw.rect(WIN, BLACK, BORDER)
    WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
    WIN.blit(RED_SPACESHIP, (red.x, red.y))
    for bullet in red_bullets:
        pygame.draw.rect(WIN, RED, bullet)
    for bullet in yellow_bullets:
        pygame.draw.rect(WIN, YELLOW, bullet)

    pygame.display.update()

Now copy and paste wont do it because I'm assuming if you defined red to be a Rect, you want to draw it in your function or similiar but this should fix the error. I also assume yellow will have the same problem. Also in the future it would be nice to explain e.g. what red is supposed to be or do (sometimes while explaining you find the solution) so that it is easier to see what you are driving at and help you :).

eisa.exe
  • 78
  • 9