0

In my code below I want to simply delete the food from the display after the collision occurs. My first thoughts were to simply change the coordinate of the food to a place out of the screen. But that didn't solve my problem. The x and y coordinates are the same and stored in two lists (just for the simplicity. One list could do me just fine) and I use pygame.draw to draw the foods and the Packman (for the time being the Packman function replaces the simple dot Packman to its picture)

Any ideas about my problem?

import pygame

white=(255,255,255)
black=(0,0,0)
foodcolor=(120,100,130)
PackmanColor=(255,215,0)

PageHeight = 500
PageWidth = 500

FPS=20

x_packman= 150
y_packman= 200
x_food = 20

pygame.init()
surface = pygame.display.set_mode((PageHeight,PageWidth))
pygame.display.set_caption("AI Game")
clock = pygame.time.Clock()

PackmanPicture = pygame.image.load('packman.png').convert()
RedGhost = pygame.image.load('redghost.png').convert()
YellowGhost = pygame.image.load('yellowghost.png').convert()


face="right"

def packmann(x , y):
    pygame.draw.rect(surface, PackmanColor, [x, y, 10, 10])

def Packman(x , y, image):

    if(face =="right"):
        temp_face = pygame.transform.rotate(image,0)
    if(face == "left"):
        temp_face = pygame.transform.rotate(image,180)
    if (face == "up"):
        temp_face = pygame.transform.rotate(image, 90)
    if (face == "down"):
        temp_face = pygame.transform.rotate(image, 270)

    surface.blit(temp_face ,(x,y))

def ghost(x,y,image):
    surface.blit(image,(x,y))



#y_food = 10

red_ghost_x = 400
red_ghost_y = 400

yellow_ghost_x = 400
yellow_ghost_y = 50

xfood = []

for i in range(50):
    x_food +=23
    xfood.append(x_food)
#print(xfood)
yfood = xfood

y_move = 0
x_move = 0


Game_Over=False

def GameOver():
    pygame.quit()
    quit()


while not Game_Over:

    global face
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            Game_Over=True
        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_UP):
                y_move = -3
                x_move = 0
                face="up"


        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_DOWN):
                y_move = 3
                x_move = 0
                face = "down"

        if(event.type == pygame.KEYDOWN):
            if(event.key == pygame.K_RIGHT):
                x_move = 3
                y_move = 0
                face = "right"

        if(event.type == pygame.KEYDOWN):
            if(event.key == pygame.K_LEFT):
                x_move = -3
                y_move = 0
                face = "left"

    y_packman+=y_move
    x_packman+=x_move


    surface.fill(black)

    #packman(x_packman,y_packman,PackmanPicture)
    packmann(x_packman,y_packman)
    ghost(red_ghost_x,red_ghost_y,RedGhost)
    ghost(yellow_ghost_x,yellow_ghost_y,YellowGhost)

    if (y_packman>PageHeight-10 or y_packman<5 or x_packman>PageWidth-10 or x_packman<5):
        GameOver()

    for i in range(len(xfood)-31):
        for j in range(len(xfood)-31):
            pygame.draw.rect(surface,foodcolor,[xfood[i],yfood[j],4,4])

    pygame.display.update()

    for i in range(len(xfood)-31):
        for j in range(len(xfood)-31):
             if (x_packman == xfood[i] and y_packman == yfood[j]):
                    xfood[i]=999
                    yfood[j]=999
    clock.tick(FPS)


pygame.quit()
quit() 
Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
bobitm
  • 29
  • 2
  • Learn OOP and sprites. – Mercury Platinum May 21 '18 at 18:31
  • You're missing a few fundamental notions, which you should practice before attempting to write this kind of code. I suggest this course on Coursera: https://www.coursera.org/learn/interactive-python-1 , it focuses on small games and it will help you out – ChatterOne May 23 '18 at 09:01

0 Answers0