Wanting to create a nitrous option in my 2D car racing game (pygame), but the car just straight off accelerating at start. What can I do? Thanks /u/babyjonny9898 Python Education

class AbstractCar: def __init__(self, max_vel, rotation_vel): self.img = self.IMG self.max_vel = max_vel self.vel = 0 self.rotation_vel = rotation_vel self.angle = 0 self.x, self.y = self.START_POS self.acceleration = 0.1 self.boosting = False def rotate(self, left=False, right=False): if left: self.angle += self.rotation_vel elif right: self.angle -= self.rotation_vel def draw(self, win): blit_rotate_center(win, self.img, (self.x, self.y), self.angle) def move_forward(self): self.vel = min(self.vel + self.acceleration, self.max_vel) self.move() def move_backward(self): self.vel = max(self.vel - self.acceleration, -self.max_vel/2) self.move() def move(self): radians = math.radians(self.angle) vertical = math.cos(radians) * self.vel horizontal = math.sin(radians) * self.vel self.y -= vertical self.x -= horizontal def collide(self, mask, x=0, y=0): car_mask = pygame.mask.from_surface(self.img) offset = (int(self.x - x), int(self.y - y)) poi = mask.overlap(car_mask, offset) return poi def reset(self): self.x, self.y = self.START_POS self.angle = 0 self.vel = 0 def key_press(self): keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: print("yes") self.boosting = True else: self.boosting = False def change_speed(self): self.vel = 7 self.max_vel = 7 def return_speed(self): self.vel = 4 self.max_vel = 4 def zero_speed(self): if self.vel == 0: return True def change_car_speed(car_instance, new_speed): car_instance.speed = new_speed class PlayerCar(AbstractCar): IMG = RED_CAR START_POS = (180, 200) def reduce_speed(self): self.vel = max(self.vel - self.acceleration / 2, 0) self.move() def bounce(self): self.vel = -self.vel self.move() class ComputerCar(AbstractCar): IMG = GREEN_CAR START_POS = (150, 200) def __init__(self, max_vel, rotation_vel, path=[]): super().__init__(max_vel, rotation_vel) self.path = path #point of path that I wanna to go self.current_point = 0 #need to know what point of the path currently at self.vel = max_vel #constant maximum velocity def draw_points(self, win): for point in self.path: pygame.draw.circle(win ,(255, 0, 0), point, 5) #255,0,0 is the colour of the circle and 5 is the radius def draw(self, win): super().draw(win) self.draw_points(win) #points of the car will move. Will hide it later def calculate_angle(self): target_x, target_y = self.path[self.current_point] x_diff = target_x - self.x y_diff = target_y - self.y if y_diff == 0: desired_radian_angle = math.pi / 2 else: desired_radian_angle = math.atan(x_diff / y_diff) if target_y > self.y: desired_radian_angle += math.pi difference_in_angle = self.angle - math.degrees(desired_radian_angle) if difference_in_angle >= 180: difference_in_angle -= 360 if difference_in_angle > 0: self.angle -= min(self.rotation_vel, abs(difference_in_angle)) else: self.angle += min(self.rotation_vel, abs(difference_in_angle)) def update_path_point(self): target = self.path[self.current_point] rect = pygame.Rect( self.x, self.y, self.img.get_width(), self.img.get_height()) if rect.collidepoint(*target): self.current_point += 1 def move(self): if self.current_point >= len(self.path): return self.calculate_angle() self.update_path_point() super().move() def draw(win, images, player_car, computer_car): for img, pos in images: win.blit(img, pos) player_car.draw(win) computer_car.draw(win) #draw the ComputerCar movement on the screen pygame.display.update() def move_player(player_car): keys = pygame.key.get_pressed() moved = False boosting = False if keys[pygame.K_a]: player_car.rotate(left=True) if keys[pygame.K_d]: player_car.rotate(right=True) if keys[pygame.K_w]: moved = True player_car.move_forward() acc_sound.play(-1) engine_playing = True idle_sound.stop() 

