【问题标题】:Spawning multiple objects off screen and making them move down the screen在屏幕外生成多个对象并使它们在屏幕上向下移动
【发布时间】:2020-08-09 17:26:49
【问题描述】:

我正在制作一个游戏,玩家试图避免立方体在屏幕上向下移动。我正在努力在屏幕外创建立方体,并在玩家试图避免时让它们随机从屏幕上掉下来。我也希望这种情况发生,直到玩家击中游戏结束的立方体(我想我可以在程序中完成碰撞部分)。到目前为止,这是我的代码:

import pygame
import random

pygame.init()

screen = pygame.display.set_mode((280, 800))

pygame.display.set_caption("Cube Run")

icon = pygame.image.load("cube.png")
pygame.display.set_icon(icon)

player_icon = pygame.image.load("cursor.png")
player_x = 124
player_y = 750
player_x_change = 0

def player(player_x, player_y):
    screen.blit(player_icon, (player_x, player_y))

running = True
while running:

    screen.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player_x_change += 0.7
            if event.key == pygame.K_LEFT:
                player_x_change -= 0.7
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT or pygame.K_LEFT:
                player_x_change = 0

    player_x += player_x_change
    if player_x < 0:
        player_x = 0
    elif player_x > 280-32:
        player_x = 280-32

    player(player_x, player_y)

    pygame.display.update()

这就是我目前所拥有的一切,包括玩家的移动。 感谢您的宝贵时间。

【问题讨论】:

    标签: python random pygame


    【解决方案1】:

    对于敌方立方体,创建一个 X/Y 坐标数组,每个立方体一个。将 Y 设置为屏幕上方(负数),将 X 设置为小于屏幕宽度的随机数。在主循环中,只需在绘制之前增加每个立方体的 Y 坐标。如果立方体掉到屏幕底部,请再次将 Y 值重置为屏幕上方。根据初始 Y 值,立方体可以波浪形出现。我还在循环底部添加了tick 调用以减慢游戏速度。

    这是更新后的代码:

    import pygame
    import random
    from random import randint
    
    pygame.init()
    
    screen = pygame.display.set_mode((280, 800))
    
    pygame.display.set_caption("Cube Run")
    
    icon = pygame.image.load("cube.png")
    pygame.display.set_icon(icon)
    
    player_icon = pygame.image.load("cursor.png")
    player_x = 124
    player_y = 750
    player_x_change = 0
    
    def player(player_x, player_y):
        screen.blit(player_icon, (player_x, player_y))
    
    # create cubes array [[x,y],[x,y],[x,y],....]
    cubes = [[
         randint(1,260),   # X coordinate
         randint(-500,-20)]   # Y coordinate, -Y is above screen  (top of screen is zero)
         for x in range(20)]  # 20 cubes
    
    # above syntax is a shortcut for the following loop
    #    cubes = []
    #    for x in range(20):
    #       cubes.append([randint(1,260), randint(-500,-20)])
    
    running = True
    while running:
    
        screen.fill((255, 255, 255))  # clear screen
        
        # render cubes
        for cb in cubes:
           cb[1] += 2  # cube moves down 2 pixels
           screen.blit(icon,cb)  # draw cube
           if cb[1] > 800:  # if cube passed bottom of screen
              cb[1] = -20  # move to above screen
              cb[0] = randint(1,260)  # random X position
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    player_x_change += 0.7
                if event.key == pygame.K_LEFT:
                    player_x_change -= 0.7
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT or pygame.K_LEFT:
                    player_x_change = 0
    
        player_x += player_x_change
        if player_x < 0:
            player_x = 0
        elif player_x > 280-32:
            player_x = 280-32
    
        player(player_x, player_y)
    
        pygame.display.update()
        pygame.time.Clock().tick(60)  # slow loop - 60 FPS
    

    【讨论】:

    • 感谢您的回答,但如果您解释一下为什么 for 循环在列表中,可以吗?
    • 这种语法称为列表理解。这是for 循环的快捷方式:检查此页面 (5.1.3):docs.python.org/3/tutorial/datastructures.html
    • 请接受答案以将帖子从“无答案”列表中删除。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-07-21
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多