Dec-30-2021, 08:52 PM
I have an issue I cannot seem to figure out on my own. I am trying to set a wall that my player object can't go through and I use a solution I have used in the past that works fine on player controlled objects, but not non-player controlled objects. Please see code below.
The code in this file that gives me problems are lines 55-64. I found out that if I reverse them to right,left,left,right the problematic wall changes from left to right.
What I don't get is why this has anything to say? I'm telling it to just block whatever side the object appears to be hitting. Yet it won't work for non-player controlled objects.
The code in this file that gives me problems are lines 55-64. I found out that if I reverse them to right,left,left,right the problematic wall changes from left to right.
What I don't get is why this has anything to say? I'm telling it to just block whatever side the object appears to be hitting. Yet it won't work for non-player controlled objects.
import pygame #Color definitions BLACK = (0,0,0) BLUE = (10,10,128) GREEN = (5,200,0) YELLOW = (255,255,0) RED = (255,0,0) RED2 = (178,34,34) #Screen width SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 pygame.init() all_sprite_list = pygame.sprite.Group() class Player(pygame.sprite.Sprite): def __init__(self,x,y): super().__init__() self.image = pygame.Surface([50,45]) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.y = y self.rect.x = x self.speedy = 0 self.speedx = 0 self.rect.x = 199 self.rect.y = 240 self.change_x = 0 self.walls = None def changespeed(self,x,y): self.change_x += x self.change_y += y def update(self): self.rect.y += self.speedy self.rect.x += self.speedx if self.rect.x > 360: self.speedx = -2 self.speedy = 0 if self.rect.x <200: self.speedx = 2 block_hit_list = pygame.sprite.spritecollide(self,self.walls, False) for block in block_hit_list: if self.change_x > 0: self.rect.left = block.rect.right else: self.rect.right = block.rect.left if self.change_x < 0: self.rect.right = block.rect.left else: self.rect.left = block.rect.right class Wall(pygame.sprite.Sprite): def __init__(self,x,y,width,height): super().__init__() self.image = pygame.Surface([width, height]) self.image.fill(GREEN) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y #Wall init wall_list = pygame.sprite.Group() #Pygame definitions screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) pygame.display.set_caption('Ninja!') all_sprite_list = pygame.sprite.Group() clock = pygame.time.Clock() #Test wall wall = Wall(320,240,10,90) wall_list.add(wall) all_sprite_list.add(wall) #Spawns player player = Player(50,50) all_sprite_list.add(player) player.walls = wall_list clock = pygame.time.Clock() #Loop done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True all_sprite_list.update() screen.fill(BLACK) all_sprite_list.draw(screen) pygame.display.flip() clock.tick(60) pygame.display.flip() pygame.quit()