submitted by /u/babyjonny9898
[link] [comments]

​r/learnpython class AbstractCar: def __init__(self, max_vel, rotation_vel): self.img = self.IMG self.max_vel = max_vel self.vel = 0 self.rotation_vel = rotation_vel self.angle = 0 self.x, self.y = self.START_POS self.acceleration = 0.1 self.boosting = False def rotate(self, left=False, right=False): if left: self.angle += self.rotation_vel elif right: self.angle -= self.rotation_vel def draw(self, win): blit_rotate_center(win, self.img, (self.x, self.y), self.angle) def move_forward(self): self.vel = min(self.vel + self.acceleration, self.max_vel) self.move() def move_backward(self): self.vel = max(self.vel – self.acceleration, -self.max_vel/2) self.move() def move(self): radians = math.radians(self.angle) vertical = math.cos(radians) * self.vel horizontal = math.sin(radians) * self.vel self.y -= vertical self.x -= horizontal def collide(self, mask, x=0, y=0): car_mask = pygame.mask.from_surface(self.img) offset = (int(self.x – x), int(self.y – y)) poi = mask.overlap(car_mask, offset) return poi def reset(self): self.x, self.y = self.START_POS self.angle = 0 self.vel = 0 def key_press(self): keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: print(“yes”) self.boosting = True else: self.boosting = False def change_speed(self): self.vel = 7 self.max_vel = 7 def return_speed(self): self.vel = 4 self.max_vel = 4 def zero_speed(self): if self.vel == 0: return True def change_car_speed(car_instance, new_speed): car_instance.speed = new_speed class PlayerCar(AbstractCar): IMG = RED_CAR START_POS = (180, 200) def reduce_speed(self): self.vel = max(self.vel – self.acceleration / 2, 0) self.move() def bounce(self): self.vel = -self.vel self.move() class ComputerCar(AbstractCar): IMG = GREEN_CAR START_POS = (150, 200) def __init__(self, max_vel, rotation_vel, path=[]): super().__init__(max_vel, rotation_vel) self.path = path #point of path that I wanna to go self.current_point = 0 #need to know what point of the path currently at self.vel = max_vel #constant maximum velocity def draw_points(self, win): for point in self.path: pygame.draw.circle(win ,(255, 0, 0), point, 5) #255,0,0 is the colour of the circle and 5 is the radius def draw(self, win): super().draw(win) self.draw_points(win) #points of the car will move. Will hide it later def calculate_angle(self): target_x, target_y = self.path[self.current_point] x_diff = target_x – self.x y_diff = target_y – self.y if y_diff == 0: desired_radian_angle = math.pi / 2 else: desired_radian_angle = math.atan(x_diff / y_diff) if target_y > self.y: desired_radian_angle += math.pi difference_in_angle = self.angle – math.degrees(desired_radian_angle) if difference_in_angle >= 180: difference_in_angle -= 360 if difference_in_angle > 0: self.angle -= min(self.rotation_vel, abs(difference_in_angle)) else: self.angle += min(self.rotation_vel, abs(difference_in_angle)) def update_path_point(self): target = self.path[self.current_point] rect = pygame.Rect( self.x, self.y, self.img.get_width(), self.img.get_height()) if rect.collidepoint(*target): self.current_point += 1 def move(self): if self.current_point >= len(self.path): return self.calculate_angle() self.update_path_point() super().move() def draw(win, images, player_car, computer_car): for img, pos in images: win.blit(img, pos) player_car.draw(win) computer_car.draw(win) #draw the ComputerCar movement on the screen pygame.display.update() def move_player(player_car): keys = pygame.key.get_pressed() moved = False boosting = False if keys[pygame.K_a]: player_car.rotate(left=True) if keys[pygame.K_d]: player_car.rotate(right=True) if keys[pygame.K_w]: moved = True player_car.move_forward() acc_sound.play(-1) engine_playing = True idle_sound.stop() submitted by /u/babyjonny9898 [link] [comments] 

