砖头游戏从墙上反弹

时间:2017-05-24 15:31:43

标签: python pygame

我在课堂上有一份很大的作业,我迷失了,要求上帝让我失去。我正在制作一个砖头游戏和最基本的功能,让球弹回墙壁,我无法弄清楚,我的老师太忙,同学们有竞争力,我不想让你为我编写代码,我只是不知道如何编码。我一直在收到错误,而且最后几堂课一直都是地狱。

import math
import pygame
import random
pygame.init()                                   
screen = pygame.display.set_mode([800,600])     
done = False                                    
clock = pygame.time.Clock()
#DEFINE COLORS
WHITE = (255,255,255)                          
BLUE=(0,102,204)
LIGHT_BLUE=(0,0,204)
pink=(238,130,238)
#import images
#lives = pygame.image.load("heart.png")
# initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("monospace", 15)
#var carying the space of the game
top = -100
left= -50
right= -750
bottom= -600
angle = math.radians(random.randint(-140,-30))

class game_screen():
    def draw_screen():
        pygame.draw.rect(screen,BLUE,(50,100,700,600))
    def save_button():
        pygame.draw.rect(screen,pink,(500,20,50,30),0)
        save = "Save"
        label = myfont.render(save, 40, (0,0,0))
        screen.blit(label, (505, 20))
    def quit_button():
        pygame.draw.rect(screen,pink,(600,20,50,30),0)
        quit1 = "Quit"
        label = myfont.render(quit1, 40, (0,0,0))
        screen.blit(label, (605, 20))

        mX, mY = pygame.mouse.get_pos()
        mouseButtons = pygame.mouse.get_pressed()
        if mouseButtons[0] == True:
            if (mX in range (600-20,600+20) and mY in range (20-20,20+20)):
                pygame.quit()
    #def display_lives():
        #lives_counter = screen.blit(lives,(50,30))

        #lives_counter2 = screen.blit(lives,(120,30))

        #lives_counter3 = screen.blit(lives,(190,30))

class PADDLE:
    def __init__(self,xpos,ypos):
        self.x = xpos
        self.y = ypos

    def draw(self): # draws paddle
        pygame.draw.rect(screen,pink,(self.x,self.y,70,20))


    def move(self):
        keys = pygame.key.get_pressed()  #checking pressed keys
        if keys[pygame.K_LEFT]:
            if self.x<=50:
                self.x=50
            else:
                self.x -= 10
        elif keys[pygame.K_RIGHT]:
            if self.x >=680:
                self.x = 680
            else:
                self.x += 10

class BALL:
    def __init__(self,paddle1):
        self.x = (paddle1.x+35)
        self.y = (paddle1.y-5)
        self.speed = 0
        self.speedX = 0
        self.speedY = 0
        self.direction = 200

    def draw(self):
        pygame.draw.circle(screen,WHITE,(self.x,self.y),10)

    def bounce(self):
        self.direction = (180 - self.direction) % 360

    def move_ball(self):
        keys = pygame.key.get_pressed()  #checking pressed keys
        ball_on_paddle = True
        if ball_on_paddle == True :
            self.x = (paddle1.x+35)
            self.y = (paddle1.y-5)
            self.speed = 0

        if keys[pygame.K_UP] == True:
            ball_on_paddle = False
            print("a")
            self.speed = 10
            self.speedX += int(math.cos(angle)* self.speed)
            self.speedY += int(math.sin(angle)* self.speed)
            print(bottom)
            print(self.speedY)
            if self.y <= 0:
                self.bounce(0)
                self.y = 1

            if self.x <= 0:
                self.direction = (360 - self.direction) % 360
                self.x = 1

            if self.x > self.screenwidth - self.width:
                self.direction = (360 - self.direction) % 360
                self.x = self.screenwidth - self.width - 1


paddle1 = PADDLE(350,550)      
ball1 = BALL(paddle1)

# MAIN LOOP
while not done:
    screen.fill((LIGHT_BLUE))
    game_screen.draw_screen()
    game_screen.save_button()
    game_screen.quit_button()
    paddle1.draw()
    paddle1.move()
    ball1.draw()
    ball1.move_ball()
    ball1.bounce()




    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.display.flip()                      
    clock.tick(60)                              

pygame.quit()

2 个答案:

答案 0 :(得分:1)

通过将球的坐标与墙壁的坐标进行比较,可以检测球与墙之间的碰撞。反射球意味着speedX或speedY的倍增 - 取决于墙壁 - 为-1。

答案 1 :(得分:0)

最简单的方法是在BALL类中创建一个函数bounce(self)。

首先,您确定要触摸哪个边框

然后,根据反弹的边界来更改球的角度
如果您在360度系统中工作,则该角度是镜像的:

    当球从底部和顶部边界反弹时,围绕垂直轴
  • 。因此角度= 180-角度。
  • 当球反弹到侧边框时,
  • 围绕水平轴。所以 angle =-angle

最后,您必须将球移回它已经移动了太多。The ball moves in 1 iteration from C1 to C3'

就像您看到的那样,球从C1到C3'进行了1次迭代,而我们希望它到达C3。我们可以通过围绕BOUNDARY_X-ball_size反射它来实现。
球的新x坐标变为 x = BOUNDARY_X-ball_size-(x-BOUNDARY_X + ball_size)= 2 *(BOUNDARY_X-ball_size)-x



将其转换为代码时,我们得到:     球类:         def反弹(自己):             如果self.x DISPLAY_WIDTH-ball_size:             self.angle = math.pi-self.angle             self.x = 2 *(DISPLAY_WIDTH-ball_size)-self.x

    if self.y < ball_size:
        self.angle = - self.angle
        self.y = 2*ball_size - self.y
    elif self.y > DISPLAY_HEIGHT - ball_size:
        self.angle = - self.angle
        self.y = 2*(DISPLAY_HEIGHT - ball_size) - self.y
相关问题