我如何让我的随机化器在特定地点随机化我的汽车

2024-03-29 10:10:52 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,正如问题所说,我希望我的随机化器在特定坐标中随机化我的车/敌人,但每次我在我的车随机化器中添加2个以上的数字时,我都会得到这个错误Car1.x = random.randint(0,100,200,300) TypeError: randint() takes 3 positional arguments but 5 were given。我该如何做才能选择我的随机化器将随机化我的车的点? I want it to only randomize my x coordination 我的随机化程序代码

for Car1 in car1s:
        if Car1.y < 1:
            Car1.y = 400

    if Car1.y == 1:
        Car1.x = random.randint(0,100,200,300)

我的完整代码

import pygame
import random
pygame.init()
# Size of screen
window = pygame.display.set_mode((700,500))
# Name of screen
pygame.display.set_caption("My Game")
# putting my background in the game
BG = pygame.image.load("BG_1.png")
# Car beeping sound
BEP = pygame.mixer.Sound("BEEP.ogg")
BEP2 = pygame.mixer.Sound("BEEP_2.ogg")
BEP3 = pygame.mixer.Sound("BEEP_3.ogg")

# Player class
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.fall = 0
        self.JumpCount = 10

        # This is for the player animation
        self.idle = [pygame.image.load("Player_idle1.png"),
                     pygame.image.load("Player_idle2.png"),
                     pygame.image.load("Player_idle3.png"),
                     pygame.image.load("Player_idle4.png")]
        
        self.idlel = [pygame.image.load("Player_idlel1.png"),
                      pygame.image.load("Player_idlel2.png"),
                      pygame.image.load("Player_idlel3.png"),
                      pygame.image.load("Player_idlel4.png")]
        
        self.run =  [pygame.image.load("Player_run1.png"),
                     pygame.image.load("Player_run2.png"),
                     pygame.image.load("Player_run3.png"),
                     pygame.image.load("Player_run4.png"),
                     pygame.image.load("Player_run5.png"),
                     pygame.image.load("Player_run6.png")]
        
        self.lrun = [pygame.image.load("Player_lrun1.png"),
                     pygame.image.load("Player_lrun2.png"),
                     pygame.image.load("Player_lrun3.png"),
                     pygame.image.load("Player_lrun4.png"),
                     pygame.image.load("Player_lrun5.png"),
                     pygame.image.load("Player_lrun6.png")]

        self.idle = [pygame.transform.scale(image,(image.get_width()*3,image.get_height()*3))for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()*3,image.get_height()*3))for image in self.idlel]
        self.run = [pygame.transform.scale(image,(image.get_width()*3,image.get_height()*3))for image in self.run]
        self.lrun = [pygame.transform.scale(image,(image.get_width()*3,image.get_height()*3))for image in self.lrun]
        ## A part of the player animation and making sure it works
        self.direction = "idle"
        self.direction = "idlel"
        self.direction = "run"
        self.direction = "lrun"
        self.isJump = False
        self.fps = 10
        clock = pygame.time.Clock()
        self.anim_index = 0
        self.next_frame_time = 0
        self.rect = pygame.Rect(x,y,width,height)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)
        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "idlel":
            image_list = self.idlel
        if self.direction == "run":
            image_list = self.run
        if self.direction == "lrun":
            image_list = self.lrun

        # Is it time to show next frame?
        time_now = pygame.time.get_ticks()
        if (time_now > self.next_frame_time):
            # How many seconds we have to wait until the next frame
            inter_time_delay = 1000 // self.fps
            self.next_frame_time = time_now + inter_time_delay
            # showing next frame
            self.anim_index += 1
            if self.anim_index >= len(image_list):
                self.anim_index = 0

        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 1
        player_rect.centery -= 17
        window.blit(player_image,player_rect)


# All Cars going up
# Car1
class Car1:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 1
        self.car = pygame.image.load("UGC.png")
        self.car = pygame.transform.scale(self.car,(self.car.get_width()//2,self.car.get_height()//2))
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        car_rect = self.car.get_rect(center = self.rect.center)
        car_rect.centerx += 0
        car_rect.centery += 2
        window.blit(self.car,car_rect)



# DRawing color
white = (255,255,255)

# Player size,and cords
playerman = Player(255,255,40,40,white)

# Cars that go up
# Car1 size and cords 110 250 410 550
car1 = Car1(90,250,35,60,white)


# Car1 list
car1s = [car1]


# Drawing my stuff on main screen
def redrawwindow():
    window.fill((0,0,0))
    # Loading the background for my game
    window.blit(BG,(0,0))

    # Player getting drawn in main screen
    playerman.draw()

    # Car1 getting drawn in main screen
    for Car1 in car1s:
        Car1.draw()
        
    


      
# The fps for game
fps = (60)
clock = pygame.time.Clock()
# timer
Btimer = 0
# seconds timer
Btimer2 = 0
# third timer
Btimer3 = 0
# Main loop
run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    for Car1 in car1s:
        if Car1.y < 1:
            Car1.y = 400

    if Car1.y == 1:
        Car1.x = random.randint(0,100,200,300)



    # timer for first sound
    if Btimer > 0:
        Btimer += 1
    if Btimer >= 450:
        Btimer = 0
    # timer for second sound
    if Btimer2 > 0:
        Btimer2 += 1
    if Btimer2 >= 550:
        Btimer2 = 0        
    # timer for third sound
    if Btimer3 > 0:
        Btimer3 += 1
    if Btimer3 >= 650:
        Btimer3 = 0

    # first sound getting played when btimer = 0
    if Btimer == 0:
        BEP.play()
    Btimer += 1
    # Second sound getting played when btimer2 = 0
    if Btimer2 == 0:
        BEP2.play()
    Btimer2 += 1
    # third sound getting played when btimer3 = 0
    if Btimer3 == 0:
        BEP3.play()
    Btimer3 += 1

    keys = pygame.key.get_pressed()

    # all for player moving
    if keys[pygame.K_a] and playerman.x > playerman.speed:
            playerman.x -= playerman.speed
            playerman.direction = "lrun"

    elif keys[pygame.K_d] and playerman.x < 700 - playerman.height - playerman.speed:
        playerman.x += playerman.speed
        playerman.direction = "run"
    else:
        if playerman.direction == "run":
            playerman.direction = "idle"
        else:
            if playerman.direction == "lrun":
                playerman.direction = "idlel"
    if keys[pygame.K_w] and playerman.y > playerman.speed:
        playerman.y -= playerman.speed
    if keys[pygame.K_s] and playerman.y <500 - playerman.width - playerman.speed:
        playerman.y += playerman.speed


    


    # redrawing the window
    redrawwindow()
    # updating the screen
    pygame.display.update()
# quiting the game
pygame.quit()

        


2条回答

如果您试图在汽车可以行驶的4个位置之间进行选择-0、100、200、300,则使用random.choicerandom.choice([0, 100, 200, 300])而不是您的random.randint(0,100,200,300),它将给出这些值中的一个随机值

我不知道这些数字是用来做什么的:random.randint(0,100,200,300)但这不是你使用random.randint的方式

从帮助中:

Help on method randint in module random:

randint(a, b) method of random.Random instance Return random integer in range [a, b], including both end points.

如果要获取二维平面中的坐标,可能需要尝试调用两次(每个坐标调用一次):

my_car_x = random.randint(min_x, max_x)
my_car_y = random.randint(min_y, max_y)

相关问题 更多 >