我一直在嘗試製作過去2天的太空射擊遊戲,我想讓背景滾動下來,但我不知道如何。 繼承人我的代碼:如何使用pygame在python中製作滾動背景?
import pygame
import time
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()
background = pygame.image.load("Space.png").convert()
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
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
x_bg = 0
while True:
disp.blit(background, (0,x_bg)) # i tried this but when i launch
#a black screen just appears without
x_bg -= 1 #any image
ship(x,y)
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
我有一個背景,我希望把它向下滾動連續你能幫助我嗎?
[添加SCRO的可能的複製在pygame中的平臺遊戲](https://stackoverflow.com/questions/14354171/add-scrolling-to-a-platformer-in-pygame) –