Issue with sprite movement in a PACMAN-like game: may be related to collision detection or key pressing?

I’m making a pygame for a senior year school project due in less than a month and I am using a particular tutorial for PACMAN and adapting it to create my own game with a similar concept. However, as I am following this and changing the code a bit to suit my game, my character ‘mouse.png’ is not moving. I don’t know what led to this occurring, if it’s part of a particular function or wrong logic, so what how would I be able to fix this?

Here is the tutorial I am following: https://www.youtube.com/watch?v=9H27CimgPsQ

The following code is my program:

from board import board
import pygame
import math

pygame.init()

pygame.display.set_caption('The Big Cheese Heist')

WIDTH = 600
HEIGHT = 625

screen = pygame.display.set_mode([WIDTH, HEIGHT])
timer = pygame.time.Clock()
fps = 60
font = pygame.font.Font('freesansbold.ttf', 20)
level = boards
color = 'orange'
PI = math.pi

image = (pygame.transform.scale(pygame.image.load('mouse.png'), (60, 45)))

class MySprite(pygame.sprite.Sprite):
    def __init__ (self, image):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect()

my_sprite = MySprite(image)

print(my_sprite.rect.x, my_sprite.rect.y, my_sprite.rect.width, my_sprite.rect.height)

playerX = 150
playerY = 338
direction = 0
counter = 0
flicker = False
#L, R, D, U
turnsAllowed = [False, False, False, False]
directionCommand = 0
playerSpeed = 2

