python - Why does the collision detection not function correctly? -
import random, pygame, sys, json, time, os import colours c import xml.etree.elementtree et pygame.init() open('config.json') config_file: config = json.load(config_file) gamedisplay = pygame.display.set_mode((config['screen_resolution'])) pygame.display.set_caption('bee rpg tycoon') pygame.display.update() #collisions gameexit = false x = 300 y = 300 x_change = 0 y_change = 0 clock = pygame.time.clock() class spritesheet: #loads atlas image def __init__(self, imgfile, datafile): self.spritesheet = pygame.image.load(imgfile).convert() if datafile: tree = et.parse(datafile) self.map = {} node in tree.iter(): if node.attrib.get('name'): name = node.attrib.get('name') self.map[name]={} self.map[name]['x'] = int(node.attrib.get('x')) self.map[name]['y'] = int(node.attrib.get('y')) self.map[name]['width'] = int(node.attrib.get('width')) self.map[name]['height'] = int(node.attrib.get('height')) def getimagerect(self, x, y, w, h): return self.spritesheet.subsurface(pygame.rect(x,y,w,h)) def getimagename(self, name): rect = pygame.rect(self.map[name]['x'], self.map[name]['y'], self.map[name]['width'], self.map[name]['height']) return self.spritesheet.subsurface(rect) #spritesg sheet = spritesheet("walkright.png", "charsprite.xml") class player(): def __init__(self, x, y, sprite, width, height): self.x=x self.y=y self.sprite = sprite self.sprite.set_colorkey(c.green) self.width = width self.height = height self.rect = self.sprite.get_rect() player = player(300, 300, sheet.getimagename("walkrightidle.png"), 20, 22) class enemy(): def __init__(self,x,y,bx,by,sprite,movement,trackrad, lock, movrange,mv,width,height): #basic properties self.x=x self.y=y self.bx=bx self.by=by self.sprite = sprite self.width = width self.height = height self.rect = self.sprite.get_rect() #artificial intelligence self.movement = movement #how far moves per frame. self.trackrad = trackrad #the radius in enemy can move self.lock = lock #locks enemy pattern self.movrange = movrange #when lock = 1 determines how far can go it's base position, when lock = 2 determines how far goes in square self.mv = mv #the random generator decides direction moves. #lock mechanics #0 - enemy goes anywhere randomly (looks janky) #1 - enemy goes anywhere randomly, if leaves it's movrange goes (also looks janky) #2 - enemy patrols in square #3 - enemy chases player #4 - enemy patrols , chases player basicenemy = enemy(310,310,310,310, sheet.getimagename("walkrightidlee.png"),5,10,1,50,random.randint(0,3), 20,22) basicenemy1 = enemy(250,250,250,250, sheet.getimagename("walkrightidlee.png"),5,10,0,0,random.randint(0,3), 20 ,22) basicenemy2 = enemy(100,100,100,100, sheet.getimagename("walkrightidlee.png"),5,10,2,2,random.randint(0,3),20,22) class hive(): def __init__(self, x, y, sprite): self.x = x self.y = y self.sprite = sprite mv = random.randint(0,3) def enemyai(enemy, player): enemy.mv = random.randint(0,50) #collisions if enemy.rect.colliderect(player.rect): print("stopped") elif enemy.lock == 0: if enemy.mv == 0: enemy.x = enemy.x + enemy.movement elif enemy.mv == 1: enemy.y = enemy.y + enemy.movement elif enemy.mv == 2: enemy.x = enemy.x - enemy.movement elif enemy.mv == 3: enemy.y = enemy.y - enemy.movement else: print("nothing") elif enemy.lock == 1: if enemy.mv == 0: if enemy.x > enemy.bx + enemy.movrange: enemy.x = enemy.x - enemy.movement else: enemy.x = enemy.x + enemy.movement elif enemy.mv == 1: if enemy.y > enemy.by + enemy.movrange: enemy.y = enemy.y - enemy.movement else: enemy.y = enemy.y + enemy.movement elif enemy.mv == 2: if enemy.x < enemy.bx - enemy.movement: enemy.x = enemy.x - enemy.movement else: enemy.x = enemy.x - enemy.movement elif enemy.mv == 3: if enemy.y < enemy.by - enemy.movrange: enemy.y = enemy.y + enemy.movement else: enemy.y = enemy.y - enemy.movement elif enemy.lock == 2: enemy.x = enemy.x + enemy.movement enemy.y = enemy.y + enemy.movement enemy.x = enemy.x - enemy.movement enemy.y = enemy.y - enemy.movement hive1 = hive(250, 300, "hive.png") spriteanim = 0 player.sprite = sheet.getimagename("walkrightidle.png") while not gameexit: # game loop enemyai(basicenemy, player) enemyai(basicenemy1,player) enemyai(basicenemy2,player) event in pygame.event.get(): #event handling if event.type == pygame.quit: #game closes when cross x ye gameexit = true # ends loop #movement #print(event) if event.type == pygame.keydown: if event.key == pygame.k_left: if spriteanim == 0: player.sprite = sheet.getimagename("walkleft1.png") else: player.sprite = sheet.getimagename("walkleft2.png") spriteanim = 0 x_change += -5 if event.key == pygame.k_right: player.sprite = sheet.getimagename("walkright1.png") x_change += 5 player.sprite = sheet.getimagename("walkright2.png") if event.key == pygame.k_up: player.sprite = sheet.getimagename("walkup1.png") y_change += -5 player.sprite = sheet.getimagename("walkup2.png") if event.key == pygame.k_down: player.sprite = sheet.getimagename("walkdown1.png") y_change += 5 player.sprite = sheet.getimagename("walkdown2.png") if event.type == pygame.keyup: if event.key == pygame.k_left: x_change = 0 player.sprite = sheet.getimagename("walkleftidle.png") if event.key == pygame.k_right: x_change = 0 player.sprite = sheet.getimagename("walkrightidle.png") if event.key == pygame.k_up: y_change = 0 player.sprite = sheet.getimagename("walkupidle.png") if event.key == pygame.k_down: player.sprite = sheet.getimagename("walkdownidle.png") y_change = 0 hivesprite = pygame.image.load(hive1.sprite) player.x += x_change player.y += y_change zone = pygame.image.load("untitled.png") gamedisplay.fill(c.white) gamedisplay.blit(zone, (310, 310)) gamedisplay.blit(player.sprite, (player.x, player.y)) gamedisplay.blit(basicenemy.sprite,(basicenemy.x, basicenemy.y)) gamedisplay.blit(basicenemy1.sprite,(basicenemy1.x, basicenemy1.y)) gamedisplay.blit(basicenemy2.sprite,(basicenemy2.x, basicenemy2.y)) pygame.display.update() clock.tick(int(config['framespersecond'])) #code goes here pygame.quit() quit()
i added code:
if enemy.rect.colliderect(player.rect):
to program , causes of enemy sprites freeze when game loads rather when touch player sprite, have no clue why doing , way stop , make work (only stop when being touched sprite) appreciated, can provide xml , json files if nessicary doubt be.
thanks in advanced.
if goes wrong collision detection, print rects of involved objects. if that, you'll see rects @ same position (0, 0), therefore they're colliding after game starts.
you have set positions of rects in classes:
self.rect.topleft = (self.x, self.y) # or self.rect.x = self.x self.rect.y = self.y # or self.rect = self.sprite.get_rect(topleft=(self.x, self.y))
you can use self.rect.center
or other attributes of rect.
also, don't forget move rects of objects in main loop, otherwise they'll stay @ same starting position. in case can set topleft
enemy.x
, .y
(or remove x , y attributes , update rect.x
, rect.y
directly):
enemy.rect.topleft = (enemy.x, enemy.y)
Comments
Post a Comment