'How to make a chomping effect in pygame [closed]
Here's my code:
import pygame
pygame.init()
pygame.display.set_caption("Pac-Man")
# Sets the size of the screen via (WIDTH, HEIGHT)
SCREEN_WIDTH = 478
SCREEN_HEIGHT = 608
# Speed of Characters
SPEED = 1
# Frames per second, how fast the game runs
FPS = 50
# Colors (RED,GREEN,BLUE)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
HIGH_SCORE = 0
# Sets the WIDTH and HEIGHT of the window
WINDOW = (SCREEN_WIDTH, SCREEN_HEIGHT)
# Displays the screen
SCREEN = pygame.display.set_mode(WINDOW)
CLOCK = pygame.time.Clock()
PacManStartSurface = pygame.transform.scale(pygame.image.load
("PacManStart.png"), (24, 24))
PacManStartSurface.convert()
PacManStartRect = PacManStartSurface.get_rect(topleft =
(((SCREEN_WIDTH - 25) // 2),
(SCREEN_HEIGHT + 144) // 2))
PacManSurface = pygame.transform.scale(pygame.image.load
("PacManRight.png"), (24, 24))
PacManSurface.convert()
PacManRect = PacManStartSurface.get_rect(topleft =
(((SCREEN_WIDTH - 125) // 2),
(SCREEN_HEIGHT + 144) // 2))
CurrentSurface = PacManStartSurface
CurrentRect = PacManStartRect
BackgroundSurface = pygame.image.load("Background.png").convert()
class PacMan():
def __init__(self):
self.LIVES = 3
class PowerUp(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("PowerUp.png")
.convert(), (23, 23))
self.mask = pygame.mask.from_surface(self.image)
class YellowGhost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load
("YellowGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class RedGhost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("RedGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class BlueGhost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("BlueGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class PinkGhost(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.transform.scale(pygame.image.load("PinkGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class Maze():
def __init__(self):
self.DOTS = []
self.WALLS = []
self.BLOCK_WIDTH = 25
self.BLOCK_HEIGHT = 25
self.MAZE_OFFSET_X = 0
self.MAZE_OFFSET_Y = 50
self.MARGIN = 3
# 0 - Dots
# 1 - Walls
# 2 - Power Up
# 3 - Empty Space
# 4 - Ghosts
self.MATRIX = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], \
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1], \
[1,2,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,2,1], \
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], \
[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1], \
[1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1], \
[1,1,1,1,0,1,1,1,3,1,3,1,1,1,0,1,1,1,1], \
[3,3,3,1,0,1,3,3,3,3,3,3,3,1,0,1,3,3,3], \
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1], \
[0,0,0,0,0,3,3,1,4,4,4,1,3,3,0,0,0,0,0], \
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1], \
[3,3,3,1,0,1,3,3,3,3,3,3,3,1,0,1,3,3,3], \
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1], \
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1], \
[1,2,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,2,1], \
[1,0,0,1,0,0,0,0,0,3,0,0,0,0,0,1,0,0,1], \
[1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1], \
[1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1], \
[1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1], \
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], \
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
# BackgroundImage(X, Y, WIDTH, HEIGHT)
self.MAZE_X = self.BLOCK_WIDTH * (len(self.MATRIX[0])
+ self.MAZE_OFFSET_X)
self.MAZE_Y = self.BLOCK_HEIGHT * (len(self.MATRIX)
+ self.MAZE_OFFSET_Y)
self.MAZE_WIDTH = self.BLOCK_WIDTH * len(self.MATRIX[0])
self.MAZE_HEIGHT = self.BLOCK_HEIGHT * len(self.MATRIX)
def DrawMaze(self, MazeSurface):
for ROW in range(len(self.MATRIX)):
for COLUMN in range(len(self.MATRIX[0])):
# Only saves the position of each dot
if self.MATRIX[ROW][COLUMN] == 0:
self.DOTS.append([(self.BLOCK_WIDTH * COLUMN),
(self.BLOCK_HEIGHT * ROW), 4, 4])
if self.MATRIX[ROW][COLUMN] == 1:
self.WALLS.append(pygame.draw.rect(MazeSurface, WHITE,
[((self.BLOCK_WIDTH) * COLUMN),
((self.BLOCK_HEIGHT) * ROW),
self.BLOCK_WIDTH, self.BLOCK_HEIGHT]))
class Main(Maze):
def __init__(self):
# Inherits Maze class
Maze.__init__(self)
self.DIRECTION = ""
self.SCORE = 0
def Movement(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and not key[pygame.K_UP] \
and not key[pygame.K_DOWN]:
self.DIRECTION = "LEFT"
elif key[pygame.K_RIGHT] and not key[pygame.K_UP] \
and not key[pygame.K_DOWN]:
self.DIRECTION = "RIGHT"
elif key[pygame.K_UP] and not key[pygame.K_LEFT] \
and not key[pygame.K_RIGHT]:
self.DIRECTION = "UP"
elif key[pygame.K_DOWN] and not key[pygame.K_LEFT] \
and not key[pygame.K_RIGHT]:
self.DIRECTION = "DOWN"
def ContinueMovement(self):
if self.DIRECTION == "LEFT":
CurrentRect.x -= SPEED
self.WallDetection(-1, 0, CurrentRect)
if self.DIRECTION == "RIGHT":
CurrentRect.x += SPEED
self.WallDetection(1, 0, CurrentRect)
if self.DIRECTION == "UP":
CurrentRect.y -= SPEED
self.WallDetection(0, -1, CurrentRect)
if self.DIRECTION == "DOWN":
CurrentRect.y += SPEED
self.WallDetection(0, 1, CurrentRect)
def ChangeImage(self):
global CurrentSurface
if self.DIRECTION == "LEFT":
CurrentSurface = pygame.transform.rotate(PacManSurface, 180)
if self.DIRECTION == "RIGHT":
CurrentSurface = PacManSurface
if self.DIRECTION == "UP":
CurrentSurface = pygame.transform.rotate(PacManSurface, 90)
if self.DIRECTION == "DOWN":
CurrentSurface = pygame.transform.rotate(PacManSurface, 270)
def Teleport(self):
if CurrentRect.right < 0:
CurrentRect.right = SCREEN_WIDTH + 20
if CurrentRect.left > SCREEN_WIDTH:
CurrentRect.right = 0
def WallDetection(self, x, y, CurrentRect):
CurrentRect.right += x
for WALL in self.WALLS:
COLLIDE = CurrentRect.colliderect(WALL)
if COLLIDE:
if x < 0:
CurrentRect.left = WALL.right
elif x > 0:
CurrentRect.right = WALL.left
break
CurrentRect.top += y
for WALL in self.WALLS:
COLLIDE = CurrentRect.colliderect(WALL)
if COLLIDE:
if y < 0:
CurrentRect.top = WALL.bottom
if y > 0:
CurrentRect.bottom = WALL.top
break
def EatDots(self):
for ROW in range(len(self.MATRIX)):
for COLUMN in range(len(self.MATRIX[0])):
for DOT in self.DOTS:
CHOMP = CurrentRect.colliderect(DOT)
if CHOMP:
self.DOTS.remove(DOT)
self.MATRIX[ROW][COLUMN] = 3
self.SCORE += 10
return str(self.SCORE)
def DrawDots(self):
for POSITION in self.DOTS:
X = POSITION[0] + 13
Y = POSITION[1] + 13
WIDTH = POSITION[2]
HEIGHT = POSITION[3]
pygame.draw.circle(MazeSurface, YELLOW, (X, Y),
WIDTH // 2, HEIGHT // 2)
def EatGhosts(self):
pass
def EatPowerUp(self):
pass
def ShowText(self):
Font = pygame.font.Font("emulogic.ttf", 15)
OneUpText = Font.render("1UP", True, WHITE)
OneUpTextRect = OneUpText.get_rect(center = (70, 10))
OneUpScoreText = Font.render("00", True, WHITE)
UpdateScore = Main.EatDots(self)
if int(self.SCORE) > 0:
OneUpScoreText = Font.render(UpdateScore, True, WHITE)
OneUpScoreRect = OneUpScoreText.get_rect(center =
((SCREEN_WIDTH - 290)
// 2, 26))
HighScoreText = Font.render("High Score", True, WHITE)
HighScoreTextRect = HighScoreText.get_rect(center =
(SCREEN_WIDTH // 2, 10))
HighScoreNumber = Font.render("00", True, WHITE)
HighScoreNumberRect = HighScoreNumber.get_rect(center =
((SCREEN_WIDTH + 90)
// 2, 26))
SCREEN.blit(OneUpText, OneUpTextRect)
SCREEN.blit(OneUpScoreText, OneUpScoreRect)
SCREEN.blit(HighScoreText, HighScoreTextRect)
SCREEN.blit(HighScoreNumber, HighScoreNumberRect)
def PacManBite(self):
SCREEN.blit(PacManStartSurface, PacManStartRect)
pygame.display.update()
Player = Main()
BackgroundSurface = pygame.transform.scale(BackgroundSurface,
(Player.MAZE_WIDTH,
Player.MAZE_HEIGHT))
BackgroundRect = BackgroundSurface.get_rect()
MazeSurface = pygame.Surface((Player.MAZE_WIDTH, Player.MAZE_HEIGHT))
MazeRect = MazeSurface.get_rect(topleft = (Player.MAZE_OFFSET_X,
Player.MAZE_OFFSET_Y))
Player.DrawMaze(MazeSurface)
'''
pregame = True
while pregame:
if key button pressed:
pregame = False
run = True
'''
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
Player.Movement()
Player.ChangeImage()
Player.Teleport()
Player.ContinueMovement()
MazeSurface.blit(BackgroundSurface, BackgroundRect)
Player.DrawDots()
MazeSurface.blit(CurrentSurface, CurrentRect)
Player.PacManBite()
SCREEN.blit(MazeSurface, MazeRect)
#SCREEN.fill(BLACK)
Player.ShowText()
pygame.display.update()
CLOCK.tick(FPS)
pygame.quit()
So I basically have two surfaces, the main screen and MazeSurface which I blit the maze, the dots, pacman, and background which I then blit them onto the main screen. So for some reason only one pacman image is blitted meaning I have PacManStartSurface which is PacMan with its mouth closed so just a circle and PacManSurface which is PacMan facing right or whatever direction im going in, basically I want to overlap them so that the PacMan images overlap and create the effect that it is biting or chomping the dots

Solution 1:[1]
With Txn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE), '%y-%m') as Month, Sum(netPrice/100) as TransactionRevenue from transactions
group by Month),
Leaves as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval -1 MONTH), '%y-%m') as Month, sum(amount/100) as LeaveRevenue from driverPaymentTransactions
group by Month),
Sxn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE ), '%y-%m') as Month, sum(amount/100) as SubscribedRevenue from subscribedDriversDailyRevenues
group by MONTH)
Select * from Txn t
join Leaves l on t.Month = l.month
join Sxn s on t.month = s.month
Solution 2:[2]
Do not join when you need to "pull" some different measures to the common attribute. Use union all, where you do not need to care about the most complete source of group values:
create table t1 as select 1 as id, 10 as val union all select 1, 20 union all select 2, 30 union all select 3, 49
create table t2 as select 1 as id, 10 as val union all select 3, 20 union all select 3, 30 union all select 5, 49
create table t3 as select 4 as id, 10 as val union all select 6, 20 union all select 2, 30 union all select 3, 49
with u as ( select id , val as t1_val , cast(null as decimal) as t2_val , cast(null as decimal) as t3_val from t1 union all select id , null as t1_val , val as t2_val , null as t3_val from t2 union all select id , null as t1_val , null as t2_val , val as t3_val from t3 ) select id , sum(t1_val) as t1_val , sum(t2_val) as t2_val , sum(t3_val) as t3_val from u group by idid | t1_val | t2_val | t3_val -: | -----: | -----: | -----: 1 | 30 | 10 | null 2 | 30 | null | 30 3 | 49 | 50 | 49 5 | null | 49 | null 4 | null | null | 10 6 | null | null | 20
db<>fiddle here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Geo |
| Solution 2 | astentx |
