How do i test for any players on some specific coordinates?


Joined
Jan 3, 2022
Messages
6
Reaction score
0
Hi, im very new to coding scince i started only a couple mounths ago. Right now im trying to create a 2D game. I made basic movement with pygame. I would like to add a feature when i go to specific coordinates that something happends or the game ends but i don't know how to. Could anyone help me.

This is the code:


Python:
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10

run = True

while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    win.fill((0, 0, 0))
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()

pygame.quit()
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
174
Reaction score
19
Pygame provides a Rect class that can check for intersection with a point or with another Rect. The following is just the simplest way to test the function (just go straight down and right and it'll turn green when it enters target_rect). In reality, you might store a list of zones to check and maybe associate them with functions/events to call when it happens.

Python:
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10

run = True

while run:
    pygame.time.delay(100)

    target_rect = pygame.Rect(100, 100, 100, 100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    win.fill((0, 0, 0))
    if target_rect.collidepoint(x, y):
        pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
    else:   
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

    pygame.display.update()

pygame.quit()
 
Joined
Jan 3, 2022
Messages
6
Reaction score
0
Pygame provides a Rect class that can check for intersection with a point or with another Rect. The following is just the simplest way to test the function (just go straight down and right and it'll turn green when it enters target_rect). In reality, you might store a list of zones to check and maybe associate them with functions/events to call when it happens.

Python:
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10

run = True

while run:
    pygame.time.delay(100)

    target_rect = pygame.Rect(100, 100, 100, 100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    win.fill((0, 0, 0))
    if target_rect.collidepoint(x, y):
        pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
    else:  
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

    pygame.display.update()

pygame.quit()
Thank you so much. This helps a lot. But I have another question that I can't solve. Im trying to make a running mechanic. Like in games when u hold shift the character accelerates. I tried doing that but the closest to that I tried making 2 new binds, one that will increse the velocity and the other that would lower it. It just feels kind of crapy to click one key to speed up and another to slow down.
Thank you again.
Python:
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10
run = 10

run = True

while run:
    pygame.time.delay(100)

    target_rect = pygame.Rect(200, 200, 200, 200)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    if keys[pygame.K_c]: # the key that accelerates the caracter
        vel += run

    if keys[pygame.K_v]: # the key that slows him down
        vel -= run

    win.fill((0, 0, 0))
    if target_rect.collidepoint(x, y):
        pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
    else:
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

    pygame.display.update()

pygame.quit()
 
Joined
Jan 3, 2022
Messages
6
Reaction score
0
I can't do the whole thing right now, but try using the get_mods function.
Like this?
Python:
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10
boost = 10

run = True

while run:
    pygame.time.delay(100)

    target_rect = pygame.Rect(200, 200, 200, 200)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    key = pygame.key.get_mods()
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    if key[pygame.KMOD_SHIFT]:  # is this correct?
        vel += boost

    win.fill((0, 0, 0))
    if target_rect.collidepoint(x, y):
        pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
    else:
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

    pygame.display.update()

pygame.quit()
 
Joined
Jan 3, 2022
Messages
6
Reaction score
0
I can't do the whole thing right now, but try using the get_mods function.
Never mind the other reply. I managed to get it to work. But it continues to speed up infinitely until i let go of shift, after that the volocity is set to the default. Is there a way to set a max speed? Thank you again for your help!
Python:
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10
boost = 10

run = True

while run:
    pygame.time.delay(100)

    target_rect = pygame.Rect(200, 200, 200, 200)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    key = pygame.key.get_mods()
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    if key & pygame.KMOD_LSHIFT:
        vel += boost

    if not key & pygame.KMOD_LSHIFT:
        vel = 5

    win.fill((0, 0, 0))
    if target_rect.collidepoint(x, y):
        pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
    else:
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

    pygame.display.update()

pygame.quit()
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
174
Reaction score
19
Sorry for the delay. You can, of course, use vel = min(MAX_SPEED, vel+boost) to accelerate to a max speed.
 
Joined
Jan 3, 2022
Messages
6
Reaction score
0
Sorry for the delay. You can, of course, use vel = min(MAX_SPEED, vel+boost) to accelerate to a max speed.
I try adding it but it doesn't work.
Python:
import pygame
pygame.init()

win = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 10
boost = 10

run = True

while run:
    pygame.time.delay(100)
    vel = min(MAX_SPEED, vel+boost)

    target_rect = pygame.Rect(200, 200, 200, 200)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    key = pygame.key.get_mods()
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        x -= vel

    if keys[pygame.K_d]:
        x += vel

    if keys[pygame.K_w]:
        y -= vel

    if keys[pygame.K_s]:
        y += vel

    if key & pygame.KMOD_LSHIFT:
        vel += boost

    if not key & pygame.KMOD_LSHIFT:
        vel = 10

    win.fill((0, 0, 0))
    if target_rect.collidepoint(x, y):
        pygame.draw.rect(win, (0, 255, 0), (x, y, width, height))
    else:
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

    pygame.display.update()

pygame.quit()
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top