Enemy Not Staying At Original Spot
Solution 1:
Improve the robustness of your code. Make sure that the movement of a Snake
works even if the position of the snake is far out of the path
. If proposed_move
is less than self.path[0]-self.vel
or self.path[1]+self.vel
, then the Snake
will never return to the path, because self.vel
will be inverted continuously. Make sure that the direction of movement is always set to the right if proposed_move < self.path[0]
and make sure that the direction of movement is always set to the left if proposed_move > self.path[1]
:
classSnake:
# [...]defmove(self):
if self.visible:
# turn around if the move would go out-of-bounds
proposed_move = self.x + self.vel
# Move hits a boundary, so we need to turn aroundif proposed_move < self.path[0]:
self.vel = abs(self.vel)
self.Walking_index = 0if proposed_move > self.path[1]:
self.vel = -abs(self.vel)
self.Walking_index = 0# now make the correct move
self.x += self.vel # add +/- velocity
Do the same for Bat
:
classBat:
# [...]defmove(self):
if self.visible:
# relizes when it hits bounds
proposed_move = self.x + self.vel
if proposed_move < self.path[0]:
self.vel = abs(self.vel)
self.Walking_index = 0if proposed_move > self.path[1]:
self.vel = -abs(self.vel)
self.Walking_index = 0# starting movment
self.x += self.vel
You have to simplify your code.
Store the initial position of the player (start_x
, start_y
) and a to methods to the class player that return the current position of the player in relation to the start position (get_x
, get_y
):
classPlayer:
def__init__(self,x,y,width,height,color):
self.start_x = x
self.start_y = y
self.x = x
self.y = y
#[...]defget_x(self):
return self.x - self.start_x
defget_y(self):
return self.y - self.start_y
Compute the positions of Snake
, Bat
, Platform
and Rule
in relation to the player:
classSnake:
# [...]defdraw(self):
self.move()
self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
window.blit(self.no,self.rect)
classBat:
# [...]defdraw(self):
self.move()
self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
window.blit(self.no, self.rect)
classPlatform:
# [...]defget_rect(self):
self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
return self.rect
defdraw(self):
pygame.draw.rect(window,self.color,self.get_rect())
classRule:
# [...]defdraw(self):
self.rect.topleft = (self.x-playerman.get_x(), self.y-playerman.get_y())
Get rid of player movement compensation (scroll
etc.). This is no longer necessary as the objects are drawn in relation to the player. Complete main loop:
run = Truewhile run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False# lets player move
keys = pygame.key.get_pressed()
if playerman.fall > 0and keys[pygame.K_SPACE]:
if keys[pygame.K_d]:
playerman.direction = "jump1"else:
if playerman.direction == "left":
if keys[pygame.K_SPACE]:
playerman.direction = "jump2"# direction for player animation and screen movment xif keys[pygame.K_d]:
ifnot playerman.isJump:
playerman.direction = "right"elif keys[pygame.K_a]:
ifnot playerman.isJump:
playerman.direction = "left"if playerman.direction == "right"and keys[pygame.K_SPACE]:
playerman.direction = "jump1"if playerman.direction == "left"and keys[pygame.K_SPACE]:
playerman.direction = "jump2"
px, py = playerman.x, playerman.y
# sides for player and player screen movment
platform_rect_list = [p.rect for p in platforms]
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)
playerman.y = py
if keys[pygame.K_d]:
playerman.x += playerman.speed
if keys[pygame.K_a]:
playerman.x -= playerman.speed
if player_rect.collidelist(platform_rect_list) > 0:
playerman.x = px
# About isJumpifnot playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False# this part lets you jump on platform only the top
collide = Falsefor Platform in platforms:
player_rect = playerman.get_rect()
player_rect.topleft = (playerman.x, playerman.y)
platform_rect = Platform.get_rect()
if playerman.get_rect().colliderect(platform_rect):
collide = True
playerman.isJump = Falseif platform_rect.top > playerman.x:
playerman.y = platform_rect.top - playerman.height
if player_rect.right > platform_rect.left and player_rect.left < platform_rect.left:
playerman.x = platform_rect.left - playerman.width
if player_rect.left < platform_rect.right and player_rect.right > platform_rect.right:
playerman.x = platform_rect.right
# colliding with floor if player_rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.Jumpcount = 10
playerman.y = 500 - playerman.height
# Jumpingif collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.y -= playerman.speed
playerman.fall = 0# Jump Countelse:
if playerman.JumpCount >= 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1else:
playerman.isJump = False
playerman.JumpCount = 10
redrawwindow()
if playerman.rect.colliderect(rule1.rect):
window.blit(move,(-40,-100))
pygame.display.update()
Post a Comment for "Enemy Not Staying At Original Spot"