1

For my coursework for computer science I have to make a program but I've ran into issues that I can't find answers to. Firstly in the section of code highlighted with *** I am trying to make the sprite character1 move which doesn't do anything when I press the arrow keys and don't know why Secondly is the picture I am using as a button doesn't do anything when i click on it - highlighted with ###

I know my code isn't the most efficient but I'm trying to do what makes sense to me

import pygame
import random
import time

WIDTH = 1080
HEIGHT =720
FPS = 30
x1 = WIDTH/2.25
y1 = HEIGHT/2.5
x2 = WIDTH/20
y2 = HEIGHT/2.5
xbut = 800
ybut = 275
gameTitle = 'Hungry Ghosts'
xChange1 = 0
yChange1 = 0
xChange2 = 0
yChange2 = 0

#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

background_image = pygame.image.load(('purplesky.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(background_image, position)
startBut = pygame.image.load('button.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 titleButton(xbut,ybut):
    screen.blit(startBut,(xbut,ybut))
    pygame.display.update()
########################################

***************************************    
def character1(x1,y1):
    screen.blit(player1_image,(x1,y1))
    pygame.display.update()
***************************************

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


def homeScreen():
    running = True
    gameplay = False
    instructions = False
    home = True
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
################################################################
            if event.type == pygame.MOUSEBUTTONDOWN:
                xbut,ybut = event.pos
                if startBut.get_rect().collidepoint(xbut,ybut):
                    print('clicked on button')
################################################################

def gameLoop():
    running = True
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        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
titleText(gameTitle)
character1(x1,y1)
character2(x2,y2)
titleButton(xbut,ybut)
homeScreen()
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
GT02
  • 41
  • 5

1 Answers1

0

You have to do the movement and the drawing of the scene in the game loop.

an application loop has to:

running = True

def homeScreen():
    global running
    start = False
    home = True
    while running and not start:
        clock.tick(FPS)
        
       # handle events
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                if startBut.get_rect(topleft = (xbut, ybut)).collidepoint(event.pos):
                    print('clicked on button')
                    start = True

        # draw background
        screen.blit(background_image, (0, 0))

        # draw scene
        titleText(gameTitle)
        titleButton(xbut,ybut)
        
        # update display
        pygame.display.flip()

homeScreen()
def gameLoop():
    global running
    global x1, y2, x2, y2, xChange1, yChange1, xChange2, yChange2
    while running:
        clock.tick(FPS)
        
        # handle events
        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
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    xChange1 = 0
        
        # update position of objects 
        x1 += xChange1

        # draw background
        screen.blit(background_image, (0, 0))
        
        # draw scene
        character1(x1,y1)
        character2(x2,y2)

        # update display
        pygame.display.flip()

gameLoop()
pygame.quit() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174