0

I am trying to make my gun turn towards my mouse but it just starts glitching out. My image begins to turn weirdly, most likely to the glitch, and becomes extremely grainy. Shortly after ,pygame runs out of memory and the IDE crashes. I am really unsure if it is because my computer is crappy or because there is an issue with my code.

This is my code:

import pygame, math
screen_width = 700
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()

class player(pygame.sprite.Sprite):
    def __init__(self, width, height, pos_x, pos_y, color):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.center = (pos_x, pos_y)
        self.height = height
        self.width = width
        self.moving_right = False
        self.moving_left = False
        
    def update(self):
        dx = 0
        dy = 0
        keys = pygame.key.get_pressed()
        if keys[pygame.K_d]:
            dx = 5
            self.moving_right = True
        else:
            self.moving_right = False
        if keys[pygame.K_a]:
            dx = -5
            self.moving_left = True
        else:
            self.moving_left = False
        if keys[pygame.K_w]:
            dy = -5
        if keys[pygame.K_s]:
            dy = 5
        self.rect.x+=dx
        self.rect.y+=dy
        if self.rect.top < wall_y:
            dy = 0
            self.rect.y  = wall_y
        if self.rect.bottom > wall_y + wall_height:
            dy = 0
            self.rect.bottom  = wall_y + wall_height
        if self.rect.right > wall_x + wall_width:
            dx = 0
            self.rect.right  = wall_x + wall_width
        if self.rect.left < wall_x:
            dx = 0
            self.rect.left  = wall_x

def Wall(width, height, pos_x, pos_y, color):
    pygame.draw.rect(screen, color, (pos_x, pos_y, width, height))
    global wall_x, wall_y, wall_height, wall_width
    wall_x, wall_y, wall_height, wall_width = pos_x, pos_y, height, width
 
class crosshair(pygame.sprite.Sprite):
    def __init__(self, width, height):
        super().__init__()
        crosshair = pygame.transform.scale(pygame.image.load('crosshair.png'), (20, 20))
        self.image = crosshair
        self.rect = self.image.get_rect()
        
    def update(self):
        self.rect.center = (pygame.mouse.get_pos())

class gun(pygame.sprite.Sprite):
    def __init__(self, width, height):
        super().__init__()
        Gun = pygame.transform.scale(pygame.image.load('gun2.png'), (width, height))
        self.image = Gun
        self.rect = self.image.get_rect()
        self.rect.center = (100, 100)
        self.width = width
        self.height = height
        
    def update(self):
        Gun = pygame.transform.scale(pygame.image.load('gun2.png'), (self.width, self.height))
        self.rect.center = (player.rect.x + 40, player.rect.y + 20)
        pos = pygame.mouse.get_pos()
        #angle = 360 - math.atan2(pos[1] - (screen_height/2), pos[0] - (screen_height/2)) * 180 / math.pi

        angle = math.atan2(pos[1] - (self.rect.y+32), pos[0] - (self.rect.x-26))
        playerrot = pygame.transform.rotate(self.image, 360-angle*57.29)
        playerpos1 = (self.rect.x-playerrot.get_rect().width/2, self.rect.x-playerrot.get_rect().height/2)
        self.image = playerrot
        self.rect.center = playerpos1

player = player(40, 40, 100, 100, (255,255,255))
player_group = pygame.sprite.Group()
player_group.add(player)

Crosshair = crosshair(20, 20)
crosshair = pygame.sprite.Group()
crosshair.add(Crosshair)

shotGun = gun(40, 25)
gun_group = pygame.sprite.Group()
gun_group.add(shotGun)

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            quit
    pygame.mouse.set_visible(False)
    screen.fill((60,179,113))
    Wall(650, 650, 25, 25, (100, 255, 100))
    player_group.draw(screen)
    player_group.update()
    gun_group.draw(screen)
    gun_group.update()
    crosshair.draw(screen)
    crosshair.update()
    clock.tick(60)
    
    pygame.display.flip()
  • Why are you laoding the `Gun.png` image in the gun.update function? You have already the same image in self.image. – The_spider May 16 '22 at 15:31
  • Your code is hard to read for everyone who follows the [Style Guide for Python Code](https://peps.python.org/pep-0008/). Class names should start with a capital letter and variables should be written in lower case (with optional underscores). You make it even worse because first `crosshair` is the name of a class and then you overwrite it with an instance of `pygame.sprite.Group`. – Matthias May 16 '22 at 15:36

0 Answers0