我一直试图在类似躲避者的游戏上做出改变,其中一些块会让你成长,而另一些会让你缩小。我还计划添加一个红色的应该让你失去生命(你将有3个生命开始),以及其他各种具有自己属性的生命。然而,我已经在路上碰到了下降的积木随机产生,这是在广泛的游戏中需要的东西。
我的计划基本上是我希望每次在随机位置重新生成块,并且在某些时候我想要增加下降块的数量以进一步增加难度。
这是我目前的进展。任何输入都非常感谢:
import pygame
import random
pygame.init()
win_width = 800
win_height = 600
window = pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("Roger Dodger")
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
orange = (255,127,0)
yellow = (255,255,0)
blue = (0,0,255)
clock = pygame.time.Clock()
class Collisions:
def __init__(self, x1,y1,w1,h1,x2,y2,w2,h2):
self.x1 = x1
self.y1 = y1
self.w1 = w1
self.h1 = h1
self.x2 = x2
self.y2 = y2
self.w2 = w2
self.h2 = h2
def checkCol(self):
if (self.x2 + self.w2 >= self.x1 >= self.x2 and self.y2 + self.h2 >= self.y1 >= self.y2):
return True
elif (self.x2 + self.w2 >= self.x1 + self.w1 >= self.x2 and self.y2 + self.h2 >= self.y1 >= self.y2):
return True
elif (self.x2 + self.w2 >= self.x1 >= self.x2 and self.y2 + self.h2 >= self.y1 + self.h1 >= self.y2):
return True
elif (self.x2 + self.w2 >= self.x1 + self.w1 >= self.x2 and self.y2 + self.h2 >= self.y1 + self.h1 >= self.y2):
return True
else:
return False
def yel_col(self):
if Collisions.checkCol(self):
return True
def ora_col(self):
if Collisions.checkCol(self):
return True
class Sprite:
def __init__(self,x,y,width,height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
def render(self,):
pygame.draw.rect(window,self.color,(self.x,self.y,self.width,self.height))
Sprite1=Sprite(win_width/2 ,win_height-60,30,30, blue)
moveX = 0
sprite2x = random.randrange(30, win_width, 30)
sprite3x = random.randrange(30, win_width, 30)
falling_pos = 0
gameLoop=True
while gameLoop:
for event in pygame.event.get():
if (event.type==pygame.QUIT):
gameLoop=False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
moveX = -3
if (event.key==pygame.K_RIGHT):
moveX = 3
window.fill(white)
ground = pygame.draw.rect(window, black, ((0, win_height-30), (win_width, 30)))
Sprite1.x+=moveX
falling_pos += 3
Sprite2=Sprite(sprite2x,falling_pos,30,30, orange)
Sprite3=Sprite(sprite3x, falling_pos, 30, 30, yellow)
collisions1=Collisions(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,Sprite2.x,Sprite2.y,Sprite2.width,Sprite2.height)
collisions2=Collisions(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,Sprite3.x,Sprite3.y,Sprite3.width,Sprite3.height)
Sprite1.render()
Sprite2.render()
Sprite3.render()
if collisions2.checkCol() and collisions2.yel_col():
if Sprite1.width and Sprite1.height > 30:
Sprite1.width -= 5
Sprite1.height -= 5
Sprite1.y += 5
if collisions1.checkCol() and collisions1.ora_col():
if Sprite1.width and Sprite1.height < 300:
Sprite1.width += 5
Sprite1.height += 5
Sprite1.y -= 5
if Sprite1.x < 0:
Sprite1.x = win_width
elif Sprite1.x > win_width:
Sprite1.x = 0
if falling_pos > win_height:
falling_pos = 0
pygame.display.flip()
clock.tick(120)
pygame.quit()
答案 0 :(得分:0)
要使它们在随机位置生成,请使用random.randint(a, b)
指定起始位置。