title: Preventing Character From Leaving Screen
body: <p>So I have a pretty simple problem but I cannot find the solution to it.</p>
  
  <p>I have tried using 'clamp_ip' to try and prevent my character from leaving the screen but I am not sure why this isn't working as I have followed all examples that I have seen and still got this problem</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-py prettyprint-override"><code>
  import math
  from random import randint
  import pygame
  from pygame.math import Vector2
  
  pygame.init()
  
  screen = pygame.display.set_mode((600, 600))
  screen_width = 600
  screen_height = 600
  black = (0, 0, 0)
  pygame.display.set_caption("gang")
  clock = pygame.time.Clock()
  
  
  class Character:
      def __init__(self, x, y, width, height):
          self.x = x
          self.y = y
          self.width = width
          self.height = height
  
  
  class Player(pygame.sprite.Sprite):
  
      def __init__(self, pos):
          super().__init__()
          self.image = pygame.Surface((50, 30), pygame.SRCALPHA)
          pygame.draw.polygon(self.image, pygame.Color('steelblue2'), [(0, 0), (50, 15), (0, 30)])
          self.original_image = self.image
          self.rect = self.image.get_rect(center=pos)
          self.pos = Vector2(pos)
          self.velocityx = Vector2(6, 0)
          self.velocityy = Vector2(0, 6)
  
      def update(self):
          self.rect.center = (int(self.pos.x), int(self.pos.y))
          self.rotate()
  
      def rotate(self):
          direction = pygame.mouse.get_pos() - self.pos
          radius, angle = direction.as_polar()
          self.image = pygame.transform.rotate(self.original_image, -angle)
          self.rect = self.image.get_rect(center=self.rect.center)
  
  
  class Enemy(Character):
      def __init__(self, x, y, width, height):
          Character.__init__(self, x, y, width, height)
  
      def draw(self, win):
          pygame.draw.rect(win, (0, 255, 0), (enemy1.x, enemy1.y, enemy1.width, enemy1.height))
  
  
  class Projectile(pygame.sprite.Sprite):
      def __init__(self, pos):
          super().__init__()
          self.image = pygame.Surface((9, 15), pygame.SRCALPHA)
          pygame.draw.circle(self.image, (0, 0, 0), (30, 50), 14)
          self.rect = self.image.get_rect(center=pos)
          self.pos = Vector2(pos)
          self.velocityx = Vector2(9, 0)
          self.velocityy = Vector2(0, 9)
  
      def update(self):
          self.pos += self.velocityx
          self.rect.center = self.pos
  
  
  def draw_window():
      screen.fill((30, 30, 30))
      enemy1.draw(screen)
      all_sprites.update()
      all_sprites.draw(screen)
      bullets.update()
      bullets.draw(screen)
      pygame.display.update()
  
  
  def xor(a, b):
      if bool(a) != bool(b):
          return True
      else:
          return False
  
  
  def game_end():
      pygame.font.init()
      text = pygame.font.Font('freesansbold.ttf', 32)
      text_surface = text.render('Game Over', False, (0, 0, 0))
      text_rect = text_surface.get_rect()
      text_rect.center = (86, 86)
      screen.blit(text_surface, (172, 172))
      pygame.display.update()
  
  
  
  player = Player((150, 150))
  enemy1 = Enemy(randint(300, 500), randint(300, 500), 60, 60)
  projectile = Projectile((150, 150))
  player_sprite = pygame.sprite.RenderPlain(player)
  bullet_sprites = pygame.sprite.RenderPlain(projectile)
  bullets = pygame.sprite.Group(projectile)
  all_sprites = pygame.sprite.Group(player, projectile)
  
  
  def main():
      screen_rect = screen.get_rect()
  
      run = True
      while run:
          clock.tick(60)
  
          keys = pygame.key.get_pressed()
  
          for e in pygame.event.get():
              if e.type == pygame.QUIT:
                  run = False
  
  
          if keys[pygame.K_SPACE]:
              if len(bullets) &lt; 5:
                  projectile.pos += projectile.velocityx
          if xor(keys[pygame.K_a], keys[pygame.K_LEFT]):
              player.pos -= player.velocityx
          elif xor(keys[pygame.K_d], keys[pygame.K_RIGHT]):
              player.pos += player.velocityx
          elif xor(keys[pygame.K_w], keys[pygame.K_UP]):
              player.pos -= player.velocityy
          elif xor(keys[pygame.K_s], keys[pygame.K_DOWN]):
              player.pos += player.velocityy
  
          player.rect.clamp_ip(screen_rect)
  
          draw_window()
  
  
  main()
  
  </code></pre>
  
  <p>The program runs with no problems but just doesn't do what I want it to do and I may not have understood how clamp works.</p>
  
  <p>I would like it so that my character does not leave the screen. What changes would I need to make for this to happen?</p>