Python 让图像多次出现在游戏中

Python 让图像多次出现在游戏中,python,pygame,Python,Pygame,我有一个单电池图像出现在这里,它从右向左移动,然后消失。我希望电池不断地来来去去去,直到玩家击中其中一个。 我的问题是,如何让电池持续供电,直到玩家击中它?我希望它们相隔100个单位 from pygame import * import os import random os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 0) init() #set screen size size = width, height = 800, 600 sc

我有一个单电池图像出现在这里,它从右向左移动,然后消失。我希望电池不断地来来去去去,直到玩家击中其中一个。 我的问题是,如何让电池持续供电,直到玩家击中它?我希望它们相隔100个单位

from pygame import * 
import os
import random
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 0)
init()
#set screen size
size = width, height = 800, 600
screen = display.set_mode(size)
#set fonts
fontGame=font.SysFont("Times New Roman", 30)
fontBack=font.SysFont("Ariel", 30)
fontTitle=font.SysFont("Ariel", 100)
fontResearch=font.SysFont ("Times New Roman", 18)
#set button and page to 0
button = 0
page=0

#setting colours
BLACK = (0, 0, 0)
RED = (255,0,0)
GREEN = (0, 255, 0)
BLUE = (106,186,232)
#loading image
backgroundPic=image.load("Background.jpg")
backgroundGame=image.load("gameBackground.jpg")
backgroundGame=transform.scale(backgroundGame,(800,600))
battery=image.load("Battery.png")
battery=transform.scale(battery,(100,100))

backgroundx=0
playerPic=image.load("player.png")
playerPic=transform.scale(playerPic,(70,70))

batteryx=[]


#defining what is going to be shown on the screen
def drawScene(screen, button,page,locationx,locationy): 
    global batteryx
    mx, my = mouse.get_pos() #will get where the mouse is
    #if the user does nothing
    if page==0:
        draw.rect(screen, BLACK, (0,0, width, height))

        screen.fill(BLACK)
        rel_backgroundx= backgroundx % backgroundGame.get_rect().width
        screen.blit(backgroundGame, (rel_backgroundx - backgroundGame.get_rect().width,0))
        if rel_backgroundx < width:
            screen.blit (backgroundGame, (rel_backgroundx,0))
        screen.blit(playerPic,(locationx,locationy))

        screen.blit(battery,(batteryx,420))                
        batteryx-=1               

    display.flip()
    return page

#def collision (battery, playerPic):
    #if battery.colliderect(playerPic):
        #return True
    #return False

running = True
myClock = time.Clock()

KEY_LEFT= False
KEY_RIGHT= False
KEY_UP= False
KEY_DOWN= False
locationx=0
jumping=False
accel=20
onGround= height-150
locationy=onGround

batteryx=random.randrange(50,width,10)  

# Game Loop
while running:
    button=0
    print (KEY_LEFT, KEY_RIGHT)
    for evnt in event.get():             # checks all events that happen
        if evnt.type == QUIT:
            running=False
        if evnt.type == MOUSEBUTTONDOWN:
            mx,my=evnt.pos
            button = evnt.button
        if evnt.type== KEYDOWN:
            if evnt.key==K_LEFT:
                KEY_LEFT= True
                KEY_RIGHT= False
            if evnt.key==K_RIGHT:
                KEY_RIGHT= True
                KEY_LEFT= False
            if evnt.key==K_UP and jumping==False:
                jumping=True 
                accel=20
            if evnt.key== K_DOWN:
                KEY_DOWN= True
                KEY_UP= False 
        if evnt.type==KEYUP:
            if evnt.key==K_LEFT:
                KEY_LEFT= False
            if evnt.key==K_RIGHT:
                KEY_RIGHT= False
            if evnt.key==K_DOWN:
                KEY_DOWN=False

    if KEY_LEFT== True:
        locationx-=10
        backgroundx+=10
    if KEY_RIGHT== True:
        locationx+=10
        backgroundx-=10
    if jumping==True:
        locationy-=accel
        accel-=1
        if locationy>=onGround:
            jumping=False
            locationy=onGround

    #player cannot move off screen
    if locationx<0:
        locationx=0
    if locationx>400:
        locationx=400
    #if collision(battery, playerPic)==True:
        #screen.fill(BLACK)

    page=drawScene(screen,button,page,locationx,locationy)
    myClock.tick(60)  # waits long enough to have 60 fps
    if page==6:  #if last button is clicked program closes
        running=False

