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()
解决方案
如果您尝试在汽车可以行驶的 4 个地方之间进行选择 - 0、100、200、300,请使用 random.choice。random.choice([0, 100, 200, 300])
而不是你的random.randint(0,100,200,300)
,这将给出这些值中的一个随机值
推荐阅读
- mysql - 从简单的 mysql 转换为 Laravel 查询构造函数
- html - 如何防止子元素继承主容器的悬停元素
- javascript - 如何为新样式表生成 CSS 变量值
- python - 具有两个系统安全性的 Web Scraping
- python - Python移动地平线时间序列非线性回归中的不良数据
- javascript - 如何在 redux 操作中每次执行多个函数只使用一个循环
- c++ - 可以只使用 QT 和 WebAssembly(而不是 HTML + CSS + JavaScript)来开发前端 Web 吗?
- swiftui - 将 SwiftUI 视图的锚点居中的简单方法是什么?
- python - 安装 zenipy 时出现问题,ModuleNotFoundError: no module named 'gi'
- ios - 如何在IOS中创建一个选择器视图来选择几个日期