Python 如何创建另一个传入矩形?
我做了一个小游戏,你的玩家是一辆车,一辆车向你冲过来。我想有两辆车向你驶来,但我不知道怎么发动。我知道我需要另一个曲面和Rect值,因为它需要能够检查碰撞。还有,我是否可以让两辆车同时从一侧驶来,或者这是不可能的Python 如何创建另一个传入矩形?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我做了一个小游戏,你的玩家是一辆车,一辆车向你冲过来。我想有两辆车向你驶来,但我不知道怎么发动。我知道我需要另一个曲面和Rect值,因为它需要能够检查碰撞。还有,我是否可以让两辆车同时从一侧驶来,或者这是不可能的 import time, pygame, random import math pygame.init() #First Variables BLACK = (0, 0, 0) BLUE = (0, 0, 255) GREEN = (100, 255, 100) ORANGE = (
import time, pygame, random
import math
pygame.init()
#First Variables
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (100, 255, 100)
ORANGE = (255, 140, 0)
YELLOW = (155, 135, 12)
white = (255, 255, 255)
GOLD = (255, 215, 0)
screenwidth = 500
screenheight = 500
Lines = True
Space = True
color = random.sample(range(250), 3)
doris = []
i = []
x = 247
y = - 20
y1 = 40
y2 = 100
y3 = 160
y4 = 220
y5 = 280
y6 = 340
y7 = 400
y8 = 460
#Display and caption
win = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Lil cary')
clock = pygame.time.Clock()
#Load in images
bkg = pygame.image.load('BG.jpg')
#Actual player class
class player():
def __init__(self):
self.color = (12, 124, 134)
self.vel = 5
self.grassdamage = 0
self.image = pygame.Surface([50, 100], pygame.SRCALPHA, 32)
pygame.draw.rect(self.image, (self.color), (0, 0, 50, 100))
self.rect = self.image.get_rect(center = (200, 390))
self.damage1 = False
self.damage2 = False
self.damage3 = False
self.damage4 = False
self.damage5 = False
self.damage6 = False
self.damage7 = False
self.damage8 = False
self.dead = False
def draw (self, win):
#pygame.draw.rect(win, (self.color), (self.x, self.y, self.width, self.height))
win.blit(self.image, self.rect.topleft)
def moveback(self):
if self.rect.y < 390 and self.rect.y >= 0:
self.rect.y += 10
def grass(self):
if self.rect.x > -10 and self.rect.x < 150:
self.grassdamage += 1
self.vel = 3
elif self.rect.x > 300 and self.rect.x < 500:
self.vel =3
self.grassdamage += 1
else:
self.vel = 5
def grasshealth(self):
if self.grassdamage == 100:
self.damage1 = True
elif self.grassdamage == 200:
self.damage2 = True
elif self.grassdamage == 300:
self.damage3 = True
elif self.grassdamage == 400:
self.damage4 = True
elif self.grassdamage == 500:
self.damage5 = True
elif self.grassdamage == 600:
self.damage6 = True
elif self.grassdamage == 600:
self.damage6 = True
elif self.grassdamage == 700:
self.damage7 = True
elif self.grassdamage == 800:
self.damage8 = True
self.dead = True
def death(self):
if self.dead == True:
exit()
#Text Setup
def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
#Drawing damage bar
def drawbar ():
if player.damage1 == True and car.damage1 == False or car.damage1 == True:
pygame.draw.rect(win, (GOLD), (50, 50, 25, 25))
if player.damage2 == True and car.damage1 == False or car.damage1 == True:
pygame.draw.rect(win, (GOLD), (50, 75, 25, 25))
if car.damage1 == True and player.grassdamage <= 200:
player.grassdamage = 201
if player.damage3 == True and car.damage2 == False or car.damage2 == True:
pygame.draw.rect(win, (GOLD), (50, 100, 25, 25))
if player.damage4 == True and car.damage2 == False or car.damage2 == True:
pygame.draw.rect(win, (GOLD), (50, 125, 25, 25))
if car.damage2 == True and player.grassdamage <= 400:
player.grassdamage = 401
if player.damage5 == True and car.damage3 == False or car.damage3 == True:
pygame.draw.rect(win, (GOLD), (50, 150, 25, 25))
if player.damage6 == True and car.damage3 == False or car.damage3 == True:
pygame.draw.rect(win, (GOLD), (50, 175, 25, 25))
if car.damage3 == True and player.grassdamage <= 600:
player.grassdamage = 601
if player.damage7 == True and car.damage4 == False or car.damage4 == True:
pygame.draw.rect(win, (GOLD), (50, 200, 25, 25))
if player.damage8 == True and car.damage4 == False or car.damage4 == True:
pygame.draw.rect(win, (GOLD), (50, 225, 25, 25))
raise SystemExit ('YOU LOST')
if car.damage4 == True and player.grassdamage <= 800:
player.grassdamage = 800
pygame.draw.rect(win, (0,0,0), (50, 50, 25, 200), 3)
Text = pygame.font.Font('freesansbold.ttf', 20)
TextSurf, TextRect = text_objects("Damage", Text)
TextRect.center = ((65), (30))
win.blit(TextSurf, TextRect)
class car():
def __init__(self):
self.color = random.sample(range(250), 3)
self.image = pygame.Surface([50, 100], pygame.SRCALPHA, 32)
pygame.draw.rect(self.image, (self.color), (0, 0, 50, 100))
self.rect = self.image.get_rect(topleft = (175, -100))
self.carvel = random.randrange(5, 10)
self.damage = 0
self.damage1 = False
self.damage2 = False
self.damage3 = False
self.damage4 = False
def draw(self, win):
#pygame.draw.rect(win,(self.color),self.rect)
win.blit(self.image, self.rect.topleft)
def move(self):
if self.rect.y < 530:
self.rect.y += self.carvel
else:
self.rect.y = -100
self.color = random.sample(range(250), 3)
self.carvel = random.randrange(8, 10)
def collision_check(self, another_object):
if self.rect.colliderect(another_object):
print('collison')
self.rect.y = -100
self.damage += 1
def cardamage(self):
if self.damage == 1:
self.damage1 = True
player.damage1 = True
player.damage2 = True
if self.damage == 2:
self.damage2 = True
player.damage3 = True
player.damage4 = True
if self.damage == 3:
self.damage3 = True
player.damage5 = True
player.damage6 = True
if self.damage == 4:
self.damage4 = True
player.damage7 = True
player.damage8 = True
#Putting variables to the classes
player = player()
car = car()
#Main drawing function
def redrawgamewindow():
win.blit(bkg, (0, 0))
pygame.draw.rect(win, (0, 0, 0), (150, 0, 200, 500))
pygame.draw.rect(win, (255, 255, 255), (x, (y), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y1), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y2), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y3), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y4), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y5), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y6), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y7), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y8), 10, 30))
player.draw(win)
car.draw(win)
player.grasshealth()
car.cardamage()
player.grass()
player.death()
car.collision_check(player.rect)
car.move()
drawbar()
pygame.display.update()
#MAINLOOP
run = True
while run:
#Making background and FPS
clock.tick(80)
#Quiting Funciton
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
#Scrolling the lines
if Lines:
y += player.vel
y1 += player.vel
y2 += player.vel
y3 += player.vel
y4 += player.vel
y5 += player.vel
y6 += player.vel
y7 += player.vel
y8 += player.vel
if Lines:
if y >= 500:
y = -40
if y1 >= 500:
y1 = -40
if y2 >= 500:
y2 = -40
if y3 >= 500:
y3 = -40
if y4 >= 500:
y4 = -40
if y5 >= 500:
y5 = -40
if y6 >= 500:
y6 = -40
if y7 >= 500:
y7 = -40
if y8 >= 500:
y8 = -40
#User input
keys = pygame.key.get_pressed()
#Boost controller
if keys[pygame.K_SPACE]:
start_time = time.time()
if player.rect.y > 200:
player.rect.y -= 9
Space = True
else:
player.moveback()
end_time = time.time()
#Left movement
if keys[pygame.K_LEFT] and player.rect.x > 150:
player.rect.x -= 5
if keys [pygame.K_LEFT] and player.rect.x <= 150:
if player.rect.x > 0:
player.rect.x -=5
#Right movement
if keys[pygame.K_RIGHT] and player.rect.x < 300:
player.rect.x += 5
if keys[pygame.K_RIGHT]and player.rect.x >= 300:
if player.rect.x < 500 - player.rect.width:
player.rect.x += 5
#Grass and grass damage
#MAIN DRAW RECALL
redrawgamewindow()
导入时间,pygame,随机
输入数学
pygame.init()
#第一变量
黑色=(0,0,0)
蓝色=(0,0255)
绿色=(100255100)
橙色=(255140,0)
黄色=(155、135、12)
白色=(255,255,255)
黄金=(255,215,0)
屏幕宽度=500
屏幕高度=500
行=真
空格=真
颜色=随机样本(范围(250),3)
多丽丝=[]
i=[]
x=247
y=-20
y1=40
y2=100
y3=160
y4=220
y5=280
y6=340
y7=400
y8=460
#显示和标题
win=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题('Lil cary')
clock=pygame.time.clock()
#加载图像
bkg=pygame.image.load('BG.jpg')
#实际玩家等级
类播放器():
定义初始化(自):
self.color=(12124134)
self.vel=5
self.grassdamage=0
self.image=pygame.Surface([50100],pygame.SRCALPHA,32)
pygame.draw.rect(self.image,(self.color),(0,0,50,100))
self.rect=self.image.get_rect(中心=(200390))
self.damage1=错误
self.damage2=错误
self.damage3=错误
self.damage4=错误
self.damage5=错误
self.damage6=错误
self.damage7=错误
self.damage8=错误
self.dead=False
def抽签(自我,赢):
#pygame.draw.rect(win,(self.color),(self.x,self.y,self.width,self.height))
win.blit(self.image,self.rect.topleft)
def回移(自):
如果self.rect.y<390且self.rect.y>=0:
自校正y+=10
def grass(自我):
如果self.rect.x>-10且self.rect.x<150:
自我伤害+=1
self.vel=3
如果self.rect.x>300且self.rect.x<500:
self.vel=3
自我伤害+=1
其他:
self.vel=5
健康(自我):
如果self.grassdamage==100:
self.damage1=真
elif self.grassdamage==200:
self.damage2=正确
elif self.grassdamage==300:
self.damage3=正确
elif self.grassdamage==400:
self.damage4=正确
elif self.grassdamage==500:
self.damage5=正确
elif self.grassdamage==600:
self.damage6=正确
elif self.grassdamage==600:
self.damage6=正确
elif self.grassdamage==700:
self.damage7=正确
elif self.grassdamage==800:
self.damage8=正确
self.dead=True
死亡(自我):
如果self.dead==True:
退出()
#文本设置
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
#拉拔损伤杆
def牵引杆():
如果player.damage1==真且car.damage1==假或car.damage1==真:
pygame.draw.rect(赢,(金),(50,50,25,25))
如果player.damage2==真且car.damage1==假或car.damage1==真:
pygame.draw.rect(赢,(金),(50,75,25,25))
如果car.damage1==真且player.grassdamage=500:
y3=-40
如果y4>=500:
y4=-40
如果y5>=500:
y5=-40
如果y6>=500:
y6=-40
如果y7>=500:
y7=-40
如果y8>=500:
y8=-40
#用户输入
keys=pygame.key.get_pressed()
#升压控制器
如果键[pygame.K_SPACE]:
开始时间=time.time()
如果player.rect.y>200:
player.rect.y-=9
空格=真
其他:
player.moveback()
结束时间=time.time()
#左移
如果键[pygame.K_LEFT]和player.rect.x>150:
player.rect.x-=5
如果键[pygame.K_LEFT]和player.rect.x 0:
player.rect.x-=5
#右转
如果键[pygame.K_RIGHT]和player.rect.x<300:
player.rect.x+=5
如果键[pygame.K_RIGHT]和player.rect.x>=300:
如果player.rect.x<500-player.rect.width:
player.rect.x+=5
#草与草害
#主抽签召回
重画游戏窗口()
您的代码几乎已经存在了。不要只创建一辆汽车
,而是创建一个汽车列表。如果汽车
被牵引、碰撞等,现在对列表中的每一辆汽车都这样做。在一些事件之后,比如说3秒钟过去,将另一辆车添加到列表中
例如:
car = Car()
car_list = [ car ] # start with one car
car_add_time = 0
class Car():
ROAD_LEFT = 150 # this is a rough guess on the correct spot
ROAD_RIGHT = 300
def __init__(self):
# ...
x_pos = random.randrange( Car.ROAD_LEFT, Car.ROAD_RIGHT )
self.rect = self.image.get_rect(topleft = ( x_pos, -100) )
现在很容易添加更多的汽车:
while run:
#Making background and FPS
clock.tick(80)
### Add another car after 3 seconds
time_now = pygame.time.get_ticks()
if ( time_now - car_add_time > 3000 ):
new_car = Car()
car_list.append( new_car )
car_add_time = time_now
重新绘制车辆列表:
def redrawgamewindow():
# ...
for car in car_list:
car.draw(win)
player.grasshealth()
for car in car_list:
car.cardamage()
player.grass()
player.death()
for car in car_list:
car.collision_check(player.rect)
car.move()
您需要做的另一件事是更改Car
类,以在随机x位置启动汽车(但仍在道路内(…或不在道路内))。由于当汽车碰撞或离开屏幕时,代码只是将其回收到-100
,因此也需要将其重新定位在此处
例如:
car = Car()
car_list = [ car ] # start with one car
car_add_time = 0
class Car():
ROAD_LEFT = 150 # this is a rough guess on the correct spot
ROAD_RIGHT = 300
def __init__(self):
# ...
x_pos = random.randrange( Car.ROAD_LEFT, Car.ROAD_RIGHT )
self.rect = self.image.get_rect(topleft = ( x_pos, -100) )
grassdamage
变量代表什么-对汽车的损坏?基本上,当它在草地上时,它会增加1,并且会继续增加,所以在100时,我会显示第一个栏。欢迎使用StackOverflow,而且。。。在这里申请。另见。不适合将整个开发过程都抛弃;笼统地问如何添加特定功能是不合适的。试试看;PyGame中有很多关于多个对象的可用教程。谢谢你,我以为这是列表中的一些东西,但不知道时间。谢谢