class AbstractCar: def __init__(self, max_vel, rotation_vel): self.img = self.IMG self.max_vel = max_vel self.vel = 0 self.rotation_vel = rotation_vel self.angle = 0 self.x, self.y = self.START_POS self.acceleration = 0.1 self.boosting = False def rotate(self, left=False, right=False): if left: self.angle += self.rotation_vel elif right: self.angle -= self.rotation_vel def draw(self, win): blit_rotate_center(win, self.img, (self.x, self.y), self.angle) def move_forward(self): self.vel = min(self.vel + self.acceleration, self.max_vel) self.move() def move_backward(self): self.vel = max(self.vel - self.acceleration, -self.max_vel/2) self.move() def move(self): radians = math.radians(self.angle) vertical = math.cos(radians) * self.vel horizontal = math.sin(radians) * self.vel self.y -= vertical self.x -= horizontal def collide(self, mask, x=0, y=0): car_mask = pygame.mask.from_surface(self.img) offset = (int(self.x - x), int(self.y - y)) poi = mask.overlap(car_mask, offset) return poi def reset(self): self.x, self.y = self.START_POS self.angle = 0 self.vel = 0 def key_press(self): keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: print("yes") self.boosting = True else: self.boosting = False def change_speed(self): self.vel = 7 self.max_vel = 7 def return_speed(self): self.vel = 4 self.max_vel = 4 def zero_speed(self): if self.vel == 0: return True def change_car_speed(car_instance, new_speed): car_instance.speed = new_speed class PlayerCar(AbstractCar): IMG = RED_CAR START_POS = (180, 200) def reduce_speed(self): self.vel = max(self.vel - self.acceleration / 2, 0) self.move() def bounce(self): self.vel = -self.vel self.move() class ComputerCar(AbstractCar): IMG = GREEN_CAR START_POS = (150, 200) def __init__(self, max_vel, rotation_vel, path=[]): super().__init__(max_vel, rotation_vel) self.path = path #point of path that I wanna to go self.current_point = 0 #need to know what point of the path currently at self.vel = max_vel #constant maximum velocity def draw_points(self, win): for point in self.path: pygame.draw.circle(win ,(255, 0, 0), point, 5) #255,0,0 is the colour of the circle and 5 is the radius def draw(self, win): super().draw(win) self.draw_points(win) #points of the car will move. Will hide it later def calculate_angle(self): target_x, target_y = self.path[self.current_point] x_diff = target_x - self.x y_diff = target_y - self.y if y_diff == 0: desired_radian_angle = math.pi / 2 else: desired_radian_angle = math.atan(x_diff / y_diff) if target_y > self.y: desired_radian_angle += math.pi difference_in_angle = self.angle - math.degrees(desired_radian_angle) if difference_in_angle >= 180: difference_in_angle -= 360 if difference_in_angle > 0: self.angle -= min(self.rotation_vel, abs(difference_in_angle)) else: self.angle += min(self.rotation_vel, abs(difference_in_angle)) def update_path_point(self): target = self.path[self.current_point] rect = pygame.Rect( self.x, self.y, self.img.get_width(), self.img.get_height()) if rect.collidepoint(*target): self.current_point += 1 def move(self): if self.current_point >= len(self.path): return self.calculate_angle() self.update_path_point() super().move() def draw(win, images, player_car, computer_car): for img, pos in images: win.blit(img, pos) player_car.draw(win) computer_car.draw(win) #draw the ComputerCar movement on the screen pygame.display.update() def move_player(player_car): keys = pygame.key.get_pressed() moved = False boosting = False if keys[pygame.K_a]: player_car.rotate(left=True) if keys[pygame.K_d]: player_car.rotate(right=True) if keys[pygame.K_w]: moved = True player_car.move_forward() acc_sound.play(-1) engine_playing = True idle_sound.stop() 

submitted by /u/babyjonny9898
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *