0
import pygame
import os 
import random 
from pygame.locals import * # Constants
import math
import sys
import random

pygame.init()  

screen=pygame.display.set_mode((1280,700)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))     # fill the background white 
background = pygame.image.load('stage.png').convert()
Black=(0,0,0)

class Player(pygame.sprite.Sprite):
x = 20
y = 615
def __init__(self):
    super().__init__()   # calls the parent class allowing sprite to initalize
    self.image = pygame.Surface((50,25))   # this is used to create a blank image with the size inputted 
    self.image.fill((0,0,128))    # fills the blank image with colour

    self.rect = self.image.get_rect(topleft =(20,615))   # This place the player at the given position   

    self.dist = 10

def update(self): # code to make the character move when the arrow keys are pressed
    if self.rect.right > 100:   # These are to make the player so move constantly
         self.rect.y += 1
         self.rect.x += 2
    if self.rect.bottom == 700:
         pygame.quit() 

    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        self.rect.move_ip(-1,0)
    elif keys[K_RIGHT]:
        self.rect.move_ip(0.5,0)
    elif keys[K_UP]:
        self.rect.move_ip(0,-0.5)
    elif keys[K_DOWN]:
        self.rect.move_ip(0,1)
    self.rect.clamp_ip(screen_rect)
    #while self.rect == (20,615):
    if keys [K_SPACE]:
        self.rect = self.image.get_rect(topleft =(100,100))

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
    super().__init__()
    #y = random.randint(300,1200)
    x = random.randint(50,450)
    self.image = pygame.Surface((50,25))
    self.image.fill((128,0,0))
    self.rect = self.image.get_rect(topleft=(300, 50))

def update(self):
    if self.rect.bottom <= 400:
        self.rect.y += 1
    if self.rect.bottom >= 400:
         self.rect.y -= 300

On this part i got so that the enemy class moves downwards and when it reaches 400 it teleport 300 upwards but i wanted it so that it constantly moves upwards and then downwards again.

I thought that i either cancel the movement downwards when it reaches the position but i don't think you can do that.

clock = pygame.time.Clock()  # A clock to limit the frame rate.
player = Player()

enemy = Enemy()


enemy_list = pygame.sprite.Group(enemy)
#enemy_list.add(enemy)
sprites = pygame.sprite.Group(player, enemy)


def main():  #my main loop 
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    sprites.update()
    screen.blit(background, (0, 0))
    sprites.draw(screen)
    clock.tick(100)  # Limit the frame rate to 60 FPS.
    pygame.display.flip()   #updates the whole screen



#Collison check
    player_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)

    for enemy in player_hit_list:
        pygame.quit()

if __name__ == '__main__': 
main()
slee423
  • 1,307
  • 2
  • 20
  • 34
bob
  • 47
  • 5

1 Answers1

1

The Enemy class needs an additional attribute that keeps track of the direction it's moving: up or down.

So a simple solution could look like this:

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
    def __init__(self):
        super().__init__()
        x = random.randint(50,450)
        self.image = pygame.Surface((50,25))
        self.image.fill((128,0,0))
        self.rect = self.image.get_rect(topleft=(300, 50))
        self.direction = 'DOWN'

    def update(self):
        self.rect.y += 1 if self.direction == 'DOWN' else -1  

        if self.rect.bottom >= 400:
            self.direction = 'UP'
        if self.rect.top <= 50:
            self.direction = 'DOWN'
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Hello thank you for your help it works now but do you know how i can use time so that the enemy spawns after certain amount of time has passed – bob Dec 07 '18 at 11:29
  • @bob that's another question but you'll find answers e.g. [here](https://stackoverflow.com/questions/52553011/how-to-implement-a-timeout-cooldown-after-collision/52557622#52557622), [here](https://stackoverflow.com/questions/18948981/do-something-every-x-milliseconds-in-pygame/18954902#18954902) and [here](https://stackoverflow.com/questions/23368999/move-an-object-every-few-seconds-in-pygame/23384291#23384291) – sloth Dec 07 '18 at 11:35