1

I asked previously about my code I am doing for my a level coursework and I was able to fix one problem but a solution offered for me not being able to move my sprites I could not fix. I copied out this code but it still didn't work, I have highlighted this section between ** My screen loads the home screen function but when the game loop runs it loads the base but doesn't do any more.

import pygame
import random
import time

#define variables

WIDTH = 1080
HEIGHT =720
FPS = 30
x1 = WIDTH/2.25
y1 = HEIGHT/2.5
x2 = WIDTH/20
y2 = HEIGHT/2.5
xbut = 475
ybut1 = 300
ybut2 = 400
gameTitle = 'Hungry Ghosts'
xChange1 = 0
yChange1 = 0
xChange2 = 0
yChange2 = 0
running = True

#define colours

WHITE = (255,255,255)
BLACK = (0,0,0)
MEGAN = (123,57,202)
MOLLIE = (244,11,12)
KATIE = (164,12,69)


#initialise pygame and window

pygame.init()
pygame.mixer.init()
pygame.font.init()
screen =pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Hungry Ghosts')
clock = pygame.time.Clock()


#load immages

home_image = pygame.image.load(('homescreen.jpg'))
game_image = pygame.image.load(('gamescreen.jpg'))
player1_image = pygame.image.load(('player 1.png')).convert_alpha()
player2_image = pygame.image.load(('player 2.png')).convert_alpha()
position = (0,0)
screen.blit(home_image, position)
startBut = pygame.image.load('startbutton.jpg').convert()
instructionBut = pygame.image.load('instructionbutton.jpg').convert()

#define functions

def textObjects(gameTitle, font):
    textSurface = font.render(gameTitle,True, WHITE)
    pygame.display.update()
    return textSurface, textSurface.get_rect()

def titleText(gameTitle):
    textForTitle = pygame.font.Font('VCR_OSD_MONO_1.001.ttf',115) 
    TextSurf, TextRect = textObjects(gameTitle, textForTitle)
    TextRect.center = ((WIDTH/2),(HEIGHT/6))
    screen.blit(TextSurf,TextRect)
    pygame.display.update()


def startButton(xbut,ybut1):
    screen.blit(startBut,(xbut,ybut1))
    pygame.display.update()

def instructionButton(xbut,ybut2):
    screen.blit(instructionBut,(xbut,ybut2))
    pygame.display.update()

    
def character1(x1,y1):
    screen.blit(player1_image,(x1,y1))
 

def character2(x,y):
    screen.blit(player2_image,(x,y))


def homeScreen():
    global running 
    start = False
    home = True
    while running and not start:
        clock.tick(FPS)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                if startBut.get_rect(topleft = (xbut, ybut1)).collidepoint(event.pos) :
                    print('clicked on button')
                    start = True
        titleText(gameTitle)
        startButton(xbut,ybut1)
        instructionButton(xbut,ybut2)
        
        pygame.display.flip()

*******************************************************************
def gameLoop():
    global running
    global x1, y2, x2, y2, xChange1, yChange1, xChange2, yChange2
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    xChange = -5
                elif event.key == pygame.K_RIGHT:
                    xChange = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    xChange = 0

        x1 += xChange1
        screen.blit(game_image,(0,0))
        character1(x1,y1)
        character2(x2,y2)
        pygame.display.flip()
        
*********************************************************************

#movement
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            xChange1 = -5
        elif event.key == pygame.K_RIGHT:
            xChange1 = 5
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            xChange1 = 0
x1 += xChange1

#calling functions
homeScreen()
gameLoop()
pygame.quit() 
GT02
  • 41
  • 5
  • if you put a breakpoint on the lines where you call `character1()` and `character2()`, do the X and Y values for either change how you'd expect? If you put a breakpoint on the lines that affect `xChange`, do you also see the behavior you'd expect? – Random Davis Dec 03 '20 at 16:08
  • Is the issue solved? – Rabbid76 Dec 07 '20 at 12:14

1 Answers1

0

There is typo. The name of the variable is xChange1 rather than xChange. Furthermore read Faster version of pygame.event.get() and just implement 1 event loop:

def gameLoop():
    global running
    global x1, y1, x2, y2, xChange1, yChange1, xChange2, yChange2
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    xChange1 = -5
                elif event.key == pygame.K_RIGHT:
                    xChange1 = 5
                elif event.key == pygame.K_a:
                    xChange2 = -5
                elif event.key == pygame.K_d:
                    xChange2 = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    xChange1 = 0
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    xChange2 = 0

        x1 += xChange1
        x2 += xChange2
        
        screen.blit(game_image,(0,0))
        character1(x1,y1)
        character2(x2,y2)
        pygame.display.flip()
        

#calling functions
homeScreen()
gameLoop()
pygame.quit()  

However I recommend to use pygame.key.get_pressed() rather than the keyboard events.

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:

def gameLoop():
    global running
    global x1, y1, x2, y2
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        keys = pygame.key.get_pressed()
        x1 += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5
        y1 += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 5
        x2 += (keys[pygame.K_d] - keys[pygame.K_a]) * 5
        y2 += (keys[pygame.K_s] - keys[pygame.K_w]) * 5

        screen.blit(game_image,(0,0))
        character1(x1,y1)
        character2(x2,y2)
        pygame.display.flip()
        

#calling functions
homeScreen()
gameLoop()
pygame.quit() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174