Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 砖块游戏从墙上弹下来_Python_Pygame - Fatal编程技术网

Python 砖块游戏从墙上弹下来

Python 砖块游戏从墙上弹下来,python,pygame,Python,Pygame,我在课堂上有一个巨大的作业,我迷路了,请求上帝饶恕我迷路。我正在做一个砖块游戏,最基本的功能是让球从墙上弹起。我搞不懂,我的老师太忙了,同学们都很争强好胜,我不想让你为我写代码,我只是不知道如何编码。我不断地出错,最近几节课都很糟糕 import math import pygame import random pygame.init() screen = pygame.display.set_mode([800,600])

我在课堂上有一个巨大的作业,我迷路了,请求上帝饶恕我迷路。我正在做一个砖块游戏,最基本的功能是让球从墙上弹起。我搞不懂,我的老师太忙了,同学们都很争强好胜,我不想让你为我写代码,我只是不知道如何编码。我不断地出错,最近几节课都很糟糕

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()
导入数学
导入pygame
随机输入
pygame.init()
screen=pygame.display.set_模式([800600])
完成=错误
clock=pygame.time.clock()
#定义颜色
白色=(255255)
蓝色=(0102204)
浅蓝色=(0,0204)
粉红色=(238130238)
#导入图像
#lives=pygame.image.load(“heart.png”)
#初始化字体;必须在“pygame.init()”之后调用,以避免“字体未初始化”错误
myfont=pygame.font.SysFont(“monospace”,15)
#我在游戏的空间里穿梭
top=-100
左=-50
右=-750
底部=-600
角度=数学弧度(random.randint(-140,-30))
类游戏屏幕():
def draw_屏幕():
pygame.draw.rect(屏幕,蓝色,(50100700600))
def保存按钮():
pygame.draw.rect(屏幕,粉红色,(500,20,50,30),0)
save=“保存”
label=myfont.render(保存,40,(0,0,0))
屏幕光点(标签,(505,20))
def quit_按钮():
pygame.draw.rect(屏幕,粉红色,(600,20,50,30),0)
quit1=“退出”
label=myfont.render(quit1,40,(0,0,0))
筛网布利特(标签,(605,20))
mX,mY=pygame.mouse.get_pos()
mouseButtons=pygame.mouse.get_pressed()
如果鼠标按钮[0]==True:
如果(mX在范围内(600-20600+20)和mY在范围内(20-20,20+20)):
pygame.quit()
#def display_lives():
#生命计数器=screen.blit(生命,(50,30))
#生命计数器2=屏幕.blit(生命,(120,30))
#lives_counter3=screen.blit(lives,(190,30))
班级桨:
定义初始化(self、xpo、ypo):
self.x=xpos
self.y=ypos
def牵引(自身):#牵引桨叶
pygame.draw.rect(屏幕,粉色,(self.x,self.y,70,20))
def移动(自我):
keys=pygame.key.get_pressed()#检查按下的键
如果键[pygame.K_左]:
如果self.x=680:
self.x=680
其他:
自身x+=10
班级舞会:
def u u初始__;(自身,桨1):
self.x=(桨1.x+35)
self.y=(桨1.y-5)
自身速度=0
self.speedX=0
self.speeded=0
自我定向=200
def牵引(自):
pygame.draw.circle(屏幕,白色,(self.x,self.y),10)
def反弹(自我):
自我方向=(180-自我方向)%360
def移动球(自身):
keys=pygame.key.get_pressed()#检查按下的键
球拍上的球=正确
如果球拍上的球=真:
self.x=(桨1.x+35)
self.y=(桨1.y-5)
自身速度=0
如果键[pygame.K_UP]==True:
球拍上的球=错误
打印(“a”)
自身速度=10
self.speedX+=int(数学cos(角度)*self.speed)
self.speed+=int(数学sin(角度)*self.speed)
打印(底部)
打印(自加速)

如果self.y可以通过比较球坐标和墙坐标来检测球和墙之间的碰撞。反射球意味着speedX或speedY(取决于墙)与-1相乘。

最简单的方法是在类球中创建一个函数反弹(self)

首先,您决定要触摸的边界

然后,根据反弹的边界更改球的角度
如果在360度系统中工作,则会镜像角度:
  • 当球从底部和顶部边界反弹时,围绕垂直轴旋转。所以角度=180度角
  • 当球从侧边反弹时,围绕水平轴旋转。所以角度=-角度

最后,你必须把球移回它跑得太远的地方

正如你所看到的,球在1次迭代中从C1移动到C3',而我们希望它移动到C3。我们可以通过围绕轴边界反射它来实现这一点
球的新x坐标变为x=边界×球×大小-(x-边界×球×+球×大小)=2*(边界×球×大小)-x



当我们将其转换为代码时,我们得到: 班级舞会: def反弹(自我): 如果self.x显示宽度-球大小: self.angle=math.pi-self.angle self.x=2*(显示宽度-球大小)-self.x

如果self.y显示高度-球大小:
self.angle=-self.angle
self.y=2*(显示高度-球大小)-self.y

您遇到了哪些错误?你到底期望它做什么?没有任何附加信息,我们无法真正帮助您。@radimpe它是一个砖块破碎机,应该会从墙上弹下来
    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