Python 如何根据变量添加对象?

Python 如何根据变量添加对象?,python,pygame,python-3.4,Python,Pygame,Python 3.4,我想知道每次“闪避”(分数)增加1时,如何在屏幕上添加“东西”(对象名称)?所以我希望“东西”的数量等于“闪避”的+1(当分数为0时,东西的数量为1,当分数为1时,东西的数量为2,以此类推)。 以下是完整的代码,我根据youtube上sentdex的“Python3游戏开发与Pygame”系列创建代码: import pygame import time import random pygame.init() crash_sound = pygame.mixer.Sound("Cr

我想知道每次“闪避”(分数)增加1时,如何在屏幕上添加“东西”(对象名称)?所以我希望“东西”的数量等于“闪避”的+1(当分数为0时,东西的数量为1,当分数为1时,东西的数量为2,以此类推)。 以下是完整的代码,我根据youtube上sentdex的“Python3游戏开发与Pygame”系列创建代码:

import pygame
import time
import random

pygame.init()

crash_sound = pygame.mixer.Sound("Crash.wav")
pygame.mixer.music.load("BackgroundMusic.wav")

display_width = 1200
display_height = 1000
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
bright_red = (255, 0, 0)
green = (0,200,0)
bright_green = (0, 255, 0)
blue = (0,0,255)
grey = (146, 159, 138)

car_width = 148
car_height = 200

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Racing game")
clock = pygame.time.Clock()

carImage = pygame.image.load("Car.png")
carIcon = pygame.image.load("caricon.png")
pygame.display.set_icon(carIcon)

pause = False

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, black)
    gameDisplay.blit(text, (0, 0))

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def car(x,y):
    gameDisplay.blit(carImage, (x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf", 115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    game_loop()

def crash():
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(crash_sound)
    
    message_display("You crashed!")

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
            
    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    if 900 + 100 > mouse[0] > 900 and 800 + 50 > mouse[1] > 800:
        pygame.draw.rect(gameDisplay, bright_red, (900, 800, 100, 50))
    else:
        pygame.draw.rect(gameDisplay, red, (900, 800, 100, 50))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x + (w/2)), (y + (h/2)))
    gameDisplay.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    quit()

def unpause():
    global pause

    pygame.mixer.music.unpause()
    
    pause = False

def paused():

    pygame.mixer.music.pause()

    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        largeText = pygame.font.Font("freesansbold.ttf", 115)
        TextSurf, TextRect = text_objects("Paused", largeText)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        pygame.draw.line(gameDisplay, black, (149, 799), (249, 799), 1)
        pygame.draw.line(gameDisplay, black, (250, 800), (250, 850), 1)
        pygame.draw.line(gameDisplay, black, (250, 850), (150, 850), 1)
        pygame.draw.line(gameDisplay, black, (149, 850), (149, 800), 1)
        pygame.draw.line(gameDisplay, black, (899, 799), (999, 799), 1)
        pygame.draw.line(gameDisplay, black, (1000, 800), (1000, 850), 1)
        pygame.draw.line(gameDisplay, black, (1000, 850), (900, 850), 1)
        pygame.draw.line(gameDisplay, black, (899, 850), (899, 800), 1)

        button("Continue", 150, 800, 100, 50, green, bright_green, unpause)
        button("Quit", 900, 800, 100, 50, red, bright_red, quitgame)
        
        pygame.display.update()
        clock.tick(15)

def game_intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        largeText = pygame.font.Font("freesansbold.ttf", 115)
        TextSurf, TextRect = text_objects("Racing game", largeText)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        pygame.draw.line(gameDisplay, black, (149, 799), (249, 799), 1)
        pygame.draw.line(gameDisplay, black, (250, 800), (250, 850), 1)
        pygame.draw.line(gameDisplay, black, (250, 850), (150, 850), 1)
        pygame.draw.line(gameDisplay, black, (149, 850), (149, 800), 1)
        pygame.draw.line(gameDisplay, black, (899, 799), (999, 799), 1)
        pygame.draw.line(gameDisplay, black, (1000, 800), (1000, 850), 1)
        pygame.draw.line(gameDisplay, black, (1000, 850), (900, 850), 1)
        pygame.draw.line(gameDisplay, black, (899, 850), (899, 800), 1)

        button("Start", 150, 800, 100, 50, green, bright_green, game_loop)
        button("Quit", 900, 800, 100, 50, red, bright_red, quitgame)
        
        pygame.display.update()
        clock.tick(15)

