u/TheEyebal

How do I get collision to detect properly in pygame
▲ 3 r/pygame+1 crossposts

How do I get collision to detect properly in pygame

Here is what the collision looks like fast that slow
https://imgur.com/a/JNVEdUV

player = Block(500, 300, 20, 20, "blue")

surfaceList = [Block(500, 100, 60, 10, "orange"), 
Block(500, 110, 60, 30, "red"),
Block(500, 140, 60, 10, "orange")]

--- MAIN LOOP ---

# COLLISION
collision_target = None # Track which surface the player is colliding with

target = player.rect.collidelist(surfaceList)

if target != -1:
    collision_target = target

if collision_target == 1 and key[pygame.K_SPACE] == target:
player.color = "green"
else:
player.color = "blue"

# Draw
for i in surfaceList:
    i.drawSurface(screen)

So I am trying to get the collision to detect when spacebar is hit and it works somewhat but it still not detecting properly. How can i fix this issue

u/TheEyebal — 20 hours ago
▲ 13 r/cpp_questions+1 crossposts

Why doe the velocity not work as an integer

int vel_y = 5;
float gravity = 0.3f;

// MAIN LOOP
while(!WindowShouldClose()){
        BeginDrawing(); // Setup canvas
        ClearBackground((Color) {0, 0, 0, 255});

        // Draw Ball
        DrawCircle(ball_x, ball_y, ball_radius, (Color){255, 255, 255, 255});

        // Physics
        vel_y += gravity;
        ball_y += vel_y;

        if (ball_y >= (screen_h - ball_radius)) {
                ball_y = screen_h - ball_radius;  
                vel_y = -vel_y * 0.8f;
        }
}

Why is int vel_y = 5; being an integer causing the ball not bounce
but when it becomes a float it works why?

This is what it looks like after than before

https://imgur.com/a/oxR07xf

u/TheEyebal — 3 days ago

How do I fix the logic for my response

In displayAnswer whenever I press the matching key, it automatically shows the response. What I want is to show the matching key when presses and if I click it again, than show the response

import pygame, string

answer = "basketball".upper()
hints = ["_"] * len(answer)
userInput = "" 

def displayGuessed(surface):
        response = "Letter already selected"

        cursorPos_x = screen_w // 4
        cursorPos_y = screen_h // 2

        createFont = pygame.font.SysFont("Arial.ttf", 35)
        renderFont = createFont.render(response, True, "black")
        surface.blit(renderFont, (cursorPos_x , 20))

def displayAnswers(hints, answer, userInput):
        correctLetter = False

        guessed_letter = []

        if userInput in guessed_letter:
                displayGuessed(screen)
                return "".join(hints)

        for i in range(len(answer)):
                if answer[i] in userInput:
                        hints[i] = answer[i]
                        joinHints = "".join(hints)
                        correctLetter = True

        if correctLetter:
                guessed_letter.append(userInput)
                #displayGuessed(screen)

        return "".join(hints)

if event.type == pygame.KEYDOWN:
        if event.unicode in string.ascii_letters:
                userInput += event.unicode.upper()
                #if len(userInput) != 1:
                        #userInput = userInput[:-1]

        if event.key == pygame.K_BACKSPACE and len(userInput    ) > 0:
                userInput = userInput[:-1]
reddit.com
u/TheEyebal — 5 days ago

How do I fix the logic for my response

In displayAnswer whenever I press the matching key, it automatically shows the response. What I want is to show the matching key when presses and if I click it again, than show the response

import pygame, string

answer = "basketball".upper()
hints = ["_"] * len(answer)
userInput = "" 

def displayGuessed(surface):
        response = "Letter already selected"

        cursorPos_x = screen_w // 4
        cursorPos_y = screen_h // 2

        createFont = pygame.font.SysFont("Arial.ttf", 35)
        renderFont = createFont.render(response, True, "black")
        surface.blit(renderFont, (cursorPos_x , 20))

def displayAnswers(hints, answer, userInput):
        correctLetter = False

        guessed_letter = []

        if userInput in guessed_letter:
                displayGuessed(screen)
                return "".join(hints)

        for i in range(len(answer)):
                if answer[i] in userInput:
                        hints[i] = answer[i]
                        joinHints = "".join(hints)
                        correctLetter = True

        if correctLetter:
                guessed_letter.append(userInput)
                #displayGuessed(screen)

        return "".join(hints)

if event.type == pygame.KEYDOWN:
        if event.unicode in string.ascii_letters:
                userInput += event.unicode.upper()
                #if len(userInput) != 1:
                        #userInput = userInput[:-1]

        if event.key == pygame.K_BACKSPACE and len(userInput    ) > 0:
                userInput = userInput[:-1]
reddit.com
u/TheEyebal — 5 days ago

How do I get the lines to loop to match the length of my word

I am building a hangman game in pygame, and I am struggling to get the lines to iterate so it can be the same number as the length of my word.

Example

My word is "APPLE" so there should be 5 lines _ _ _ _ _
My word is "BALL" so there should be 4 lines _ _ _ _

I wrote a class for the line and placed the loop inside a method.

Here is what I got so far

class displayHints:
    def __init__(self, x_pos, y_pos, color, thickness):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.color = color
        self.start_pos = pygame.math.Vector2(self.x_pos, self.y_pos)
        self.end_pos = pygame.math.Vector2(self.x_pos + 50, self.y_pos)
        self.thickness = thickness

    def drawLines(self, surface, word):
        for i in range(len(word)):
            self.start_pos[0] += 30 * i
            self.end_pos[1] * i

            pygame.draw.line(surface, self.color, self.start_pos, self.end_pos, self.thickness)

How can I properly iterate the lines

SOLVED

I used Text Fonts for the lines instead

reddit.com
u/TheEyebal — 8 days ago