Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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(和编码)新手,不知道如何让我的图像(xwing)从屏幕的一侧转到屏幕的另一侧。我的当前代码有一个错误,上面写着 Traceback (most recent call last): File "/Users/Grant/Documents/xwingexample.py", line 85, in <module> main() File "/Users/Grant/Documents/xwingexample.py", line 61, in ma

我是pygame(和编码)新手,不知道如何让我的图像(xwing)从屏幕的一侧转到屏幕的另一侧。我的当前代码有一个错误,上面写着

Traceback (most recent call last):
  File "/Users/Grant/Documents/xwingexample.py", line 85, in <module>
    main()
  File "/Users/Grant/Documents/xwingexample.py", line 61, in main
    player_rect.x += change_x
AttributeError: 'int' object has no attribute 'x'
回溯(最近一次呼叫最后一次):
文件“/Users/Grant/Documents/xwingexample.py”,第85行,在
main()
文件“/Users/Grant/Documents/xwingexample.py”,第61行,主目录
玩家_rect.x+=更改_x
AttributeError:“int”对象没有属性“x”
谢谢

这是代码

import sys
import random
import pygame as pg


pg.init()
screen = pg.display.set_mode((1280, 800))
display_width = 1280
display_height= 800

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

xwingImg = pg.image.load('X-Wing.bmp').convert()
tieImg= pg.image.load('tiefighter.png').convert()
space=pg.image.load('space.jpg').convert()

BG_image = pg.image.load('space.jpg').convert()


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 = xwingImg.get_rect()
    player_rect.center = ( 800,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(tieImg.get_rect(topleft=(random.randrange(1280), -800 )))
            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.blit(BG_image, (0,0))
        for enemy_rect in enemies:
            screen.blit(tieImg, enemy_rect)
        screen.blit(xwingImg, player_rect)

        if player_rect.x > display_width:
            player_rect = 0
        if player_rect.y < display_width:
            player_rect= display_width

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


if __name__ == '__main__':
    main()
    pg.quit()
    sys.exit()
导入系统 随机输入 导入pygame作为pg 第init页() 屏幕=pg.display.set_模式((1280800)) 显示宽度=1280 显示高度=800 黑色=(0,0,0) 白色=(255255) 红色=(255,0,0) 蓝紫=(138,43226) xwingImg=pg.image.load('X-Wing.bmp').convert() tieImg=pg.image.load('tiefighter.png').convert() space=pg.image.load('space.jpg').convert() BG_image=pg.image.load('space.jpg').convert() def main(): 时钟=pg.time.clock() #曲面/图像具有“get_rect”方法,该方法 #返回一个包含图像尺寸的矩形。 player_rect=xwingImg.get_rect() 玩家右中锋=(800400) 改变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
#繁殖敌人如果计数器检查第75-78行,您尝试将整数分配给
player\u rect
,因此在此之后,
player\u rect.x+=change\u x
返回此错误:

'int' object has no attribute 'x'
我想您想更改
player_rect.x
player_rect.y
,所以您可以试试这个,它工作得很好

if player_rect.x > display_width:
    player_rect.x = 0
if player_rect.y < display_width:
    player_rect.y= display_width
if player_rect.x>显示宽度:
player_rect.x=0
如果播放器右y<显示宽度:
player_rect.y=显示宽度