import pygame
import constants
import levels
import random, sys
from pygame.locals import *
from player import Player
BADDIEMINSIZE = 45
BADDIEMAXSIZE = 55
ADDNEWBADDIERATE = 13
BADDIEMINSPEED = 1
BADDIEMAXSPEED = 3
TEXTCOLOR = (255, 255, 255)
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
terminate()
return
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def playerHasHitBaddie(playerRect, baddies):
for b in baddies:
if playerRect.colliderect(b['rect']):
return True
return False
def terminate():
pygame.quit()
sys.exit()
def main():
""" Main Program """
pygame.init()
pygame.mixer.music.load('background.mp3')
# set up sounds
smalljump = pygame.mixer.Sound('small_jump.ogg')
startmenu = pygame.mixer.Sound('startmenu.ogg')
# set up fonts
font = pygame.font.SysFont(None, 48)
#set up images
baddieImage = pygame.image.load('baddie.png')
# Set the height and width of the screen
size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("OVER 9000 WHEN HE CHECK THE SCOUTER")
# Create the player
player = Player()
# Create all the levels
level_list = []
level_list.append(levels.Level_01(player))
# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]
active_sprite_list = pygame.sprite.Group()
player.level = current_level
player.rect.x = 340
player.rect.y = constants.SCREEN_HEIGHT - player.rect.height
active_sprite_list.add(player)
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# show the "Start" screen
drawText('THE INTERNET OF THINGS', font, screen, (constants.SCREEN_WIDTH / 3) - 40, (constants.SCREEN_HEIGHT / 3) + 50)
drawText('Press a key to start.........', font, screen, (constants.SCREEN_WIDTH / 3), (constants.SCREEN_HEIGHT / 3) + 100)
startmenu.play()
pygame.display.update()
waitForPlayerToPressKey()
#GAME LOOP
score = 0
startmenu.stop()
pygame.mixer.music.play(-1, 0.0)
while not done:
baddieAddCounter = 0
baddies = []
score += 1
fact = ""
baddieAddCounter += 1
if baddieAddCounter == ADDNEWBADDIERATE:
baddieAddCounter = 0
baddieSize = random.randint(BADDIEMINSIZE, BADDIEMAXSIZE)
newBaddie = {'rect': pygame.Rect(random.randint(0, SCREEN_WIDTH-baddieSize), 0 - baddieSize, baddieSize, baddieSize),
'speed': random.randint(BADDIEMINSPEED, BADDIEMAXSPEED),
'surface':pygame.transform.scale(baddieImage, (baddieSize, baddieSize)),
}
baddies.append(newBaddie)
#GIVES PLAYER A NEW FACT EVERYTIME THEY REACH THE TARGET SCORE
if score < 400:
fact = 'NOTHING'
elif score > 400 and score <= 799:
fact = 'Physical objects having network connectivity'
elif score > 800 and score <= 1100:
fact = '1982 - First inter-connected appliance: Coke machine'
drawText('Score: %s' % (score), font, screen, 10, 0)
drawText('Fact: %s' % (fact), font, screen, 10, 40)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.go_left()
if event.key == pygame.K_RIGHT:
player.go_right()
if event.key == pygame.K_SPACE:
smalljump.play()
player.jump()
if event.key == pygame.K_UP:
smalljump.play()
player.jump()
if event.key == K_ESCAPE:
terminate()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and player.change_x < 0:
player.stop()
if event.key == pygame.K_RIGHT and player.change_x > 0:
player.stop()
# Move the baddies down.
for b in baddies:
b['rect'].move_ip(0, b['speed'])
# Draw each baddie
for b in baddies:
screen.blit(b['surface'], b['rect'])
pygame.display.update()
# Delete baddies that have fallen past the bottom.
for b in baddies[:]:
if b['rect'].top > WINDOW_HEIGHT:
baddies.remove(b)
pygame.display.update()
pygame.display.update()
active_sprite_list.update()
current_level.update()
#WHEN PLAYER GOES RIGHT, SHIFT WORLD LEFT
if player.rect.right >= 500:
diff = player.rect.right - 500
player.rect.right = 500
current_level.shift_world(-diff)
#WHEN PLAYER FOES LEFT, SHIFT WORLD RIGHT
if player.rect.left <= 120:
diff = 120 - player.rect.left
player.rect.left = 120
current_level.shift_world(diff)
# RESTRICTS PLAYER FROM GOING TO FAR TO THE RIGHT
current_position = player.rect.x + current_level.world_shift
if current_position < current_level.level_limit:
player.rect.x = 120
current_level.draw(screen)
active_sprite_list.draw(screen)
clock.tick(60)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
For some reason my enemies (baddies) are not spawning in, i got all the baddie code from another program called "dodger" so i don't know why it's not working. It works when i tried it in dodger obviously but when i put it in my code nothing happens. My code still runs but when you're playing no baddies will fall from the sky like it's supposed to. There are other modules for this code but i didn't add them.
There are several issues with your code, but your current problem is that you create a new list of baddies every iteration of your main loop:
...
while not done:
baddieAddCounter = 0
baddies = []
score += 1
fact = ""
...
You should move baddies = []
outside the while
loop.
You already seem to use sprites and groups, so I don't know why do you use dicts for your baddies instead of sprites.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.