[ad_1]
I’m a newbie in Python and Pygame and I’ve an issue with my sport in pygame. I’ve Maze class and Player class however I am unable to do correct collisions. I reached the second the place I do not know why my code is not working.
Here is my code:
class Maze(pygame.sprite.Sprite):
def __init__(self):
self.W = 25
self.H = 14
self.maze = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]
def draw(self,display):
block_surf = pygame.picture.load(os.path.be part of(img_folder, "new.png")).convert()
bx = 0
by = 0
for i in vary(0,self.W*self.H):
if self.maze[ bx + (by*self.W) ] == 1:
display.blit(block_surf,( bx * 40 , by * 40))
bx = bx + 1
if bx > self.W-1:
bx = 0
by = by + 1
def collisions(self):
bx = 0
by = 0
for i in vary(0, self.W * self.H):
if self.maze[ bx + (by * self.W) ] == 1:
rect = pygame.Rect(bx * 40, by * 40, 40, 40)
if rect.colliderect(Player.get_rect):
cross # Collision!
return False
bx = bx + 1
if bx > self.W-1:
bx = 0
by = by + 1
class Player(pygame.sprite.Sprite):
def __init__(self): #sprite for a participant
pygame.sprite.Sprite.__init__(self) #for sprite working
self.picture = pygame.picture.load(os.path.be part of(img_folder, "participant.png")).convert() #look of the sprite
self.picture.set_colorkey(white) #to delete black issues round rect img
self.rect = self.picture.get_rect() #sort of border round it
self.rect.centerx = width/2 + 40
self.rect.backside = height- 120
self.speedx = 0
self.speedy = 0
def replace(self): #transferring participant
maze = Maze()
collisions = maze.collisions
self.speedx = 0
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
if collisions == False:
self.speedx = 0
else:
self.speedx = -1
elif keystate[pygame.K_RIGHT]:
if self.rect.x > 920:
self.speedx = 0
# elif self.rect.y <= 636 and self.rect.y > 580:
# self.speedy = 0
elif self.rect.x == 40 and self.rect.y < 482 and self.rect.y > 401:
self.speedy = 0
elif self.rect.x == 40 and self.rect.y < 399 and self.rect.y > 282:
self.speedy = 0
elif self.rect.x == 40 and self.rect.y < 279 and self.rect.y > 160:
self.speedy = 0
elif self.rect.x == 40 and self.rect.y < 157 and self.rect.y > 41:
self.speedy = 0
else:
self.speedx = 1
elif keystate[pygame.K_UP]:
if self.rect.y < 41:
self.speedy = 0
elif self.rect.y == 159 and self.rect.x > 46 and self.rect.x < 160:
self.speedy = 0
elif self.rect.y == 281 and self.rect.x > 45 and self.rect.x < 154:
self.speedy = 0
else:
self.speedy = -1
elif keystate[pygame.K_DOWN]:
if self.rect.y > 480:
self.speedy = 0
elif self.rect.x > 405 and self.rect.x < 634 and self.rect.y > 440:
self.speedy = 0
elif self.rect.y == 280 and self.rect.x > 46 and self.rect.x < 155:
self.speedy = 0
else:
self.speedy = 1
self.rect.x += self.speedx
self.rect.y += self.speedy
def get_rect(self):
return pygame.Rect(self.rect.x, self.rect.y, 40, 40)
Can somebody clarify to me why it is not working and assist to enhance it?
I attempted many concepts – even writing from hand positions x and y to create collisions. But it is actually dangerous follow to take action I turned to pygame possibility sprite.colliderect(sprite2)
I need to obtain data why my code is not working correctly and suggestions for bettering it.
[ad_2]