[英]I want player to dodge 2 rectangles but they appear parallel to each other
I am trying to make a car dodging game in pygame, I just learnt python and learning pygame . 我试图在pygame中制作汽车躲避游戏,我刚刚学习了python并学习了pygame。
I have made a function things which a called a times with different parameters of x axis to draw to rectangles as I want player to dodge 2 rects .But i cant figure out to delay the second one so that they doesnt not come at the same time. 我已经制作了一个函数,用x轴的不同参数调用一次以绘制到矩形,因为我希望玩家躲避2个rects。但我不能想出延迟第二个以便它们不会同时出现。
I want the second to be drawn after some delay, basically some difference in their y axis. 我希望第二个在延迟之后绘制,基本上在y轴上有一些差异。 attaching a image here and code 在这里附加图像和代码
import pygame
import time
import random
pygame.init()
display_width=600
display_height=800
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
brown=(165,42,42)
car_width=60
gameDisplay=pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Lets Race")
bg = pygame.image.load("road.png")
clock=pygame.time.Clock()
CarImg=pygame.image.load("Image.png")
pygame.mouse.set_visible(1)
def things_dodged(count):
font=pygame.font.SysFont(None,40)
text=font.render("Score "+str(count),True,black)
gameDisplay.blit(text,(20,20))
def car(x,y):
gameDisplay.blit(CarImg,(x,y))
def things(thingx,thingy,thingw,thingh,color):
color=brown
pygame.draw.rect(gameDisplay,color,[thingx,thingy,thingw,thingh])
def text_objects(text,font):
textSurface=font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText=pygame.font.Font('freesansbold.ttf',80)
TextSurf, TextRect=text_objects(text, largeText)
TextRect.center=((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('Game Over')
def game_loop():
x=(display_width*0.48)
y=(display_height*0.79)
x_change=0
thing_startx=random.randrange(0,display_width-100)
thing_startx2=random.randrange(0,display_width-100)
thing_starty=-600
thing_speed=9
thing_width=100
thing_height=100
dodged=0
gameExit=False
while not gameExit:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
x_change=-15
elif event.key==pygame.K_RIGHT:
x_change=15
if event.type==pygame.KEYUP:
if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
x_change=0
x+=x_change
#gameDisplay.fill(white)
gameDisplay.blit(bg, (0, 0))
#things(thingx,thingy,thingw,thingh,color)
things(thing_startx,thing_starty,thing_width,thing_height,black)
things(thing_startx2,thing_starty,thing_width,thing_height,black)
thing_starty+=thing_speed
car(x,y)
things_dodged(dodged)
if x>display_width-car_width or x<0:
crash()
if thing_starty>display_height:
thing_starty=0-thing_height
thing_startx=random.randrange(0,display_width-100)
dodged+=1
if thing_speed<15:
thing_speed+=0.15
if y<thing_starty+thing_height:
pass
if x>thing_startx and x<thing_startx+thing_width or x+car_width>thing_startx and x+car_width<thing_startx+thing_width:
crash()
pygame.display.update()
clock.tick(100)
game_loop()
pygame.quit()
quit()
The 2 rectangles have 2 different x coordinates ( thing_startx
, thing_startx2
). 2个矩形有2个不同的x坐标( thing_startx
, thing_startx2
)。 They've to have 2 different y coordinates, too. 它们也有2个不同的y坐标。
Create a list of "things", which is basically a list of positions (x and y coordinates): 创建一个“事物”列表,它基本上是一个位置列表(x和y坐标):
thing_list = [
[random.randrange(0,display_width-100), -600],
[random.randrange(0,display_width-100), -300]]
thing_speed=9
thing_width=100
thing_height=100
Operate on the list of things in the main loop. 在主循环中的事物列表上操作。
Further I recommend to use pygame.Rect.colliderect()
to chek for collisions. 此外,我建议使用pygame.Rect.colliderect()
来解决冲突。
while not gameExit:
# [...]
for thing in thing_list:
things(*thing, thing_width, thing_height, black)
thing[1] += thing_speed
car(x,y)
things_dodged(dodged)
if x>display_width-car_width or x<0:
crash()
carRect = pygame.Rect(x, y, car_width, 1)
for thing in thing_list:
if thing[1] > display_height:
thing[0] = random.randrange(0,display_width-100)
thing[1] = 0-thing_height;
dodged += 1
thing_speed = min(thing_speed+0.15, 15)
if carRect.colliderect(pygame.Rect(*thing, thing_width, thing_height)):
crash()
pygame.display.update()
clock.tick(100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.