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()
出于某种原因,我的敌人(坏人)没有产卵,我从另一个名为“道奇”的程序中得到了所有的坏代码,所以我不知道为什么它不起作用。当我在道奇中尝试它时它很有效,但当我把它放在我的代码中时没有任何反应。我的代码仍在运行,但是当你正在玩的时候,没有坏人会像天空一样从天而降。此代码还有其他模块,但我没有添加它们。
答案 0 :(得分:0)
您的代码存在一些问题,但您当前的问题是您在主循环的每次迭代中都会创建一个新的baddies列表:
...
while not done:
baddieAddCounter = 0
baddies = []
score += 1
fact = ""
...
您应该将baddies = []
移到while
循环之外。
你似乎已经使用了精灵和团体,所以我不知道你为什么使用dicts代替你的坏人而不是精灵。