我正在尝试制作游戏,在这个游戏中我想在0到-100之间的随机Y位置制作100个对象。
但是当我尝试random.randrange(0, -100)
时,它返回了一条错误消息:
Traceback (most recent call last):
File "main.py", line 144, in <module>
main();
File "main.py", line 30, in main
setGlobalValues();
File "main.py", line 23, in setGlobalValues
food = Food();
File "main.py", line 92, in __init__
self.y = random.randrange(0, -100);
File "C:\Users\sidna\AppData\Local\Programs\Python\Python36-32\lib\random.py", line 198, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0,-100, -100)
以下是完整的代码:
PYTHON
# IMPORTS
import pygame, random;
# GLOBALS
global screen, displayW, displayH;
global clock, FPS;
global end, food, player;
# SETGLOBALVALUES
def setGlobalValues():
global screen, displayW, displayH;
global clock, FPS;
global end, food, player;
displayW = 800;
displayH = 600;
screen = pygame.display.set_mode((displayW, displayH));
clock = pygame.time.Clock();
FPS = 60;
end = False;
food = Food();
player = Player();
# MAIN
def main():
pygame.init();
setGlobalValues();
setup();
gameLoop();
quitGame();
# GAMELOOP
def gameLoop():
global end, player;
while(not end):
for event in pygame.event.get():
# ONCLICK QUIT
if(event.type == pygame.QUIT):
end = True;
# KEYDOWN
if(event.type == pygame.KEYDOWN):
if(event.key == pygame.K_LEFT):
player.velX -= 1;
if(event.key == pygame.K_RIGHT):
player.velX += 1;
# KEYUP
if(event.type == pygame.KEYUP):
if(event.key == pygame.K_LEFT):
player.velX = 0;
if(event.key == pygame.K_RIGHT):
player.velX = 0;
draw();
animate();
# DRAW
def draw():
global screen, food, player;
# fill background
screen.fill((255, 255, 255));
food.draw();
# update
pygame.display.update();
# ANIMATE
def animate():
global food, player;
food.animate();
player.animate();
# COLLISION
def collision():
pass;
# CLASSES
class Food():
def __init__(self, x=0, y=0, w=0, h=0, velY=0, color=()):
global displayW;
self.x = random.randrange(0, displayW);
self.y = random.randrange(0, -100);
self.w = 20;
self.h = 20;
self.velY = 0.7;
self.color = (255, 0, 0);
def draw(self):
global screen;
for amount in range(100):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.w, self.h));
def animate(self):
self.y += self.velY;
def collision(self):
global displayW, displayH;
pass;
class Player():
def __init__(self, x=0, y=0, velX=0, velY=0, w=0, h=0, color=()):
global displayW, displayH;
self.w = 20;
self.h = 20;
self.x = displayW / 2 - self.w / 2;
self.y = displayH - 100;
self.velX = 0;
self.velY = 0;
self.color = (0, 0, 0);
def draw(self):
global screen;
pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.w, self.h));
def animate(self):
self.x += self.velX;
self.y += self.velY;
# SETUP
def setup():
pygame.display.set_caption("Food Catcher");
# QUIT GAME
def quitGame():
pygame.quit();
quit();
# CALL MAIN
if(__name__ == "__main__"):
main();
答案 0 :(得分:3)
smaller number should be the first argument(如果您使用多个参数):
import random
random.randrange(-100, 1)
第二个参数未包含在内,因此如果您希望0
作为可能的结果,则需要将其设置为1
。
但是,如果您实际上没有使用step
- 参数,那么还有random.randint
:
random.randint(-100, 0) # randint includes the "stop"!
答案 1 :(得分:0)
介于-100和0之间的随机数与0到100之间的随机数乘以-1
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'X';
$password = 'X';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '</div>';
}
echo $test , $test , $output;
}
imap_close($inbox);
或
-1*random.randrange(100)