微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

游戏:创建多个障碍物飞向你的船

如何解决游戏:创建多个障碍物飞向你的船

我必须为大学制作一款游戏。游戏应该是这样的:在左侧,您有一个可以控制(上下)的飞机。到目前为止,这非常有效。右侧障碍物正在生成,它们正朝着飞机飞行,因此您必须躲避障碍物。对于我的代码,一开始只产生了一个障碍。它像它应该的那样飞向飞机,并且碰撞检测也起作用。我的问题是,我只是不知道如何产生多个障碍。我曾考虑过实现一个计时器,该计时器每 x 秒就会产生一个新的障碍物,但是如果我在 while 循环中写入诸如“time.sleep”之类的内容,游戏甚至不会开始。我非常感谢每一个想法和提示,因为我会在这个项目上得到一个分数。所以提前非常感谢!如果您还有其他需要了解的内容,请发表评论。最好的问候,蒂莫:)

enter image description here

import pygame
import random
import time
import math

# Initialize pygame
pygame.init()

# Create window (width,height)
screen = pygame.display.set_mode(((800,600)))
ScreenHeight = screen.get_height()
ScreenWidth = screen.get_width()

# Background picture
background = pygame.image.load("background.jpg")

# Title and Icon
pygame.display.set_caption("F22-Raptor Simulator")
icon = pygame.image.load("jet1.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load("player1.png")
playerX = 10
playerY = 270
playerY_change = 0


def player(x,y):
    screen.blit(playerImg,(x,y))


# Obstacle
obstacleImg = pygame.image.load("rock.png")
obstacleX = random.randint(600,700)
obstacleY = random.randint(0,ScreenHeight - 64)
obstacleX_change = -0.3


def obstacle(x,y):
    screen.blit(obstacleImg,y))


# Collosion detection (Player with obstacle)
def collisionDetection(playerX,playerY,obstacleX,obstacleY):
    distance = math.sqrt((math.pow(playerX - obstacleX,2)) + ((math.pow(playerY - obstacleY,2))))
    if distance < 64:
        return True
    else:
        return False


# Keep window running (Infinite-Loop)
running = True

# While-Loop (Everything that takes place during the game is inside here
while running:

    # Background-Color of window
    screen.fill((192,192,192))

    # Insert Background
    screen.blit(background,(0,0))

    # End game / close window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Movement up/down
        if event.type == pygame.KEYDOWN:
            # Movement up
            if event.key == pygame.K_w:
                playerY_change = -0.15
            # Movement down
            if event.key == pygame.K_s:
                playerY_change = 0.15
        # If no key pressed -> no movement
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                playerY_change = 0

    # display movement of player
    playerY += playerY_change

    # Movement-boundaries Player
    if playerY <= 0:
        playerY = 0
    if playerY >= 536:
        playerY = 536

    # display player
    player(playerX,playerY)

    # display movement of obstacle
    obstacleX += obstacleX_change

    # display obstacle

    obstacle(obstacleX,obstacleY)

    # Collision Detection
    collision = collisionDetection(playerX,obstacleY)
    if collision:
        playerX = 10
        playerY = 270
        player(playerX,playerY)

    # Update after each iteration of the while-loop
    pygame.display.update()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。