Search code examples
pythonpygamegame-physics

I am unable to make an object move from point A to point B without user input using python 2.7 with pygame


I am attempting to make a game where the 'player' (who spawns as the moon furthest to the right) can move around using the 'WASD' keys and enemies fall from the top of the screen in order to hit the player and end the game. I am currently doing this for an assignment and i have set up a test to see whether it is possible for me to do this and so far i have been able to create a 'player' object that is controllable by the 'WASD' keys and make a screen which requires players to press 'SPACE' to play the game. I am having trouble though with being able to have the enemies fall on the 'Y' axis down the screen smoothly. The picture moves down the screen only when the user presses or releases a key, this creates a jagged movement or when the user moves the mouse over the pygame screen, creating a very smooth movement.

 #import necessary modules and pygame.
    import pygame, sys, random
    from pygame.locals import *
    #set global variables
    pygame.init()
    WINDOWWIDTH =  800
    WINDOWHEIGHT = 800
    BACKGROUNDCOLOUR = (255,255,255)
    TEXTCOLOUR = (0,0,0)
    FPS = 30
    ENEMYMINSIZE = 10
    BOMBSAWAY = -1
    ENEMYMAXSIZE = 40
    ENEMYMINSPEED = 1
    ENEMYMAXSPEED = 10
    ADDNEWENEMYRATE = 5
    PLAYERMOVERATE = 5
    FSIZE = 48
    BLUE = (0,0,255)
    global PLAY
    PLAY = False
    global fpsClock
    fpsClock = pygame.time.Clock()
    # set up pygame and GUI

    MainClock = pygame.time.Clock()
    windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    windowSurface.fill(BACKGROUNDCOLOUR)
    pygame.display.set_caption('Bubble Dash')


    enemy = pygame.image.load('player.jpg')
    char = pygame.image.load('player.jpg')

    # set up fonts
    basicFont = pygame.font.SysFont(None, 48)

    # set up the text
    text = basicFont.render('Press any key to play!', True, (255,255,0))
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx
    textRect.centery = windowSurface.get_rect().centery
    # draw the text onto the surface


    # set up x and y coordinates

    # music 
    windowSurface.blit(text, textRect)


    def playgame(PLAY):
        x,y = 0,0
        movex,movey = 0,0
        charx=300
        chary=200
        direction = 'down'
        enemyx= 10
        enemyy=10
        while PLAY:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN: 
                    if event.key == ord('m'):
                        pygame.mixer.music.stop()
                    if event.key == ord('n'):
                        pygame.mixer.music.play()
                    if event.key == ord('a'):
                        movex = -0.5
                    if event.key == ord('d'):
                        movex = 0.5
                    if event.key == ord('w'):
                        movey = -0.5
                    if event.key == ord('s'):
                        movey = 0.5


                if event.type ==KEYUP:
                    if event.key == ord('a'):
                        movex = 0
                    if event.key == ord('d'):
                        movex = 0
                    if event.key == ord('w'):
                        movey = 0
                    if event.key == ord('s'):
                        movey = 0
                if direction == 'down':
                    enemyy += 7
            windowSurface.fill(BLUE)
            windowSurface.blit(char, (charx, chary))
            windowSurface.blit(enemy, (enemyx, enemyy))
            pygame.display.update()
            charx+=movex
            chary+=movey

    def playertopresskey(PLAY):

        while True: # main game loop

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_SPACE:
                        PLAY = True
                if PLAY == True:
                    playgame(PLAY)
            pygame.display.update()


    playertopresskey(PLAY)

I would like for the 'enemy' object to be able to fall from the top without the user either needing to keypress or to key up or to have to move the mouse on the screen, rather the 'enemy' would just fall from the top as soon as the subroutine is called.

You may need to tweak it a bit because it is set up for me at the moment but i deleted a few things that would give you bother. Hopefully someone can help me. Thanks.

The below is link to a picture similar to mine which you can download and replace in the code for both the 'char' and the 'enemy' variables to view this yourself for i cannot access the courses at the present time. http://www.roleplaygateway.com/roleplay/the-five-elements/characters/miss-joy/image


Solution

  • I found your error, you would have caught it yourself if you would have divided your code into some functions. Here is the problem:

    for event in pygame.event.get():
        if event.type == QUIT:
            ...
        if event.type == KEYDOWN:
            ...
        if event.type == KEYUP:
            ...
        if direction == 'down':
             enemyy += 7
    

    your code moving the enemy is called only when an event is waiting in the queue. Move it out of the loop, and you will be good to go.