python - Pygame colliderect constant collisions? -
thanks blackjack, here's code suggestion. player image stays in top-left corne (0,0). image cycles through in runner array, pressing left or right not make player sprite move @ edge of background image, or move background image , blocks.
import pygame, math math import * backimage = pygame.image.load("c:/users/jed/desktop/drawings/dsc_0001_cropped.jpg") camerax_offset = 0 cameray_offset = 0 screen = pygame.display.set_mode([700,700]) pygame.init() pygame.key.set_repeat(20, 20) #runner instances# runner = [] #lots of images disk add array currentrunner = 17 #end of runners!# freespeed = 5 #speed character moves @ edges of background jumping = false jumpingleft = true jumpvariable = 0 initialy = none class player(pygame.sprite.sprite): def __init__(self): self.image = runner[int(floor(currentrunner))] self.rect = self.image.get_rect() self.rect.topleft = (350, 500) def see_on_block(self): new_rect = self.rect.move(0, 2) if not any(new_rect.colliderect(b.rect) b in blocks): self.rect= new_rect def movement (self): global camerax_offset, currentrunner, jumping, jumpingleft, jumpvariable, initialy event in pygame.event.get(): if event.type == pygame.keydown: if event.key == pygame.k_left: #moving character left if camerax_offset <= 0 , self.rect.left > 20: self.rect.left -= freespeed elif 350 < self.rect.left <= 800: self.rect.left -= freespeed elif camerax_offset > 0: camerax_offset -=1 if currentrunner >= 7: currentrunner = 0 else: currentrunner += 0.3 if event.key == pygame.k_right: #moving character right if camerax_offset >= 250 , self.rect.left < 635: self.rect.left += freespeed elif -10 <= self.rect.left < 350: self.rect.left += freespeed elif camerax_offset < 250: camerax_offset += 1 if currentrunner < 8 or currentrunner >= 15: currentrunner = 8 else: currentrunner += 0.3 if event.key == pygame.k_space: jumping = true initialy = self.rect.top elif event.type == pygame.keyup: if 0 <= currentrunner <= 7: currentrunner = 16 elif 8 <= currentrunner <= 15: currentrunner = 17 if jumping == true: if 0 <= currentrunner <= 7 or currentrunner == 16 or 18 <= currentrunner <= 21: #sees whether character facing left or right when jump initiated jumpingleft = true else: jumpingleft = false jumpvariable += (radians(5)) if radians(90) <= jumpvariable < radians(270): self.rect.top = initialy - 100*sin(jumpvariable - radians(90)) #moves character in y according sin if jumpingleft == true: if camerax_offset <= 0 , self.rect.left > 20: #moves character in x if facing left self.rect.left -= jumpvariable elif 350 < self.rect.left <= 800: self.rect.left -= jumpvariable elif camerax_offset > 0: camerax_offset -= 2 elif jumpingleft == false: if camerax_offset >= 250 , self.rect.left < 635: #moves character in x if facing right self.rect.left += jumpvariable elif -10 <= self.rect.left < 350: self.rect.left += jumpvariable elif camerax_offset < 250: camerax_offset += 2 if jumpvariable >= radians(270): #changes character's sprite when jumping according variable jumping = false jumpvariable = 0 if 0 < jumpvariable < radians(45): if jumpingleft == true: currentrunner = 18 else: currentrunner = 22 elif radians(45) <= jumpvariable < radians(70): if jumpingleft == true: currentrunner = 19 else: currentrunner = 23 elif radians(70) <= jumpvariable < radians (105): if jumpingleft == true: currentrunner = 20 else: currentrunner = 24 elif radians(105) <= jumpvariable < radians (225): if jumpingleft == true: currentrunner = 21 else: currentrunner = 25 elif radians(225) <= jumpvariable <= radians(260): if jumpingleft == true: currentrunner = 18 else: currentrunner = 22 elif radians(260) < jumpvariable < radians(270): if jumpingleft == true: currentrunner = 16 else: currentrunner = 17 if jumping == false: jumpvariable = 0 self.image = runner[int(floor(currentrunner))] self.rect = self.image.get_rect() screen.blit(self.image, (self.rect.left, self.rect.top)) class block(pygame.sprite.sprite): def __init__(self, blockx, blocky, block_width, block_height): self.blockx = blockx self.blocky = blocky self.block_width = block_width self.block_height = block_height def draw_block(self): self.rect = pygame.draw.rect(screen,[160, 160, 160], (self.blockx - 2*camerax_offset, self.blocky - cameray_offset, self.block_width, self.block_height)) ##create object instances## blocks = [] floorblock = block (0, 680, 1200, 20) blocks.append(floorblock) leftwall = block (0 , 0, 20, 700) blocks.append(leftwall) rightwall = block (1180 , 0, 20, 700) blocks.append(rightwall) block1 = block (450, 600, 100, 20) blocks.append(block1) player = player() ##main loop## running = true while running: pygame.time.clock().tick(100) event in pygame.event.get(): if event.type == pygame.quit: running = false screen.blit(backimage, (0-camerax_offset, 0-cameray_offset)) b in blocks: b.draw_block() player.movement() player.see_on_block() pygame.display.flip() pygame.quit()
the part of code set rectangle player matters. set rectangle of runner same 1 of image, turns out (0, 0, width, height).
because runner moving around, have take position account , in see_on_block modify position of rectangle (the first 2 values of rect).
def see_on_block(self): self.y += 2 temprect = (self.rect[0] + self.x, self.rect[1] + self.y, self.rect[2], self.rect[3]); b in blocks: if b.rect.colliderect(temprect): self.y -= 2 break
i'd recommend use first 2 values in self.rect position instead of self.x , self.y , modify code needed.
Comments
Post a Comment