I am making a pokemon ripoff in pygame, and I have made a method to test whether the player is in the tall grass. However, I do not know where I should call the method. I have tried in the player move method, in the game loop method, and the update method. When I do it like this, it checks after you have moved out of the grass, which means the first time you move into grass it won't work, and when you first move out of grass it works (when it shouldn't).
in_grass(self) method:
def in_grass(self):
for g in self.game.grass:
if pygame.sprite.collide_rect(self, g):
print(random.randint(1, 10000))
if random.randint(1, 180) <= 25:
self.battle()
player move(self) method:
def move(self, x_change, y_change):
if x_change > 0:
self.dir = 'RIGHT'
if x_change < 0:
self.dir = 'LEFT'
if y_change > 0:
self.dir = 'DOWN'
if y_change < 0:
self.dir = 'UP'
if not self.collide(x_change, y_change):
self.x += x_change
self.y += y_change
image_list = None
if self.dir == 'UP':
image_list = self.image_up
elif self.dir == 'LEFT':
image_list = self.image_left
elif self.dir == 'RIGHT':
image_list = self.image_right
elif self.dir == 'DOWN':
image_list = self.image_down
if image_list:
if self.walkcount >= len(image_list):
self.walkcount = 0
self.image = image_list[self.walkcount]
self.walkcount += 1
self.image.set_colorkey(BLACK)
Main loop:
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
self.player.move(0, -1)
if event.key == pygame.K_a:
self.player.move(-1, 0)
if event.key == pygame.K_s:
self.player.move(0, 1)
if event.key == pygame.K_d:
self.player.move(1, 0)
if event.type == pygame.KEYUP:
self.player.image = self.player.default_images[self.player.dir]
self.player.image.set_colorkey(BLACK)
EDIT: I finally got it working! I can't give myself the bounty, so I guess it will just sit there.