Pygame:有没有办法在不设置背景图像的情况下滚动白色填充显示器的'coordinate system'?



我有一个游戏显示器,在上面我使用了blit-功能来显示飞行路径和无人机。飞行路径从显示器的右侧开始,并超出显示器的左侧。

游戏显示屏是白色的,我想要的是通过按键沿着飞行路径从右向左移动我的无人机(这只是一组连接随机点的连续线(。

我想让显示器的"坐标系"移动/滚动,这样你就可以看到飞行路线的终点。同时,我希望我的无人机在滚动过程中保持静止位置,例如,当它沿着飞行路径飞行时,保持在屏幕中间。

有人知道能让我做到这一点的功能吗?我在论坛和YouTube上发现的一切似乎都相当复杂,需要先设置背景图像。我只想在我将无人机向左移动以遵循红色飞行路径时,滚动白色填充的屏幕。以下是我迄今为止的编码。

提前非常感谢您的建议!

import pygame
import pygame.gfxdraw
import random
import sys

white = (255,255,255)
display_width = 1200
display_height = 700
game_screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('gameScreen')
the_drone = pygame.image.load('drone.png')

X=1000
Y=350
p1=[X, Y]
p2=[X, Y]
p3=[X, Y]
p4=[X, Y]
p5=[X, Y]
pointlist = [p1, p2, p3, p4, p5]
limit1=1000
limit2=850

for i in pointlist:
i[0] = random.randrange(limit2, limit1)
limit1-=300
limit2-=300

for i in pointlist:
if i == 0:
i[1] = random.randrange(200, 600)
else:
range = i[1]-1
i[1] = random.randrange(range-100, range+100)

def flightpath(pointlist):
pygame.draw.lines(game_screen, (255, 0, 0), False, pointlist, 3)


def drone(x,y):
game_screen.blit(the_drone,(X,Y))   


def game_loop():
global X, Y
gameExit = False
while not gameExit:

for event in pygame.event.get():

if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()    
keys = pygame.key.get_pressed()  #checking pressed keys
if keys[pygame.K_LEFT]:
X -= 0.5
if keys[pygame.K_DOWN]:
Y -= 0.5
if keys[pygame.K_UP]:
Y +=0.5 
game_screen.fill(white)

flightpath(pointlist)

drone(X,Y)        

pygame.display.update()

game_loop()
pygame.quit()
sys.exit()    

嗨,我真的不懂你的代码,但我能做到:

import pygame
import sys
import random

# init window
def init():
pygame.init()
pygame.display.set_caption("Drone Game")
screen = pygame.display.set_mode((500, 500))
return screen

# make closing the window possible
def escape():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

# draws all objects on screen
def draw(screen, f, y_pos):
screen.fill((50, 50, 50))
for y in range(20):
for x in range(5):
pygame.draw.rect(screen, f[y][x], (x * 100, (y * 100) - y_pos, 100, 100))
pygame.draw.rect(screen, (250, 0, 0), (240, 240, 20, 20))  # drone
pygame.display.update()

# creates background
def field():
f = []
for y in range(20):
f.append([])
for x in range(5):
f[y].append((random.randint(200, 255), random.randint(200, 255), random.randint(200, 255)))
return f

# combines all functions
def main(screen):
f = field()
y_pos = 500
while True:
pygame.time.Clock().tick(30)
escape()
y_pos -= 1
draw(screen, f, y_pos)
# starts program
if __name__ == '__main__':
main(init())

我希望它对你有用

最新更新