A
A
Alex_Kuzen2021-09-10 08:57:52
Python
Alex_Kuzen, 2021-09-10 08:57:52

Why is the object not moving in pygame?

It is necessary that the red dot move to the white dots, but for some reason it stands still. Sorry for the terrible code, but I did it as best I could.

import pygame
import sys
import random
import time

pygame.init()
win = pygame.display.set_mode((800, 800))
white = (200, 200, 200)
red = (220, 51, 102)
green = (120, 120, 200)
black = (0, 0, 0)


class Hunter:
    white = pygame.Color(220, 100, 100)

    def __init__(self, x, y, h, w, color):
        self.x = x
        self.y = y
        self.h = h
        self.w = w
        self.color = color

    def hunter_show(self):
        pygame.draw.rect(win, (self.color), (self.x, self.y, self.h, self.w))

    def huner_move(self, x, y):
        self.x = x
        self.y = y


class Ex:
    def __init__(self, x, y, h, w, color):
        self.x = x
        self.y = y
        self.h = h
        self.w = w
        self.color = color

    def ex_show(self):
        pygame.draw.rect(win, (self.color), (self.x, self.y, self.h, self.w))

    def ex_position(self, x, y):
        self.x = x
        self.y = y


while True:
    color = white
    h_color = red
    pos_x_rand = random.randint(1, 790)
    pos_y_rand = random.randint(1, 790)

    hunter_pos_x = 0
    hunter_pos_y = 0
    time.sleep(0.0007)
    hunter_1 = Hunter(hunter_pos_x, hunter_pos_y, 5, 5, h_color)
    exl = Ex(pos_x_rand, pos_y_rand, 3, 3, color)
    if hunter_pos_y < pos_y_rand:
        hunter_pos_y += 1
    if hunter_pos_x < pos_x_rand:
        hunter_pos_x += 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()


    exl.ex_show()
    hunter_1.hunter_show()
    pygame.display.update()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2021-09-10
@Alex_Kuzen

Because you are not comparing the coordinates of the hunter_1 object, but not the coordinates associated with it

if hunter_pos_y < pos_y_rand:
        hunter_pos_y += 1

when it should be
if hunter_1.y < pos_y_rand:
        hunter_1.y += 1

Similarly, the coordinates of the targets (pos_y_rand). Now the hunter's coordinates are compared simply with this random point every round, and not with the coordinates of the real object - the target.
In addition, this very hunter is created anew every cycle, although logically I suppose it should be only 1.
Ie . the creation of the hunter object must be moved outside the cycle, and the coordinates should be checked by the attributes of the object, and it should be moved.
Secondly, these targets (stars?) are not saved anywhere in their current form, each cycle the previous target objects are overwritten, leaving only their image on the screen. Those. There will be nowhere to take the coordinates of previously created targets. If, nevertheless, the logic of the work was - to follow these goals, then they need to be stored (the simplest thing is to add to the list).

R
Ruslan., 2021-09-10
@LaRN

Here, you probably need to move the initialization of the hunter position outside the while loop.
I'm talking about this code:
hunter_pos_x = 0
hunter_pos_y = 0
And so, at each iteration of the loop, everything starts from 0.0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question