Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pygame中添加敌人?_Python_Pygame - Fatal编程技术网

Python 如何在pygame中添加敌人?

Python 如何在pygame中添加敌人?,python,pygame,Python,Pygame,我正在用pygame做一个简单的游戏,目标是躲避来袭的敌人。我想要一个tie战斗机的图像随机下来,xwing必须躲避它们,否则你会死。我如何在我的脚本中实现随机绑定?在我的代码中,我得到一个错误,说x没有定义 import pygame import time import random pygame.init() display_width = 1280 display_height= 800 pygame.display.set_mode((display_width, display

我正在用pygame做一个简单的游戏,目标是躲避来袭的敌人。我想要一个tie战斗机的图像随机下来,xwing必须躲避它们,否则你会死。我如何在我的脚本中实现随机绑定?在我的代码中,我得到一个错误,说x没有定义

import pygame
import time
import random
pygame.init()

display_width = 1280
display_height= 800


pygame.display.set_mode((display_width, display_height), pygame.FULLSCREEN)

gameDisplay= pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('X-Wing Game')
clock= pygame.time.Clock()

black= (0,0,0)
white= (255,255,255)
red = (255,0,0)
blue_violet = (138,43,226)


xwing_width = 65
xwing_height = 130

tie_width = 80
tie_height =64

#images
xwingImg = pygame.image.load('X-Wing.bmp')
tieImg= pygame.image.load('tiefighter.png')

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

def tieImg (x,y):
    pygame.image.load('tieImg')


def xwing (x,y):
    gameDisplay.blit (xwingImg,(x,y))

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

def crash ():
    message_display ('Ouch, You Crashed!')

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',25)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(1.3)

    game_loop()


def game_loop():

    x = (display_width * 0.45)
    y = (display_height * .75)

    x_change = 0
    y_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 7
    thing_width = 81
    thing_height =65


    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
                if event.key == pygame.K_UP:
                     y_change= -5
                if event.key == pygame.K_DOWN:
                    y_change =5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5


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



        x += x_change
        y += y_change

        gameDisplay.fill(black)

        things(thing_startx, thing_starty, thing_width, thing_height, black )
        thing_starty += thing_speed
        xwing (x,y)

        if x > display_width - xwing_width or x < 0:
             x_change=0
        if y>display_height-xwing_height or y<0:
            y_change=0


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


game_loop()
pygame.quit()
quit()
导入pygame
导入时间
随机输入
pygame.init()
显示宽度=1280
显示高度=800
pygame.display.set_模式((显示宽度、显示高度),pygame.FULLSCREEN)
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题(“X翼游戏”)
clock=pygame.time.clock()
黑色=(0,0,0)
白色=(255255)
红色=(255,0,0)
蓝紫=(138,43226)
xwing_宽度=65
xwing_高度=130
领带宽度=80
系紧高度=64
#图像
xwingImg=pygame.image.load('X-Wing.bmp')
tieImg=pygame.image.load('tiefighter.png')
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
游戏显示.blit(tieImg,(x,y))
def tieImg(x,y):
pygame.image.load('tieImg')
def x机翼(x,y):
gameDisplay.blit(xwingImg,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本,真,红色)
返回textSurface,textSurface.get_rect()
def crash():
信息显示(“哎哟,你崩溃了!”)
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',25)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(1.3)
博弈(循环)
def game_loop():
x=(显示宽度*0.45)
y=(显示高度*.75)
x_变化=0
y_变化=0
thing\u startx=random.randrange(0,显示宽度)
事情开始=-600
速度=7
宽度=81
物体高度=65
gameEXIT=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-5
如果event.key==pygame.K_UP:
y_变化=-5
如果event.key==pygame.K_向下:
y_变化=5
elif event.key==pygame.K_RIGHT:
x_变化=5
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_左或event.key==pygame.K_右:
x_变化=0
x+=x_变化
y+=y_变化
游戏显示。填充(黑色)
事物(事物开始,事物开始,事物宽度,事物高度,黑色)
物体开始速度+=物体速度
x翼(x,y)
如果x>显示宽度-x宽度或x<0:
x_变化=0

如果y>display_height-xwing_height或y,您可以使用一个列表并附加到该列表中,该列表存储敌人的位置和大小,然后只需在rect位置闪烁敌人的图像。对于碰撞检测,循环敌人列表,并使用
Rect.collide Rect
方法检查玩家Rect是否与敌人Rect发生碰撞

import sys
import random
import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))