def game_loop():
    global pause

    pygame.mixer.music.play(-1)
    
    x = (display_width * 0.45)
    y = (display_height * 0.6)
    x_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 3
    thing_width = 100
    thing_height = 100
    dodged = 0

    gameExit = False

    while not gameExit:
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                if event.key == pygame.K_p:
                    pause = True
                    paused()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        gameDisplay.fill(white)

        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        car(x,y)
        things_dodged(dodged)

        if x > display_width - car_width or x <  0:
                crash()
        if thing_starty > display_height:
            thing_starty = 0
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            if thing_speed < 12:
                thing_speed += 0.5
            
        if y < thing_starty+thing_height and y + car_height > thing_starty:
            print("")
            if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
                print("")
                crash()

        pygame.display.update()
        clock.tick(120)

game_intro()
game_loop()
pygame.quit()
quit()
导入pygame
导入时间
随机输入
pygame.init()
crash\u sound=pygame.mixer.sound(“crash.wav”)
pygame.mixer.music.load(“BackgroundMusic.wav”)
显示宽度=1200
显示高度=1000
黑色=(0,0,0)
白色=(255255)
红色=(200,0,0)
鲜红色=(255,0,0)
绿色=(0200,0)
亮绿色=(0,255,0)
蓝色=(0,0255)
灰色=(146159138)
轿厢宽度=148
轿厢高度=200
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题(“赛车游戏”)
clock=pygame.time.clock()
carImage=pygame.image.load(“Car.png”)
carIcon=pygame.image.load(“carIcon.png”)
pygame.display.set_图标(carIcon)
暂停=错误
def道奇事件(计数):
font=pygame.font.SysFont(无,25)
text=font.render(“减淡:”+str(计数),真,黑色)
blit(文本,(0,0))
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
def汽车(x,y):
游戏显示.blit(carImage,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font(“freesansbold.ttf”,115)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(崩溃声音)
显示消息(“您崩溃了!”)
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(游戏显示,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(游戏显示,ic,(x,y,w,h))
如果900+100>鼠标[0]>900和800+50>鼠标[1]>800:
pygame.draw.rect(游戏显示,亮红色,(90080010050))
其他:
pygame.draw.rect(游戏显示,红色,(90080010050))
smallText=pygame.font.font(“freesansbold.ttf”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
blit(textSurf,textRect)
def quitgame():
pygame.quit()
退出
def unpause():
全局暂停
pygame.mixer.music.unpuse()
暂停=错误
def暂停():
pygame.mixer.music.pause()
暂停时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(白色)
largeText=pygame.font.font(“freesansbold.ttf”,115)
TextSurf,TextRect=text\u对象(“暂停”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.draw.line(游戏显示,黑色,(149799),(249799),1)
pygame.draw.line(游戏显示,黑色,(250800),(250850),1)
pygame.draw.line(游戏显示,黑色,(250850),(150850),1)
pygame.draw.line(游戏显示,黑色,(149850),(149800),1)
pygame.draw.line(游戏显示,黑色,(899799),(999799),1)
pygame.draw.line(游戏显示,黑色,(1000800),(1000850),1)
pygame.draw.line(游戏显示,黑色,(1000850),(900850),1)
pygame.draw.line(游戏显示,黑色,(899850),(899800),1)
按钮(“继续”、150、800、100、50、绿色、亮绿色、取消暂停)
按钮(“退出”,900,800,100,50,红色,亮红色,退出游戏)
pygame.display.update()
时钟滴答(15)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(白色)
largeText=pygame.font.font(“freesansbold.ttf”,115)
TextSurf,TextRect=text_对象(“赛车游戏”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.draw.line(游戏显示,黑色,(149799),(249799),1)
pygame.draw.line(游戏显示,黑色,(250800),(250850),1)
pygame.draw.line(游戏显示,黑色,(250850),(150850),1)
pygame.draw.line(游戏显示,黑色,(149850),(149800),1)
pygame.draw.line(游戏显示,黑色,(899799),(999799),1)
pygame.draw.line(游戏显示,黑色,(1000800),(1000850),1)
pygame.draw.line(游戏显示,黑色,(1000850),(900850),1)
pygame.draw.line(游戏显示,黑色,(899850),(899800),1)
按钮(“开始”,150,800,100,50,绿色,亮绿色,游戏循环)
按钮(“退出”,900,800,100,50,红色,亮红色,退出游戏)
pygame.display.update()
时钟滴答(15)
def game_loop():
全局暂停
pygame.mixer.music.play(-1)
x=(显示宽度*0.45)
y=(显示高度*0.6)
x_变化=0
thing\u startx=random.randrange(0,显示宽度)
事情开始=-600
速度=3
物体宽度=100
物体高度=100
减淡=0
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.KEYDOWN:
objects = [Object()]
if thing_starty > display_height:
    thing_starty = 0
    thing_startx = random.randrange(0, display_width)
    objects.append(Object())
    dodged += 1
for item in objects:
    gameDisplay.blit(item, (x,y))