0

I created a circle in pygame. The circle moves to wherever you click, but instead of "walking" over there, it just appears. I tried some ways, but it doesn't work. If you could find out a way to do it, that would be great. The moving function is in the move function in the Player class.

# import
import pygame

# initialize pygame
pygame.init()

# frame rate variables
FPS = 120
clock = pygame.time.Clock()

# game variables
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
mouse_pos = ''

# colors
BLUE = (0, 0, 255)

# activate screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Bonker')


class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        # init the sprite class
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.mouse_x = 0
        self.mouse_y = 0

    def move(self):
        # delta x and delta y
        dx = 0
        dy = 0
        # extract info from tuple
        (x, y) = mouse_pos
        self.mouse_x = x
        self.mouse_y = y
        # create tuple destination and current position tuple
        destination = (self.mouse_x, self.mouse_y)
        current_pos = [self.x, self.y]
        # draw the rectangle
        if current_pos[0] >= SCREEN_WIDTH // 2:
            dx = 10
            self.x += dx
        if current_pos[0] < SCREEN_WIDTH // 2:
            dx = -10
            self.x += dx
        if current_pos[1] >= SCREEN_HEIGHT // 2:
            dy = 10
            self.y += dy
        if current_pos[1] < SCREEN_HEIGHT // 2:
            dy = -10
            self.y += dy
        pygame.draw.circle(screen, BLUE, (self.x, self.y), 20)

    def draw(self):
        # draw the circle
        pygame.draw.circle(screen, BLUE, (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2), 20)


# create instances
# player instance
player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
player.draw()

# main loop
run = True
while run:
    # run frame rate
    clock.tick(FPS)
    # events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            screen.fill((0, 0, 0))
            mouse_pos = pygame.mouse.get_pos()
            player.move()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False

    pygame.display.update()

pygame.quit()

I think some code is not needed Help would be appreciated

  • when i run the code,every time I click it move 10 pixel it that right?I'm not very sure u trying to do is move to the position I clicked or move to the direction I clicked? – pepper Jun 11 '21 at 01:00

1 Answers1

0

This will move the circle to the mouse position. It doesn't move in one diagonal, but that can be changed.

# import
import pygame

# initialize pygame
pygame.init()

# frame rate variables
FPS = 120
clock = pygame.time.Clock()

# game variables
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800

# colors
BLUE = (0, 0, 255)

# activate screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Bonker')


class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        # init the sprite class
        pygame.sprite.Sprite.__init__(self)
        self.rect = pygame.Rect(0, 0, 40, 40)
        self.rect.x = x
        self.rect.y = y
        self.radius = 20
        self.destination = None
        self.moving = False
        self.dx = 0
        self.dy = 0
        
    def set_destination(self, pos):
        self.destination = pos
        # delta x and delta y
        self.dx = self.destination[0] - self.rect.centerx 
        self.dy = self.destination[1] - self.rect.centery
        
        self.moving = True
        
    def move(self):
        if self.rect.centerx != self.destination[0]:
            if self.dx > 0:
                self.rect.centerx += 1
            elif self.dx < 0:
                self.rect.centerx -= 1
    
        if self.rect.centery != self.destination[1]:
            if self.dy > 0:
                self.rect.centery += 1
            elif self.dy < 0:
                self.rect.centery -= 1
        elif self.rect.center == self.destination:
            self.moving = False
            
    def draw(self):
        # draw the circle
        pygame.draw.circle(screen, BLUE, self.rect.center, self.radius)


# create instances
# player instance
player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
player.draw()

# main loop
run = True
movetime = 100
move = False

while run:
    # run frame rate
    dt = clock.tick(FPS)
    movetime -= dt
    if movetime <= 0:
        move = True
        movetime = 400
        
    # events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            player.set_destination(mouse_pos)
                
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False

    if player.moving:
        player.move()
    
    screen.fill((0, 0, 0))
    player.draw()
    pygame.display.update()

pygame.quit()
marienbad
  • 1,461
  • 1
  • 9
  • 19