PythonPyGame:如果球击中墙壁的次数和输入()值相等,如何退出 导入pygame 导入系统 pygame.init() screen=pygame.display.set_模式([640480]) 屏幕填充([255255]) #具有每像素alpha的透明曲面。 圆=pygame.Surface((60,60),pygame.SRCALPHA) #在“圆”曲面上绘制一个圆。 pygame.draw.circle(圆圈[255,0,0],[30,30],30) x=10 y=10 x_速度=5 y_速度=5 完成=错误 虽然没有这样做: 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: 完成=正确 pygame。时间。延迟(20) 屏幕填充((40,40,40)) x=x+x_速度 y=y+y\U速度 如果x>screen.get_width()-30或x屏幕。获取高度()-30或yscreen.get_width()-30或x屏幕。获取高度()-30或y=最大墙命中率: sys.exit() #现在浮出水面。 屏幕光点(圆形,[x,y]) pygame.display.flip() pygame.quit() import pygame import sys pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill([255,255,255]) # A transparent surface with per-pixel alpha. circle = pygame.Surface((60, 60), pygame.SRCALPHA) # Draw a circle onto the `circle` surface. pygame.draw.circle(circle, [255,0,0], [30, 30], 30) x = 10 y = 10 x_speed = 5 y_speed = 5 wall_hit = 0 max_wall_hit = 5 done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.time.delay(20) screen.fill((40, 40, 40)) x = x + x_speed y = y + y_speed if x > screen.get_width() - 30 or x < 0: x_speed = -x_speed wall_hit += 1 if y > screen.get_height() - 30 or y < 0: y_speed = -y_speed wall_hit += 1 if wall_hit >= max_wall_hit: sys.exit() # Now blit the surface. screen.blit(circle, [x, y]) pygame.display.flip() pygame.quit()
让我知道如何:PythonPyGame:如果球击中墙壁的次数和输入()值相等,如何退出 导入pygame 导入系统 pygame.init() screen=pygame.display.set_模式([640480]) 屏幕填充([255255]) #具有每像素alpha的透明曲面。 圆=pygame.Surface((60,60),pygame.SRCALPHA) #在“圆”曲面上绘制一个圆。 pygame.draw.circle(圆圈[255,0,0],[30,30],30) x=10 y=10 x_速度=5 y_速度=5 完成=错误 虽然没有这样做: 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: 完成=正确 pygame。时间。延迟(20) 屏幕填充((40,40,40)) x=x+x_速度 y=y+y\U速度 如果x>screen.get_width()-30或x屏幕。获取高度()-30或yscreen.get_width()-30或x屏幕。获取高度()-30或y=最大墙命中率: sys.exit() #现在浮出水面。 屏幕光点(圆形,[x,y]) pygame.display.flip() pygame.quit() import pygame import sys pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill([255,255,255]) # A transparent surface with per-pixel alpha. circle = pygame.Surface((60, 60), pygame.SRCALPHA) # Draw a circle onto the `circle` surface. pygame.draw.circle(circle, [255,0,0], [30, 30], 30) x = 10 y = 10 x_speed = 5 y_speed = 5 wall_hit = 0 max_wall_hit = 5 done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.time.delay(20) screen.fill((40, 40, 40)) x = x + x_speed y = y + y_speed if x > screen.get_width() - 30 or x < 0: x_speed = -x_speed wall_hit += 1 if y > screen.get_height() - 30 or y < 0: y_speed = -y_speed wall_hit += 1 if wall_hit >= max_wall_hit: sys.exit() # Now blit the surface. screen.blit(circle, [x, y]) pygame.display.flip() pygame.quit(),python,python-3.x,pygame,Python,Python 3.x,Pygame,让我知道如何: 求一个球和墙碰撞的次数 指定球可以撞击墙壁的最大次数,并在球撞击墙壁的次数等于最大值时退出 声明两个新变量,wall\u hit=0,用于计算球击中墙壁的次数,max\u wall\u hit=5,用于计算球在退出前可以击中墙壁的最大次数。在我的例子中,它会在撞击墙壁5次后退出 现在,每次球击中墙壁时,你已经改变了速度方向,我只是添加了一条线,将计数器wall\u hit增加一,然后将其与max\u wall\u hit的限制进行比较。一旦wall\u hit达到最大值,只需调用
声明两个新变量,
wall\u hit=0
,用于计算球击中墙壁的次数,max\u wall\u hit=5
,用于计算球在退出前可以击中墙壁的最大次数。在我的例子中,它会在撞击墙壁5次后退出
现在,每次球击中墙壁时,你已经改变了速度方向,我只是添加了一条线,将计数器wall\u hit
增加一,然后将其与max\u wall\u hit
的限制进行比较。一旦wall\u hit
达到最大值,只需调用sys.exit()
以下是完整的代码:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
# A transparent surface with per-pixel alpha.
circle = pygame.Surface((60, 60), pygame.SRCALPHA)
# Draw a circle onto the `circle` surface.
pygame.draw.circle(circle, [255,0,0], [30, 30], 30)
x = 10
y = 10
x_speed = 5
y_speed = 5
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.time.delay(20)
screen.fill((40, 40, 40))
x = x + x_speed
y = y + y_speed
if x > screen.get_width() - 30 or x < 0:
x_speed = -x_speed
if y > screen.get_height() - 30 or y < 0:
y_speed = -y_speed
# Now blit the surface.
screen.blit(circle, [x, y])
pygame.display.flip()
pygame.quit()
导入pygame
导入系统
pygame.init()
screen=pygame.display.set_模式([640480])
屏幕填充([255255])
#具有每像素alpha的透明曲面。
圆=pygame.Surface((60,60),pygame.SRCALPHA)
#在“圆”曲面上绘制一个圆。
pygame.draw.circle(圆圈[255,0,0],[30,30],30)
x=10
y=10
x_速度=5
y_速度=5
wall_hit=0
最大墙击数=5
完成=错误
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
pygame。时间。延迟(20)
屏幕填充((40,40,40))
x=x+x_速度
y=y+y\U速度
如果x>screen.get_width()-30或x<0:
x_速度=-x_速度
墙击数+=1
如果y>屏幕。获取高度()-30或y<0:
y_速度=-y_速度
墙击数+=1
如果墙命中率>=最大墙命中率:
sys.exit()
#现在浮出水面。
屏幕光点(圆形,[x,y])
pygame.display.flip()
pygame.quit()
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
# A transparent surface with per-pixel alpha.
circle = pygame.Surface((60, 60), pygame.SRCALPHA)
# Draw a circle onto the `circle` surface.
pygame.draw.circle(circle, [255,0,0], [30, 30], 30)
x = 10
y = 10
x_speed = 5
y_speed = 5
wall_hit = 0
max_wall_hit = 5
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.time.delay(20)
screen.fill((40, 40, 40))
x = x + x_speed
y = y + y_speed
if x > screen.get_width() - 30 or x < 0:
x_speed = -x_speed
wall_hit += 1
if y > screen.get_height() - 30 or y < 0:
y_speed = -y_speed
wall_hit += 1
if wall_hit >= max_wall_hit:
sys.exit()
# Now blit the surface.
screen.blit(circle, [x, y])
pygame.display.flip()
pygame.quit()