quit()
从pygame导入*
导入操作系统
随机输入
操作系统环境['SDL\U视频窗口位置]=%d,%d%(0,0)
init()
#设置屏幕大小
尺寸=宽度,高度=800600
屏幕=显示。设置模式(大小)
#设置字体
fontGame=font.SysFont(“新罗马时代”,30)
fontBack=font.SysFont(“Ariel”,30)
fontTitle=font.SysFont(“Ariel”,100)
fontResearch=font.SysFont(“新罗马时代”,18)
#将按钮和页面设置为0
按钮=0
第页=0
#定色
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(106186232)
#加载图像
backgroundPic=image.load(“Background.jpg”)
backgroundGame=image.load(“gameBackground.jpg”)
backgroundGame=变换比例(backgroundGame,(800600))
电池=image.load(“battery.png”)
电池=变换刻度(电池,(100100))
背景x=0
playerPic=image.load(“player.png”)
playerPic=变换比例(playerPic,(70,70))
电池x=[]
#定义将在屏幕上显示的内容
def drawScene(屏幕、按钮、页面、位置X、位置Y):
全球电池
mx,my=mouse.get_pos()#将获得鼠标所在的位置
#如果用户什么也不做
如果页面==0:
绘制矩形(屏幕,黑色,(0,0,宽度,高度))
屏幕填充(黑色)
rel_backgroundx=backgroundx%backgroundGame.get_rect().width
blit(backgroundGame,(rel_backgroundx-backgroundGame.get_rect().width,0))
如果rel_backgroundx<宽度:
screen.blit(backgroundGame,(rel_backgroundx,0))
屏幕blit(播放器(位置X,位置Y))
屏幕blit(电池,(电池X,420))
电池x-=1
display.flip()
返回页
#def碰撞(电池、播放器):
#如果是battery.Collide Rect(播放画面):
#返回真值
#返回错误
运行=真
myClock=time.Clock()
键左=假
KEY\u RIGHT=False
KEY\u UP=False
按键向下=错误
位置X=0
跳跃=错误
加速度=20
圆形=高度-150
位置Y=圆形
batteryx=随机。随机范围(50,宽度,10)
#游戏循环
运行时:
按钮=0
打印(左键,右键)
对于event.get()中的evnt:#检查所有发生的事件
如果evnt.type==退出:
运行=错误
如果evnt.type==MOUSEBUTTONDOWN:
mx,my=evnt.pos
按钮=evnt.button
如果evnt.type==KEYDOWN:
如果evnt.key==K_左:
左键=真
KEY\u RIGHT=False
如果evnt.key==K_RIGHT:
KEY_RIGHT=True
键左=假
如果evnt.key==K_UP and jumping==False:
跳跃=正确
加速度=20
如果evnt.key==K_向下:
按键向下=正确
KEY\u UP=False
如果evnt.type==KEYUP:
如果evnt.key==K_左:
键左=假
如果evnt.key==K_RIGHT:
KEY\u RIGHT=False
如果evnt.key==K_向下:
按键向下=错误
如果KEY_LEFT==True:
位置X-=10
背景x+=10
如果KEY_RIGHT==True:
位置X+=10
背景x-=10
如果跳跃==真:
位置Y-=加速度
加速度-=1
如果位置Y>=onGround:
跳跃=错误
位置Y=圆形
#玩家不能离开屏幕
如果位置为X400:
位置X=400
#如果碰撞(电池、播放器)==真:
#屏幕填充(黑色)
页面=绘图场景(屏幕、按钮、页面、位置X、位置Y)
myClock.tick(60)#等待时间足够长,每秒60帧
如果页面==6:#如果单击最后一个按钮,程序将关闭
运行=错误
退出

创建用作电池位置的矩形列表。使用for循环更新RECT并在RECT处闪烁电池映像。删除已离开屏幕的矩形并附加新矩形以创建新电池

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')

battery_image = pg.Surface((30, 50))
battery_image.fill(pg.Color('sienna1'))

# Append pygame.Rect objects to this list.
batteries = []
batteries.append(battery_image.get_rect(topleft=(700, 100)))
battery_speed = -5
battery_timer = 40

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    # A simple frame based timer.
    battery_timer -= 1
    if battery_timer <= 0:
        battery_timer = 40
        # After 40 frames a new rect gets appended to the list.
        batteries.append(battery_image.get_rect(topleft=(700, 100)))

    temp_list = []
    # Iterate over the rects to update them.
    for battery_rect in batteries:
        battery_rect.x += battery_speed
        # Rects with x <= 50 won't be appended to the temp_list.
        if battery_rect.x > 50:
            temp_list.append(battery_rect)

    # Assign the list with the remaining rects to the batteries variable.
    batteries = temp_list

    # Blit everything.
    screen.fill(BG_COLOR)
    for battery_rect in batteries:
        screen.blit(battery_image, battery_rect)
    pg.display.flip()
    clock.tick(30)

pg.quit()
将pygame导入为pg
第init页()
屏幕=pg.display.set_模式((640480))
时钟=pg.time.clock()
背景颜色=背景颜色(“灰色12”)
电池图像=pg.表面((30,50))
电池图像填充(pg.Color('SIENA1'))
#将pygame.Rect对象追加到此列表。
电池=[]
batteries.append(battery_image.get_rect(topleft=(700100)))
电池转速=-5
电池定时器=40
完成=错误
虽然没有这样做:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
完成=正确
#一个简单的基于帧的计时器。
电池定时器-=1

如果电池超时,欢迎使用StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。创建一个用作电池位置的RECT列表。使用
for
循环更新RECT,并在RECT处闪烁电池图像。删除离开屏幕的RECT并附加新的RECT以创建新电池。您能在@skrx发布一个示例吗