Reputation: 47
so I wanted to make my platform move left and right within screen range so I did this, and its just moving right none stop
for enemy in enemies:
if moving.x > 10:
moving.x += 0.5
else:
if moving.x != 10:
moving.x -= 0.5
for enemy in enemies:
if moving.y < 10:
moving.y -= 0.5
else:
if moving.y < 10:
moving.y -= 0.5
my full code
import pygame
pygame.init()
# screen
windowheight = 500
windowwidth = 500
window = pygame.display.set_mode((windowheight,windowwidth))
pygame.display.set_caption("platformer")
# colors
whitecolor = (255, 255, 255)
NiceLime = (0,255,0)
NiceYellow =(255,255,0)
NiceGreen = (145,245,105)
# player class
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.isJump = False
self.JumpCount = 10
self.speed = 5
self.fall = 0
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, self.color, self.rect)
# enemy class
class enemy:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
moving = False
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, self.color, self.rect)
# coin class
class coin:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, self.color, self.rect)
font = pygame.font.Font('freesansbold.ttf', 32)
score = 0
text = font.render('Score = ' + str(score), True, (255, 255, 255))
textRect = text.get_rect()
textRect.center = (100, 40)
# FPS
FPS = 60
clock = pygame.time.Clock()
# define player and enemy class
playerman = player(50,390,30,30, NiceLime)
enemy1 = enemy(190,380,150,10, NiceGreen)
enemy2 = enemy(340,280,150,10, NiceGreen)
enemy3 = enemy(70,250,150,10, NiceGreen)
enemy4 = enemy(-10000,450,9999999,50, NiceGreen)
moving = enemy(150,200,150,10, NiceGreen)
enemies = [enemy1,enemy2,enemy3,enemy4]
# define coins
coin1 = coin(200,360,20,20, NiceYellow)
coin2 = coin(230,360,20,20, NiceYellow)
coin3 = coin(250,360,20,20, NiceYellow)
coin3 = coin(260,360,20,20, NiceYellow)
coin4 = coin(290,360,20,20, NiceYellow)
coin5 = coin(410,240,20,20, NiceYellow)
coin6 = coin(410,210,20,20, NiceYellow)
coin7 = coin(140,180,20,20, NiceYellow)
coin8 = coin(140,210,20,20, NiceYellow)
Coins_list = [coin1,coin2,coin3,coin4,coin5,coin6,coin7,coin8]
# main loop
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
#--------------------------------moving platform
for enemy in enemies:
if moving.x > 10:
moving.x += 0.5
else:
if moving.x != 10:
moving.x -= 0.5
for enemy in enemies:
if moving.y < 10:
moving.y -= 0.5
else:
if moving.y < 10:
moving.y -= 0.5
#--------------------------------------------
window.fill((0,0,0))
moving.draw()
playerman.draw()
for enemy in enemies:
enemy.draw()
window.blit(text,textRect)
for coin in Coins_list:
coin.draw()
if playerman.y < 250:
playerman.y += 1
for enemy in enemies:
enemy.y += playerman.speed
for coin in Coins_list:
coin.y += playerman.speed
if playerman.y > 450:
playerman.y -= playerman.fall
for enemy in enemies:
enemy.y -= playerman.fall
for coin in Coins_list:
coin.y -= playerman.fall
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerman.x -= playerman.speed
if playerman.x < 100:
playerman.x += playerman.speed
for enemy in enemies:
enemy.x += playerman.speed
for coin in Coins_list:
coin.x += playerman.speed
if keys[pygame.K_RIGHT]:
playerman.x += playerman.speed
if playerman.x > 400:
playerman.x -= playerman.speed
for enemy in enemies:
enemy.x -= playerman.speed
for coin in Coins_list:
coin.x -= playerman.speed
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
collide = False
for enemy in enemies:
if playerman.rect.colliderect(enemy.rect):
collide = True
playerman.isJump = False
playerman.y = enemy.rect.top - playerman.height + 1
if playerman.rect.right > enemy.rect.left and playerman.rect.left < enemy.rect.left - playerman.width:
playerman.x = enemy.rect.left - player.width
if playerman.rect.left < enemy.rect.right and playerman.rect.right > enemy.rect.right + playerman.width:
playerman.x = enemy.rect.right
for i in range(len(Coins_list)-1,-1,-1):
if playerman.rect.colliderect(Coins_list[i].rect):
del Coins_list[i]
score += 1
text = font.render('Score = ' + str(score), True, (255, 255, 255))
textRect = text.get_rect()
textRect.center = (100, 40)
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 500 - playerman.height
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
else:
if playerman.JumpCount > 0:
playerman.y -=(playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
pygame.display.update()
pygame.quit()
Upvotes: 0
Views: 82
Reputation: 2110
In the first loop. you have if moving.x > 10
then move right 0.5. this will be the issue, since its always above 10. What you need is a velocity/ direction to move in and then reverse direction when get to certain point.
class enemy:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
moving = False
self.veloctity= [0.5,0.5] #this is direction and speed
self.rect = pygame.Rect(x,y,height,width)
You also dont add moving to the enemies list so the player cant collide(just a heads up), and you loop over the enemies but only move the one enemy, so you are moving moving
4 times every frame, so if you take it out of the loop and it moves really slow, that is because you are moving it 0.5 instead of 0.5*4 times each frame
Now you can check if the platform needs to turn around and reverse the velocity
moving.x += moving.velocity[0] #move on x axis
moving.y += moving.velocity[1] #move on y axis
if moving.x < 10 or moving.x > 400: #if outside moving area
moving.velocity[0] *= -1 #reverse direction
if moving.y < 10 or moving.y > 400: #if outside moving area
moving.velocity[1] *= -1 #reverse direction
Now, just change the 10 and 400 to where ever you want the platform to move and it will move between those points
Upvotes: 2