Bonnie49er1
Bonnie49er1

Reputation: 31

Python programming trouble

I was programming a game and I did not know how to program in python 3.7 so I had to get a book called Code This Game, book from OddDot. And When I got to chapter 6 I was learning how to create a group for the enemy sprites. and every time I ran the program all it drew was the background image and the grid(because the book is not only teaching me how to code in Python, while I am learning I am making a game from the book while I learn from the book.) it did not draw the group of Vampire Pizzas in the right column if fact it doesn't even draw them at all. I have tried redoing the chapter over and over again and I can't do it

here is my code

#Import Libraries
import pygame
from pygame import *
from random import randint

#Initialize pygame
pygame.init()

#Define constant variables
WINDOW_WIDTH = 1100
WINDOW_HEIGHT = 600
WINDOW_RES = (WINDOW_WIDTH, WINDOW_HEIGHT)

#Define Tile Parameters
WIDTH = 100
HEIGHT = 100

#Define Tile colors
WHITE = (255, 255, 255)

#Set up rates
SPAWN_RATE = 360
#This is the code where the game window will show up
GAME_WINDOW = display.set_mode(WINDOW_RES)
display.set_caption('Attack of the Vampire Pizzaas!')

#---------------------------------------------------------
#Set up the enemy image
pizza_img = image.load('vampire.png')
pizza_surf = Surface.convert_alpha(pizza_img)
VAMPIRE_PIZZA = transform.scale(pizza_surf, (HEIGHT, WIDTH))

#Create a subclass of Sprite called VampireSprite
class VampireSprite(sprite.Sprite):

    #Set up enemy instances
    def __init__(self):
        super().__init__()
        self.speed = 2
        self.lane = randint(0, 4)
        all_vampires.add(self)
        self.image = VAMPIRE_PIZZA.copy()
        y = 50 + self.lane * 100
        self.rect = self.image.get_rect(center = (1100, y))

        def update(self, game_window):
            game_window.blit(self.image, (self.rect.x, self.rect.y))
#Set up the background image
background_img = image.load('restaurant.jpg')
background_surf = Surface.convert_alpha(background_img)
BACKGROUND = transform.scale(background_surf, WINDOW_RES)


#------------------------------------------------------
all_vampires = sprite.Group()
#Initialile and draw background grid
tile_color = WHITE

for row in range(6):
    for column in range(11):
        draw.rect(BACKGROUND, tile_color, (WIDTH * column,
                                        HEIGHT * row, WIDTH, HEIGHT),1)
GAME_WINDOW.blit(BACKGROUND, (0, 0))

#---------------------------------------------------------
#Start The Main Game Loop

#Game Loop
game_running = True
while game_running:

#Check for Events
    for event in pygame.event.get():
        if event.type == QUIT:
            game_running = False

#Spawn vampire pizza sprites
    if randint(1, SPAWN_RATE) == 1:
        VampireSprite()

#Update displays
        for vampire in all_vampires:
            vampire.update(GAME_WINDOW)
        display.update()

#End of the Main game loop
#---------------------------------------------------------
#Clean up game
pygame.quit()


I don't know if I am doing something wrong or what

Upvotes: 0

Views: 141

Answers (2)

PleatedZombus
PleatedZombus

Reputation: 11

I had the same issue while working my way through this fun coding book. Comparing the code from the book's website and my own code I found the issue was incorrect spacing while defining the update() function within the VampireSprite class.

# Create a subclass of Sprite called VampireSprite
class VampireSprite(sprite.Sprite):

    # Set up enemy instances
    def __init__(self):
        super().__init__()
        self.Speed = 2
        self. Lane = randint(0, 4)
        all_vampires.add(self)
        self.image = VAMPIRE_PIZZA.copy()
        y = 50 + self.lane * 100
        self.rect = self.image.get_rect(center = (1100, y))

        def update(self, game_window):
            game_window.blit(self.image, (self.rect.x, self.rect.y))

The correct code is:

# Create an enemy class
class VampireSprite(sprite.Sprite):

    # This function creates an instance of the enemy
    def __init__(self):
        super().__init__()
        self.speed = 2
        self.lane = randint(0, 4)
        all_vampires.add(self)
        self.image = VAMPIRE_PIZZA.copy()
        y = 50 + self.lane * 100
        self.rect = self.image.get_rect(center=(1100, y))

    # This function moves the enemies from right to left and destroys them after they've left the screen
    def update(self, game_window):
        game_window.blit(self.image, (self.rect.x, self.rect.y))

Upvotes: 1

Syed M. Sannan
Syed M. Sannan

Reputation: 1363

If my eyes are working fine(I guess) the code is valid

Make sure that the pygame version you are using is the one compatible with Python 3.7 and not older versions such as Python 2.

Upvotes: 1

Related Questions