Arcticcu
Arcticcu

Reputation: 31

Character doesn't change direction like it's supposed to

another pygame problem. I've tried to make a square move around on the screen in a formation of a, well, square. It works fine: first it goes down, then it turns to right, then it starts going up, then left, but as it gets to the point where it's supposed to go down, it simply doesn't and instead continues forward and out of the screen. Here's what I think is the relevant part

import pygame
from pygame.locals import *
from render import *
from player import *
import sys
pygame.init()

class collisions():
    square1x = 50
    square1y = 60
    direction1 = "down"
    vel1y = 0
    vel1x = 0

    square2x = 0
    square2y = 0
    direction2 = -1

    square3x = 0
    square3y = 0
    direction3 = -1

    square4x = 0
    square4y = 0
    direction4 = -1

    square5x = 0
    square5y = 0
    direction5 = -1

    green = pygame.Color(0,255,0)
def hitbox():
    red = pygame.Color(255,0,0)
    time = 1000/50
    playerhb = pygame.rect.Rect(playerObj.x, playerObj.y,20,20)
    if collisions.square1x < 40:
        collisions.direction1 = "down"
    if collisions.square1y > 200:
        collisions.direction1 = "right"
    if collisions.square1x > 600:
        collisions.direction1 = "up"
    if collisions.square1y < 50:
        collisions.direction1 = "left"


    if collisions.direction1 == "down":
        collisions.vel1y = 0.02*time
        collisions.vel1x = 0
    if collisions.direction1 == "right":
        collisions.vel1x = 0.02*time
        collisions.vel1y = 0
    if collisions.direction1 == "up":
        collisions.vel1y = -0.02*time
        collisions.vel1x = 0
    if collisions.direction1 == "left":
        collisions.vel1x = -0.02*time
        collisions.vel1y = 0

    collisions.square1x = collisions.square1x+collisions.vel1x
    collisions.square1y = collisions.square1y+collisions.vel1y
    enemy = pygame.rect.Rect(collisions.square1x,collisions.square1y,20,20)


    draw(enemy,playerhb)

    if enemy.colliderect(playerhb):
        pygame.quit()
        sys.exit()
    pygame.display.flip()

Ignore all the other square coordinates and directions, I'm planning on making more of the squares move around on the screen.

Upvotes: 1

Views: 151

Answers (1)

ndvo
ndvo

Reputation: 989

There is a some problem with your sequence of "if". You cannot check only x or y axis in order to decide whether to change directions. You need to check both axis (or make sure the square goes back 1 pixel when the goal is reached).

I believe the code bellow will fix the problem with changing collisions.direction1.

if collisions.square1x < 40 and collisions.square1y < 50:
    collisions.direction1 = "down"
if collisions.square1y > 200 and collisions.square1x < 40:
    collisions.direction1 = "right"
if collisions.square1x > 600 and collision.square1y > 200:
    collisions.direction1 = "up"
if collisions.square1y < 50 and collisions.square1x > 600:
    collisions.direction1 = "left"

Upvotes: 0

Related Questions