XWING_IMG = pg.Surface((25, 38))
XWING_IMG.fill((90, 120, 150))
TIE_IMG = pg.Surface((40, 24))
TIE_IMG.fill((190, 60, 50))
BG_COLOR = pg.Color('gray15')


def main():
    clock = pg.time.Clock()
    # Surfaces/images have a `get_rect` method which 
    # returns a rect with the dimensions of the image.
    player_rect = XWING_IMG.get_rect()
    player_rect.center = (300, 400)
    change_x = 0
    change_y = 0
    enemies = []
    spawn_counter = 30

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_d:
                    change_x = 5
                if event.key == pg.K_a:
                    change_x = -5
            if event.type == pg.KEYUP:
                if event.key == pg.K_d and change_x > 0:
                    change_x = 0
                if event.key == pg.K_a and change_x < 0:
                    change_x = 0

        # Spawn enemies if counter <= 0 then reset it.
        spawn_counter -= 1
        if spawn_counter <= 0:
            # Append an enemy rect. You can pass the position directly as an argument.
            enemies.append(TIE_IMG.get_rect(topleft=(random.randrange(600), 0)))
            spawn_counter = 30

        # Update player_rect and enemies.
        player_rect.x += change_x
        player_rect.y += change_y
        for enemy_rect in enemies:
            enemy_rect.y += 5
            # Collision detection with pygame.Rect.colliderect.
            if player_rect.colliderect(enemy_rect):
                print('Collision!')

        # Draw everything.
        screen.fill(BG_COLOR)
        for enemy_rect in enemies:
            screen.blit(TIE_IMG, enemy_rect)
        screen.blit(XWING_IMG, player_rect)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()
    sys.exit()
导入系统 随机输入 导入pygame作为pg 第init页() 屏幕=pg.display.set_模式((640480)) XWING_IMG=pg.表面((25,38)) XWING_IMG.fill((90120150)) 拉杆IMG=pg.表面((40,24)) 连接填料(190、60、50)) BG_COLOR=pg.COLOR('gray15') def main(): 时钟=pg.time.clock() #曲面/图像具有“get_rect”方法,该方法 #返回一个包含图像尺寸的矩形。 player_rect=XWING_IMG.get_rect() 玩家右中锋=(300400) 改变x=0 改变y=0 敌人=[] 产卵计数器=30 完成=错误 虽然没有这样做: 对于pg.event.get()中的事件: 如果event.type==pg.QUIT: 完成=正确 如果event.type==pg.KEYDOWN: 如果event.key==pg.K\u d: 变化x=5 如果event.key==pg.K_a: 变化x=-5 如果event.type==pg.KEYUP: 如果event.key==pg.K_d且change_x>0: 改变x=0 如果event.key==pg.K_a且change_x<0: 改变x=0
#如果在你的
东西
功能中使用计数器,
x
确实是未定义的,那么
y
也将是未定义的。你的意思是键入
gameDisplay.blit(tieImg,(thingx,thingy))
吗?你已经知道如何使用类、pygame精灵和精灵组了吗?如果没有,你可以使用一个列表,并在其上附加一个存储敌人位置和大小的s,然后只需在rect位置闪烁敌人的图像。另外,对玩家和敌人列表上的碰撞检测循环使用rect,并检查
如果玩家正确。碰撞rect(敌人正确):
。我想我必须发布一个例子。我还建议大家看看类和
pygame.sprite.sprite
s(第12章和第13章)。