Answer the question
In order to leave comments, you need to log in
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
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
if hunter_1.y < pos_y_rand:
hunter_1.y += 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question