#drawing each tile type onto the board
def drawBoard():
    num1 = ((HEIGHT - 50) // 32)
    num2 = (WIDTH // 30)
    for i in range (len(level)):
        for j in range(len(level[i])):
            if level [i][j] == 1:
                pygame.draw.circle(screen, 'white', (j * num2 + (0.5 * num2), i * num1 + (0.5 * num1)), 4)
            if level [i][j] == 2 and not flicker:
                pygame.draw.circle(screen, 'white', (j * num2 + (0.5 * num2), i * num1 + (0.5 * num1)), 10)
            if level [i][j] == 3: 
                pygame.draw.line(screen, color, (j * num2 + (0.5 * num2), i * num1),
                                (j * num2 + (0.5 * num2), i * num1 + num1), 3)
            if level [i][j] == 4: 
                pygame.draw.line(screen, color, (j * num2, i * num1 + (0.5 * num1)),
                                (j * num2 + num2, i * num1 + (0.5 * num1)), 3)
            if level [i][j] == 5:
                pygame.draw.arc(screen, color, [(j * num2 - (num2 *0.4)) - 2, (i * num1 + (0.5 * num1)), num2, num1], 0, PI/2, 3)
            if level [i][j] == 6:
                pygame.draw.arc(screen, color, [(j * num2 + (num2 *0.5)), (i * num1 + (0.5 * num1)), num2, num1], PI/2, PI, 3)
            if level [i][j] == 7:
                pygame.draw.arc(screen, color, [(j * num2 + (num2 *0.5)), (i * num1 - (0.4 * num1)), num2, num1], PI, 3 *PI/2, 3)
            if level [i][j] == 8:
                pygame.draw.arc(screen, color, [(j * num2 - (num2 *0.4)) - 2, (i * num1 - (0.4 * num1)), num2, num1], 3 * PI/2, 2 * PI, 3)
            if level [i][j] == 9: 
                pygame.draw.line(screen, 'white', (j * num2, i * num1 + (0.5 * num1)),
                                (j * num2 + num2, i * num1 + (0.5 * num1)), 3)

def drawPlayer():
    #screen.blit(image,(0,0))
    # 0 - RIGHT, 1 - LEFT, 2 - UP, 3 - DOWN
    if direction == 0:
        screen.blit(image, (playerX, playerY))
    elif direction == 1:
        screen.blit(pygame.transform.flip(image, True, False), (playerX, playerY))
    elif direction == 2:
        screen.blit(pygame.transform.rotate(image, (90)), (playerX, playerY))
    elif direction == 3:
        screen.blit(pygame.transform.rotate(image, (270)), (playerX, playerY))

def checkPosition(centerX, centerY):
    turns = [False, False, False, False]
    #checks if players center position is clear
    num1 = (HEIGHT - 50 //32)
    num2 = (WIDTH//30)
    num3 = 15
    # check collisions based on center x and center y of player +/0 fudge number
    if centerX // 30 < 29:
            #L, R, D, U !!!
            #row, column
        if direction == 1: #right
                if level[centerY//num1][(centerX - num3)// num2] < 3: #empty square, small dot or big dot
                    turns[0] = True
        if direction == 0: #left
                if level[centerY//num1][(centerX + num3)// num2] < 3: #empty square, small dot or big dot
                    turns[1] = True
        if direction == 3: #up
                if level[(centerY+num3)//num1][centerX // num2] < 3: #empty square, small dot or big dot
                    turns[2] = True
        if direction == 2: #down
                if level[(centerY-num3)//num1][centerX// num2] < 3: #empty square, small dot or big dot
                    turns[3] = True
        
        #L, R, D, U
        # L = 0, R = 1, D = 2, U = 3
        if direction == 2 or direction == 3:
            if 12 <= centerX % num2 <= 18:
            # if centerX divided by num2 (how wide each tile is) if the remainder is 12 <= *** <= 18 
            # then that means the character is roughly in the middle of the tile
                if level[(centerY+num3)//num1] [centerX // num2] < 3:
                    turns [2] = True
                #if I'm clear below  me, I can turn down
                if level[(centerY-num3)//num1] [centerX // num2] < 3:
                    turns [3] = True
                #if I'm clear above me, I can turn up
            
            if 12 <= centerX % num1 <= 18:
                if level[centerY//num1] [(centerX-num2) // num2] < 3:
                    turns [0] = True
                #if I'm clear behind me, I can turn left
                if level[centerY//num1] [(centerX + num2) // num2] < 3:
                    turns [1] = True
                #if I'm clear infront me, I can turn right

        if direction == 0 or direction == 1:
            if 12 <= centerX % num2 <= 18:
            # if centerX divided by num2 (how wide each tile is) if the remainder is 12 <= *** <= 18 
            # then that means the character is roughly in the middle of the tile
                if level[(centerY+num1)//num1] [centerX // num2] < 3:
                    turns [2] = True
                #if I'm clear below  me, I can turn down
                if level[(centerY-num1)//num1] [centerX // num2] < 3:
                    turns [3] = True
                #if I'm clear above me, I can turn up
            
            if 12 <= centerX % num1 <= 18:
                if level[centerY//num1] [(centerX-num3) // num2] < 3:
                    turns [0] = True
                #if I'm clear behind me, I can turn left
                if level[centerY//num1] [(centerX + num2) // num2] < 3:
                    turns [1] = True
                #if I'm clear infront me, I can turn right

    else:
        turns[0] = True #left
        turns [1] = True #right

    return turns

def movePlayer(playX, playY):
    #l, r, d, u
    if direction == 0 and turnsAllowed[0]:
        playX -= playerSpeed
    elif direction == 1 and turnsAllowed[1]:
        playX += playerSpeed
    if direction == 2 and turnsAllowed[2]:
        playY -= playerSpeed
    elif direction == 3 and turnsAllowed[3]:
        playY += playerSpeed
    return playX, playY

run = True
while run:
    timer.tick(fps)
    if counter < 30:
        counter +=1
        if counter > 5:
            flicker = False
    else:
        counter = 0
        flicker = True

    screen.fill('black')
    drawBoard()
    drawPlayer()
    centerX = playerX + 11
    centerY = playerY + 12
    turnsAllowed = checkPosition(centerX, centerY)
    playerX, playerY = movePlayer(playerX, playerY)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                directionCommand = 0
            if event.key == pygame.K_RIGHT:
                directionCommand = 1
            if event.key == pygame.K_DOWN:
                directionCommand = 2
            if event.key == pygame.K_UP:
                directionCommand = 3
        
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and directionCommand == 0:
                directionCommand = direction
            if event.key == pygame.K_RIGHT and directionCommand == 1:
                directionCommand = direction
            if event.key == pygame.K_DOWN and directionCommand == 2:
                directionCommand = direction 
            if event.key == pygame.K_UP and directionCommand == 3:
                directionCommand = direction

    for i in range(4):
        if directionCommand == i and turnsAllowed[i]: #code knows its a True/False variable (its True)
            direction = i
        
    if playerX > 600:
        playerX = -17
    elif playerX < -50:
        playerX = 597

    pygame.display.flip()

pygame.quit()

I’ve ran through the tutorial and attempted to fix up some logic and even restarted programming bits to see if I’ve made a mistake but nothing’s worked! Please help, thank you!

New contributor

Y-Orozco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật