Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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_Python 3.x_Pygame - Fatal编程技术网

Python 如何在pygame中调整图像大小以达到屏幕的顶部/底部?

Python 如何在pygame中调整图像大小以达到屏幕的顶部/底部?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在pygame中重新制作flappy bird,但有一个星球大战主题。我已经完成了游戏的艺术和一般格式,但现在我需要调整细节。我一直在切换数字,试图让光剑完全到达屏幕的顶部和底部,因为此时有时会出现一些间隙,而这些间隙不是预期要通过的空间。 导入pygame 从随机导入randint 从pygame.locals导入* #定义颜色-RGB 黑色=(0,0,0) 白色=(255255) 绿色=(0255,0) 红色=(255,0,0) pygame.init() #屏幕大小 尺寸=70050

我正在pygame中重新制作flappy bird,但有一个星球大战主题。我已经完成了游戏的艺术和一般格式,但现在我需要调整细节。我一直在切换数字,试图让光剑完全到达屏幕的顶部和底部,因为此时有时会出现一些间隙,而这些间隙不是预期要通过的空间。

导入pygame
从随机导入randint
从pygame.locals导入*
#定义颜色-RGB
黑色=(0,0,0)
白色=(255255)
绿色=(0255,0)
红色=(255,0,0)
pygame.init()
#屏幕大小
尺寸=700500
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“Python中的Flappy Bird”)
完成=错误
clock=pygame.time.clock()
def球(x,y):
#半径为20px
ballImg1=pygame.image.load('PlayerFrame1.png')
ballImg1=pygame.transform.scale(ballImg1,(50,50))
屏幕blit(ballImg1,(x,y))
ballImg2=pygame.image.load('PlayerFrame2.png')
ballImg2=pygame.transform.scale(ballImg2,(50,50))
屏幕光点(ballImg2,(x,y))
def gameover():
font=pygame.font.SysFont(无,55)
text=font.render(“游戏结束!重试?”,True,红色)
screen.blit(文本,[150250])
def障碍物(xloc、yloc、xsize、ysize):
pipe=pygame.image.load('blade.png')
pipe1=pygame.transform.scale(管道,(xsize,ysize))
pipe2=pygame.transform.scale(管道,(xsize,500))
blit(pipe1,[xloc,yloc,xsize,ysize])
blit(pipe2,[xloc,int(yloc+ysize+space),xsize,ysize+500]))
#如果球在屏幕上的2分之间,则增加得分
def分数(分数):
font=pygame.font.SysFont(无,55)
text=font.render(“分数:+str(分数),真,白色)
blit(文本,[0,0])
x=350
y=250
x_速度=0
y_速度=0
接地=477
xloc=700
yloc=0
xsize=70
ysize=randint(0350)
#两个块之间的空间大小
空间=150
obspeed=2
分数=0
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_UP:
y_速度=-5
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_UP:
y_速度=4
屏幕填充(黑色)
障碍物(xloc、yloc、xsize、ysize)
球(x,y)
分数
y+=y_速度
xloc-=障碍速度
如果y>接地:
gameover()
y_速度=0
obspeed=0
如果x+20>xloc且y-20xloc,y+20xloc和x
如果我理解您的变量名,可能在函数的第四行中,请尝试:


您应该只加载一次图像,然后重新缩放到窗口高度,并将其大小和位置设置为
pygame.Rect()
(使用
get_Rect())

然后可以为每个管道创建
pygame.Rect()

pipe1_rect = image_rect.copy()
pipe2_rect = image_rect.copy()
若间隙大小为200,则顶部应为300像素

gap_size = 200
gap_top = 300
然后确定管道的位置

pipe1_rect.bottom = gap_top
pipe2_rect.top = pipe1_rect.bottom + gap_size
你会把它当作

screen.blit(image, pipe1_rect)
screen.blit(image, pipe2_rect)
你将它从右向左移动

pipe1_rect.x -= 1
pipe2_rect.x -= 1

示例代码

编辑:我添加了:gap处于随机位置,管道正在移动,当球触碰管道时,它会在控制台/终端上打印“游戏结束”,当管道触碰屏幕左侧时,它会在控制台/终端上打印“赢”

import pygame
import random 

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 60

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)

# --- classes --- (CamelCaseNames)

# empty 

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
screen_rect = screen.get_rect()

image = pygame.image.load("Obrazy/images/paddle.png").convert()
image = pygame.transform.scale(image, (50, SCREEN_HEIGHT))
image_rect = image.get_rect()

pipe1_rect = image_rect.copy()
pipe2_rect = image_rect.copy()

pipe1_rect.right = screen_rect.right # move to right of screen
pipe2_rect.right = screen_rect.right # move to right of screen

gap_size = 200
gap_top = 300

#pipe1_rect.bottom = gap_top
pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
pipe2_rect.top = pipe1_rect.bottom + gap_size

ball_rect = pygame.Rect((0,0,100,100))
ball_rect.center = screen_rect.center

