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!
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