【问题标题】:Pygame randomly move enemiesPygame 随机移动敌人
【发布时间】:2019-12-30 04:13:31
【问题描述】:

我正在尝试将岩石发送到随机位置,我尝试过: self.x += random.randint(-5, 5) 但这并不好,因为主循环的每次迭代都会重新计算随机数,这会产生闪烁效果,除了可怕之外,还会挡住岩石的直角。我该如何解决? 我只想从屏幕的高中心开始,让岩石以随机角度下降,然后我会实现一些反弹并创建我的视频游戏,但我被困在这一点上。 这是我第一次在这个论坛上写,所以如果我做错了什么,我提前道歉。

I want this random angle for the rocks

这是我的代码:

import pygame
import random
import time
import math

pygame.init()

display_width = 600
display_height = 500
screen = pygame.display.set_mode( (display_width, display_height) )
fps = 60
font_ubuntumono = pygame.font.SysFont('ubuntumono', 50, True)
rock = pygame.image.load('cave-painting.png').convert_alpha()
rock_width = rock.get_width()
rock_height = rock.get_height()
player = pygame.image.load('wizard.png').convert_alpha()

def initialize():
    global speed_rock, rocks, playerX, playerY
    speed_rock = 2
    playerX = display_width//2
    playerY = 400
    rocks = []
    rocks.append( RockClass() )

class RockClass:
    def __init__(self):
        self.x = display_width//2
        self.y = 0

    def continueDraw(self, a, b):
        self.x += **???**
        self.y += speed_rock
        screen.blit( rock, (self.x, self.y) )

    def collision(self, player, playerX, playerY):
        tolerability = 5
        right_player = playerX + player.get_width() - tolerability
        left_player = playerX + tolerability
        up_player = playerY + tolerability
        right_rock = self.x + rock_width
        left_rock = self.x
        down_rock = self.y + rock_height

        #check collision
        if down_rock > up_player and 
           ( ( left_player < left_rock and left_rock < right_player) or 
               ( right_player > right_rock and right_rock > left_player) ):
            #delete the rock from the screen
            self.x = 5000

initialize()

def update():
    pygame.display.update()
    pygame.time.Clock().tick(fps)

def drawThings():
    #draw background
    screen.fill ( (255, 255, 255) )

    #draw rocks
    for rock2 in rocks:
        rock2.continueDraw(210, 210)

    #draw the player
    screen.blit( player, (playerX, playerY) )

running = True
#main loop
while running:

    #event loop
    for event in pygame.event.get():

        #move player
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX -= 30
            if event.key == pygame.K_RIGHT:
                playerX += 30

        #quit
        if event.type == pygame.QUIT:
            running = False

    #to generate infinite rocks
    if rocks[-1].y > 10:
        rocks.append( RockClass() )

    #check the player-rock collision
    for rock2 in rocks:
        rock2.collision(player, playerX, playerY)

    #bordo alto
    if playerX <= 0:
        playerX = 0
    if playerX >= 600:
        playerX = 600

    #update screen
    drawThings()
    update()

【问题讨论】:

  • 您需要在初始化每块岩石时创建一个随机角度,然后在每个滴答声中沿该方向移动岩石并考虑从侧面反弹
  • 谢谢回复,我不知道怎么把石头往角度的方向移动。
  • 能否请您发布原始代码?此代码不可执行。
  • 原始代码是什么意思?我尝试复制各种游戏,现在我尝试自己做,所以这是原始代码,我没有复制它。编辑:也许你不能运行它,因为你没有资源(图像)。我发布了整个代码,idk:/
  • @C-Gian 否,rocks 未在全局命名空间中定义,因为缺少 global rocks。 rock 被多次使用。首先它是一个pygame.Surface 对象,后来它是一个Rcok 对象。这会导致 screen.blit( rock, (self.x, self.y) ) 出现错误

标签: python pygame


【解决方案1】:

我建议在Rock的构造函数中通过random.uniform(a, b)为x轴生成一个随机速度:

class Rock:
    def __init__(self):
        self.x = display_width//2
        self.y = 0
        self.speed_x = random.uniform(-2, 2)

在continueDraw 中添加self.speed_x 到self.x:

class Rock:
    # [...]

    def continueDraw(self, a, b):
        self.x += self.speed_x
        self.y += speed_rock
        screen.blit( rock_img, (self.x, self.y) )

我建议使用pygame.Rect 和colliderect() 进行碰撞检测:

class Rock:
    # [...]

    def collision(self, player, playerX, playerY):

        tolerability = 5
        player_rect = pygame.Rect(playerX+tolerability, playerY,
                                  player.get_width()-2*tolerability, player.get_height())
        rock_rect = pygame.Rect(self.x, self.y, rock_width, rock_height)

        #check collision
        if player_rect.colliderect(rock_rect):
            #delete the rock from the screen
            self.x = 5000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多