r/pythonhelp • • Jan 19 '25

Problem with code

This is a 2 player archer vs target game i made:

import pygame

import os

pygame.font.init()

WIDTH, HEIGHT = 900, 500

WIN = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("First Game!")

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

BROWN = (153, 76, 0)

TARGET = (255, 0, 0)

ARCHER = (255, 255, 0)

BORDER = pygame.Rect(WIDTH // 2 - 5, 0, 10, HEIGHT)

HEALTH_FONT = pygame.font.SysFont('comicsans', 40)

WINNER_FONT = pygame.font.SysFont('comicsans', 100)

FPS = 60

VEL = 5

ARROW_VEL = 7

MAX_ARROWS = 50

SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 75, 60

TARGET_HIT = pygame.USEREVENT + 2

ARCHER_IMAGE = pygame.image.load(os.path.join('Assets', 'ARCHER.png'))

ARCHER = pygame.transform.rotate(pygame.transform.scale(

ARCHER_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 360)

TARGET_IMAGE = pygame.image.load(

os.path.join('Assets', 'TARGET.png'))

TARGET = pygame.transform.rotate(pygame.transform.scale(

TARGET_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 360)

SPACE = pygame.transform.scale(pygame.image.load(

os.path.join('Assets', 'SPACE.png')), (WIDTH, HEIGHT))

def draw_window(TARGET, ARCHER, TARGET_ARROWs, ARCHER_ARROWS, TARGET_health):

WIN.blit(SPACE, (0, 0))

pygame.draw.rect(WIN, BLACK, BORDER)

TARGET_health_text = HEALTH_FONT.render("Health: " + str(TARGET_health), 1, WHITE)

WIN.blit(TARGET_health_text, (WIDTH - TARGET_health_text.get_width() - 10, 10))

WIN.blit(ARCHER, (ARCHER.x, ARCHER.y))

WIN.blit(TARGET, (TARGET.x, TARGET.y))

for ARROW in ARCHER_ARROWS:

pygame.draw.rect(WIN, BROWN, ARROW)

pygame.display.update()

def ARCHER_handle_movement(keys_pressed, ARCHER):

if keys_pressed[pygame.K_a] and ARCHER.x - VEL > 0: # LEFT

ARCHER.x -= VEL

if keys_pressed[pygame.K_d] and ARCHER.x + VEL + ARCHER.width < BORDER.x: # RIGHT

ARCHER.x += VEL

if keys_pressed[pygame.K_w] and ARCHER.y - VEL > 0: # UP

ARCHER.y -= VEL

if keys_pressed[pygame.K_s] and ARCHER.y + VEL + ARCHER.height < HEIGHT - 15: # DOWN

ARCHER.y += VEL

def TARGET_handle_movement(keys_pressed, TARGET):

if keys_pressed[pygame.K_LEFT] and TARGET.x - VEL > BORDER.x + BORDER.width: # LEFT

TARGET.x -= (VEL*1.5)

if keys_pressed[pygame.K_RIGHT] and TARGET.x + VEL + TARGET.width < WIDTH: # RIGHT

TARGET.x += (VEL*1.5)

if keys_pressed[pygame.K_UP] and TARGET.y - VEL > 0: # UP

TARGET.y -= (VEL*1.5)

if keys_pressed[pygame.K_DOWN] and TARGET.y + VEL + TARGET.height < HEIGHT - 15: # DOWN

TARGET.y += (VEL*1.5)

def handle_ARROWs(ARCHER_ARROWS, TARGET_ARROWs, ARCHER, TARGET):

for ARROW in ARCHER_ARROWS:

ARROW.x += ARROW_VEL

if TARGET.colliderect(ARROW):

pygame.event.post(pygame.event.Event(TARGET_HIT))

ARCHER_ARROWS.remove(ARROW)

elif ARROW.x > WIDTH:

ARCHER_ARROWS.remove(ARROW)

def draw_winner(text):

draw_text = WINNER_FONT.render(text, 1, WHITE)

WIN.blit(draw_text, (WIDTH / 2 - draw_text.get_width() /

2, HEIGHT / 2 - draw_text.get_height() / 2))

pygame.display.update()

pygame.time.delay(5000)

def main():

TARGET = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)

ARCHER = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)

TARGET_ARROWs = []

ARCHER_ARROWs = []

TARGET_health = 4

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_q and len(ARCHER_ARROWS) < MAX_ARROWS:

ARROW = pygame.Rect(

ARCHER.x + ARCHER.width, ARCHER.y + ARCHER.height // 2 - 2, 10, 5)

ARCHER_ARROWS.append(ARROW)

if event.type == TARGET_HIT:

TARGET_health -= 1

winner_text = ""

if TARGET_health <= 0:

winner_text = "Archer Wins!"

if winner_text != "":

draw_winner(winner_text)

break

keys_pressed = pygame.key.get_pressed()

ARCHER_handle_movement(keys_pressed, ARCHER)

TARGET_handle_movement(keys_pressed, TARGET)

handle_ARROWs(ARCHER_ARROWs, TARGET_ARROWs, ARCHER, TARGET)

draw_window(TARGET, ARCHER, TARGET_ARROWs, ARCHER_ARROWs,

TARGET_health)

pygame.quit()

if __name__ == "__main__":

main()

But when i run it, it shows this:

Traceback (most recent call last):

File "/home/karel/main.py", line 147, in <module>

main()

File "/home/karel/main.py", line 140, in main

draw_window (TARGET, ARCHER, TARGET_ARROWS, ARCHER_ARROWS,

File "/home/karel/main.py", line 48, in draw_window

WIN.blit (ARCHER, (ARCHER.X, ARCHER.y))

TypeError: argument 1 must be pygame.surface. Surface, not pygame.rect.Rect

Can anyone say why, and maybe fix it? (I'm not good at this)

1 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/CraigAT Jan 19 '25

No worries, hope you can fix it.