我对python非常陌生,我一直在试图制作游戏。但我想让背景不断循环下去,我无法做到。 这里是我的代码:使用Pygame在Python中持续向下滚动的背景
import pygame
pygame.init()
width = 800
height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
ship_width = 35
ship_height = 64
disp = pygame.display.set_mode((width,height))
pygame.display.set_caption("Space Jump")
clock = pygame.time.Clock()
bg1 = pygame.image.load("Space.png")
bg2 = pygame.image.load("Space.png")
shipImg = pygame.image.load("Ship.png")
def ship(x,y):
disp.blit(shipImg, (x,y))
def gameLoop():
x = (width * 0.45)
y = (height * 0.8)
x_ch = 0
y_ch = 0
x_bg = 0
y_bg = bg1.get_height()
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_ch = -5
elif event.key == pygame.K_RIGHT:
x_ch = 5
elif event.key == pygame.K_UP:
y_ch = -5
elif event.key == pygame.K_DOWN:
y_ch = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_ch = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_ch = 0
x += x_ch
y += y_ch
if x > width - ship_width or x < 0:
x_ch = 0
if y > height - ship_height or y < 0:
y_ch = 0
disp.blit(bg1, (0,x_bg))
disp.blit(bg2, (0,y_bg))
ship(x,y)
pygame.display.update()
x_bg += 1
y_bg += 1
if x_bg == -1 * x_bg.get_height(): #This is the part I'm stuck on
x_bg = y_bg + y_bg.get_height() #When I run this i get and error:
#if x_bg == -1 * x_bg.get_height():
#AttributeError: 'int' object has no
#attribute 'get_height'
if y_bg == -1 * y_bg.get_height():
y_bg = x_bg + x_bg.get_height()
clock.tick(30)
gameLoop()
pygame.quit()
quit()
所以,当我运行这段代码我得到一个错误,但是当我刚写上一个新的文件,后台代码它的工作原理,但背景在某些时候它没有循环结束。 我一直在尝试做这个过去3天的循环背景,请帮助我。谢谢。
我得到它的工作感谢疯狂的meesh,但背景在某些时候结束可以有人告诉我如何循环它,所以它不会结束? –