Pygame打破代码,创造一堵砖墙

时间:2017-06-01 06:05:46

标签: pygame game-development

这是我第一次使用pygame而且我为可能的误解和'愚蠢'问题道歉。我正在开发一款破球游戏,包括一个球,一个球拍和一堵砖墙。到目前为止我还有球,那很好。我试图实现我的代码以获得墙,但我错过了一些东西,当我运行它时它不会出现在屏幕上。谁能帮助我?砖块是50x60,3排。先感谢您。 (如果有人也会建议一些关于创建球拍并使球棒粘在上面的东西,那将非常感激。) 到目前为止,这是我的代码:

  """"BREAKING GAME"""
   #Import and initialize pygame
   import pygame as pg

   pg.init()

   #Set the colour of the background and store the ball data
   backgroundcolour = (50,50,50)
   randomcolour=(205,55,0)
   ballimg = pg.image.load("ball.gif")
   ballimage = ballimg.get_rect()


   #Set the frame
   xmax = 800
   ymax = 800
   screen = pg.display.set_mode((xmax,ymax))

   #Starting values

   horizontalposition = 400.     #pixels (unit f length of computer)
   verticalposition = 400.       #pixels
   v_x = 400.                    #pixels
   v_y = 300.                    #pixels

   #Set the clock of the computer
   t0 = float(pg.time.get_ticks())/1000.


   # Create wall of bricks
   position_Bricks_x=[0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750]



   position_Bricks_y = [16,32,48]


    i = 0
    j = 0






    #Infinite loop
    running = True
    while running:
        t = float(pg.time.get_ticks())/1000.
        dt = min(t-t0, 0.1)
        t0 = t

             # Motion of the ball
        horizontalposition = horizontalposition+v_x*dt
        verticalposition = verticalposition+v_y*dt

        #Bounce the ball on the edges of the screen
        if horizontalposition > xmax:
            v_x=-abs(v_x)
        elif horizontalposition < 0:
            v_x= abs(v_x)
       if verticalposition > ymax:
            v_y = -abs(v_y)
       elif verticalposition<0:
            v_y = abs(v_y)

     # Draw the frame
       screen.fill(backgroundcolour)
       ballimage.centerx = int(horizontalposition)
       ballimage.centery = int(verticalposition)

       screen.blit(ballimg,ballimage)

       while i < len(position_Bricks_x):
             if j < len(position_Bricks_y):

        pg.draw.rect(screen,randomcolour,[position_Bricks_x[i],position_Bricks_y[j],50,16])
        j = j + 1
    else:
        j=0
        i=i+1
pg.display.update()
pg.display.flip()
# Event handling
pg.event.pump()
for event in pg.event.get():
    if event.type == pg.QUIT:
        running = False

退出pygame

pg.quit() 打印“准备好”

0 个答案:

没有答案