ball_speed = 5

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    # --- events ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                ball_speed = -3

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                ball_speed = 4

    # --- changes/moves/updates ---

    ball_rect.y += ball_speed

    pipe1_rect.x -= 1
    pipe2_rect.x -= 1

    if pipe1_rect.colliderect(ball_rect) or pipe2_rect.colliderect(ball_rect):
        print('Game Over')
        pipe1_rect.right = screen_rect.right
        pipe2_rect.right = screen_rect.right
        pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
        pipe2_rect.top = pipe1_rect.bottom + gap_size

    if pipe1_rect.left == 0:
        print("WIN")
        pipe1_rect.right = screen_rect.right
        pipe2_rect.right = screen_rect.right
        pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
        pipe2_rect.top = pipe1_rect.bottom + gap_size

    # --- draws ---

    screen.fill(BLACK)

    screen.blit(image, pipe1_rect)
    screen.blit(image, pipe2_rect)

    pygame.draw.rect(screen, GREEN, ball_rect)

    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

# --- end ---

pygame.quit()

你有没有试过使用很长的图像(只要屏幕很高),然后把它们放在“后缘”离开屏幕的地方?显然,屏幕顶部的图像需要按位图的底部边缘放置,而不是顶部坐标。
PyGame
具有
PyGame.Rect()
以保持对象的位置和大小,您可以使用它来blit image
blit(image,Rect)
。它具有类似于
rect.x
rect.y
的属性,但也具有
rect.bottom
的属性,可以与
rect.bottom=screen.get\rect()一起使用.bottom
将对象放在屏幕底部。顺便说一句:您应该只加载和重新缩放一次图像-现在您可以在一秒钟内加载并缩放它60次。如果您希望重新缩放顶部和底部管道,则应使用“缩放两次”创建两个具有不同高度的零件。如果第一个使用
ysize
,那么第二个应该在开始时使用
screen\u height-ysize-gap\u size
,您可以创建与屏幕高度相同高度的图像,然后您不必稍后调整图像大小,只需将其放置在正确的位置。请编辑您的答案,解释它如何解决OP的问题。这并不完全正确工作这使得军刀之间的空间不存在。刀下还有空间,我刚把它修好。。。我将管道添加为两个独立的对象。。。除了现在,用户可以在不输掉比赛的情况下通过底部的马刀传球?底部的军刀似乎不再有杀手锏了?这非常有用。。。关于rect()是否有用,您说得很对。唯一的问题是,现在我不知道如何实现球的移动?使用
pygame.Rect()
保持球的位置-
ball\Rect
。当您按键时,然后更改速度,稍后在部分
中更改/移动/更新“
将此速度添加到
ball_rect.y
是否只是将旧版本的代码添加到该部分?我将此添加到示例中。但我不得不用较低的速度值。我非常感谢你们!它终于完全按照我的想象工作了。。。看到一个项目成功真是太有趣了。再次感谢!
screen.blit(image, pipe1_rect)
screen.blit(image, pipe2_rect)
pipe1_rect.x -= 1
pipe2_rect.x -= 1
import pygame
import random 

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 60

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)

# --- classes --- (CamelCaseNames)

# empty 

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
screen_rect = screen.get_rect()

image = pygame.image.load("Obrazy/images/paddle.png").convert()
image = pygame.transform.scale(image, (50, SCREEN_HEIGHT))
image_rect = image.get_rect()

pipe1_rect = image_rect.copy()
pipe2_rect = image_rect.copy()

pipe1_rect.right = screen_rect.right # move to right of screen
pipe2_rect.right = screen_rect.right # move to right of screen

gap_size = 200
gap_top = 300

#pipe1_rect.bottom = gap_top
pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
pipe2_rect.top = pipe1_rect.bottom + gap_size

ball_rect = pygame.Rect((0,0,100,100))
ball_rect.center = screen_rect.center

ball_speed = 5

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    # --- events ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                ball_speed = -3

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                ball_speed = 4

    # --- changes/moves/updates ---

    ball_rect.y += ball_speed

    pipe1_rect.x -= 1
    pipe2_rect.x -= 1

    if pipe1_rect.colliderect(ball_rect) or pipe2_rect.colliderect(ball_rect):
        print('Game Over')
        pipe1_rect.right = screen_rect.right
        pipe2_rect.right = screen_rect.right
        pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
        pipe2_rect.top = pipe1_rect.bottom + gap_size

    if pipe1_rect.left == 0:
        print("WIN")
        pipe1_rect.right = screen_rect.right
        pipe2_rect.right = screen_rect.right
        pipe1_rect.bottom = random.randint(50, SCREEN_HEIGHT-gap_size-50)
        pipe2_rect.top = pipe1_rect.bottom + gap_size

    # --- draws ---

    screen.fill(BLACK)

    screen.blit(image, pipe1_rect)
    screen.blit(image, pipe2_rect)

    pygame.draw.rect(screen, GREEN, ball_rect)

    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

# --- end ---

pygame.quit()