Character moving with keyboard inputs without actually moving on screen

Viewed 39

So i have a problem where my character is drawn at the first frame and i am able to move and collect "ammo" with it but my character is invisible and i have no idea why it is happening. Also tried to change something from Vector to rect but it does not works.

import pygame
from sys import exit
from pygame.math import Vector2
import random

pygame.init()
win = pygame.display.set_mode((800,600))
pygame.display.set_caption("Rectshooter")
clock = pygame.time.Clock()
game_active = True


class Player :
    width = height = 50

    def __init__(self,color=(255,0,0)):
        self.body = Vector2(500,200)
        self.color = color
        self.direction = Vector2(0,0)
        self.player_rect = pygame.Rect(self.body.x,self.body.y,self.width,self.height)



    def draw(self):
        #pygame.draw.rect(win,self.color,(self.body.x,self.body.y,self.width,self.height))
        pygame.draw.rect(win, self.color, self.player_rect)

    def move(self):
        self.body += self.direction

class Player2 :
    width = height = 50

    def __init__(self,color=(255,0,0)):
        self.body = Vector2(300,200)
        self.color = color
        self.direction = Vector2(0,0)




    def draw(self):
        pygame.draw.rect(win,self.color,(self.body.x,self.body.y,self.width,self.height))

    def move(self):
        self.body += self.direction

class Ammo:
    width = height = 30

    def __init__(self,x1,x2):
        self.x = random.randint(x1,x2)
        self.y = random.randint(50,550)
        self.pos = Vector2(self.x,self.y)
        self.ammo_rect = pygame.Rect(self.x,self.y,self.width,self.height)

    def draw_ammo(self):
        pygame.draw.ellipse(win,(255,100,00),self.ammo_rect)









player1 = Player()
player2 = Player2()
speed = 15 #player speed
ammo1 = Ammo(50,300)
ammo2 = Ammo(450,750)
print(ammo2.pos.x)
print(player1.body.x)

There is another problem with colliding, i wrote a long if statement for it and it works fine,but i want to make it with simple "player1.player_rect.colliderect(ammo2.ammo_rect)" if statement,but this only triggers when the ammo(random spawn) spwans at the location of the player,i mean the original location of the player that spawns at the first frame not the current location. I am so confused,and i need a little help with it.

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        #player1 key pressing movement
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player1.direction = Vector2(0,-speed)
            if event.key == pygame.K_DOWN:
                player1.direction = Vector2(0,speed)
            if event.key == pygame.K_LEFT:
                player1.direction = Vector2(-speed,0)
            if event.key == pygame.K_RIGHT:
                player1.direction = Vector2(speed,0)

        #player2 key pressing movement
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                player2.direction = Vector2(0,-speed)
            if event.key == pygame.K_s:
                player2.direction = Vector2(0,speed)
            if event.key == pygame.K_a:
                player2.direction = Vector2(-speed,0)
            if event.key == pygame.K_d:
                player2.direction = Vector2(speed,0)


    if game_active:
        #player1 preventing from going out of the screen and the middle line
        if player1.body.y < 0:#up
            player1.body.y = 0
            player1.direction = Vector2(0,0)
        if player1.body.x < 410:#left
            player1.body.x = 410
            player1.direction = Vector2(0,0)
        if player1.body.y > 550:#down
            player1.body.y = 550
            player1.direction = Vector2(0,0)
        if player1.body.x > 750:#right
            player1.body.x = 750
            player1.direction = Vector2(0,0)

        #player2 preventing from going out of the screen and the middle line
        if player2.body.y < 0:#up
            player2.body.y = 0
            player2.direction = Vector2(0,0)
        if player2.body.x < 0:#left
            player2.body.x = 0
            player2.direction = Vector2(0,0)
        if player2.body.y > 550:#down
            player2.body.y = 550
            player2.direction = Vector2(0,0)
        if player2.body.x > 341:#right
            player2.body.x = 341
            player2.direction = Vector2(0,0)

        #collide p1 with ammo1
        if ammo2.pos.x + 20 > player1.body.x - 20 and ammo2.pos.x - 20 < player1.body.x + 20 and ammo2.pos.y + 20 > player1.body.y - 20 and ammo2.pos.y - 20 < player1.body.y + 20:
            ammo2 = Ammo(450,750)
            ammo2.draw_ammo()
            print("collide")
        if player1.player_rect.colliderect(ammo2.ammo_rect):
            print("collide with start position")

        #print(player1.body.x)
        #print(player1.body.y)





        win.fill((0, 0, 0))
        pygame.draw.line(win, (255, 255, 255), (400, 600), (400, 0), 20)
        #p1
        player1.draw()
        player1.move()

        #p2
        player2.draw()
        player2.move()

        #ammo
        ammo1.draw_ammo()
        ammo2.draw_ammo()





        pygame.display.update()
        clock.tick(60)
1 Answers

The player is drawn at the position stored in the player_rect attribute. When you change the body attribute, the position of the rectangle does not magically change. You need to update the position of the rectangle:

class Player :
    width = height = 50

    def __init__(self,color = (255,0,0)):
        self.body = Vector2(500, 200)
        self.color = color
        self.direction = Vector2(0, 0)
        self.player_rect = pygame.Rect(self.body.x, self.body.y, self.width, self.height)

    def draw(self):
        pygame.draw.rect(win, self.color, self.player_rect)

    def move(self):
        self.body += self.direction

        # update player_rect
        self.player_rect.x = round(self.body.x)
        self.player_rect.y = round(self.body.y)
Related