Python Forum
[PyGame] my pygame car wont move
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] my pygame car wont move
#1
Question 
hi like it says in the title my pygame car wont move unless the mouse is on the window and actively moving about. the window itself is updating (i know because the values change according to key presses)

I'm using pygame 2.3.0 (SDL 2.24.2, Python 3.7.9) in visual studio i've also just linked the code file
import pygame
import math


class Envir:
    def __init__(self, dimensions):
        #  colours
        self.black = (0, 0, 0)
        self.white = (255, 255, 255)
        self.green = (0, 255, 0)
        self.blue = (0, 0, 255)
        self.red = (255, 0, 0)
        self.yellow = (255, 255, 0)

        #  Map dimensions
        self.height = dimensions[0]
        self.width = dimensions[1]

        #  Window Settings
        pygame.display.set_caption("Differential Drive Car")
        self.map = pygame.display.set_mode((self.width,
                                            self.height))
        
        #  Text variables
        self.txt = "default text"
        self.font = pygame.font.Font('freesansbold.ttf',50)
        self.txt = self.font.render('default', True, self.white, self.black)
        self.txtRect = self.txt.get_rect()
        self.txtRect.center = (dimensions [1] - 600,
                               dimensions [0] - 100)
        
    def write_info(self, Vl, Vr, theta):
        txt = f"Vl = {Vl} Vr = {Vr}  theta = {int(math.degrees(theta))}"
        self.txt = self.font.render (txt, True, self.white, self.black)
        self.map.blit(self.txt, self.txtRect)



class Robot:
    def __init__(self, startpos, car, width):
        self.m2p = 3779.52  # meters to pixels

        #  robot dims
        self.w = width
        self.x = startpos[0]
        self.y = startpos[1]
        self.theta = 0
        self.vl = 0.01 * self.m2p  # speed in meters per second initially before multiplication
        self.vr = 0.01 * self.m2p
        self.maxspeed = 0.02 * self.m2p
        self.minspeed = 0.02 * self.m2p

        #  graphics
        self.img = pygame.image.load(car)
        self.rotated = self.img
        self.rect = self.rotated.get_rect(center=(self.x,
                                                  self.y))

    def draw(self, map):
        map.blit(self.rotated, self.rect)


    def move(self, event=None):

        #  input keys for the robot
        if event is not None:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    self.vl += 0.001 * self.m2p
                elif event.key == pygame.K_2:
                    self.vl -= 0.001 * self.m2p
                elif event.key == pygame.K_9:
                    self.vr += 0.001 * self.m2p
                elif event.key == pygame.K_8:
                    self.vr -= 0.001 * self.m2p
        
        #  moving the robot
        self.x += ((self.vl+self.vr)/2) * math.cos(self.theta) * dt
        self.y -= ((self.vl+self.vr)/2) * math.cos(self.theta) * dt
        self.theta += (self.vr - self.vl)/self.w * dt

        #  rotating the robot
        self.rotated = pygame.transform.rotozoom(self.img,
                                                 math.degrees(self.theta), 1)
        self.rect = self.rotated.get_rect(center=(self.x, self.y))




#  Initialisation
pygame.init()

start = (200, 200)  # Start position
dims = (800, 1200)  # dimensions
running = True  # Running or not
environment = Envir(dims)  # the envir
dt = 0
lasttime = pygame.time.get_ticks()

#  Robot initialization
robot = Robot(start, r"C:\Users\mrash\OneDrive\Diff drive robot\car.png",
              0.01 * 3779.52)
#  Simulation loop
while running:
    for event in pygame.event.get():  # Activating the quit button
        if event.type == pygame.QUIT:
            running = False
        robot.move(event)
        
    dt = (pygame.time.get_ticks() - lasttime)/1000
    lasttime = pygame.time.get_ticks()
    pygame.display.update()
    environment.map.fill(environment.black)
    robot.draw(environment.map)
    environment.write_info(int(robot.vl),
                           int(robot.vr),
                           robot.theta)

Attached Files

.py   main.py (Size: 3.8 KB / Downloads: 147)
Reply
#2
nvm solved it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] pygame-manu : using controller buttons to move around menu mlw19mlw91 2 1,640 Mar-12-2023, 01:55 PM
Last Post: deanhystad
Exclamation pong paddles wont move skullkat232 5 2,363 Feb-13-2023, 03:53 PM
Last Post: Vadanane
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,315 Nov-10-2021, 03:38 PM
Last Post: metulburr
  Getting a ship to move in pygame djwilson0495 2 3,641 Dec-09-2020, 11:03 AM
Last Post: djwilson0495
  Text wont draw fierygaming 1 2,996 Jul-28-2018, 10:39 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020