Pygame随机移动敌人

时间:2019-12-28 21:13:50

标签: python pygame

我正在尝试将岩石发送到随机位置,我尝试过: 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()

1 个答案:

答案 0 :(得分:2)

我建议通过while True: num=str(input('Type the name of your city:')) if num==' ' or num=='': print('Don´t put only spaces!') continue num1=num.lower().split() num3=num1[0] num2='santo' in num3 if num2==True: print(f'{num} has Santo in the beggining.') break else: print(f'{num} doesn't have Santo in the beggining.') break 的构造函数中的random.uniform(a, b)为x轴生成随机速度:

Rock

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

continueDraw

我建议分别使用pygame.Rectcolliderect()进行碰撞检测:

class Rock:
    # [...]

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