import pygame
pygame.init()
red = 255,0,0
blue = 0,0,255
black = 0,0,0
screenWidth = 800
screenHeight = 600
gameDisplay = pygame.display.set_mode((screenWidth,screenHeight)) ## screen width and height
pygame.display.set_caption('JUST SOME BLOCKS') ## set my title of the window
clock = pygame.time.Clock()
class player(): ## has all of my attributes for player 1
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.height = height
self.width = width
self.vel = 5
self.left = False
self.right = False
self.up = False
self.down = False
class projectile(): ## projectile attributes
def __init__(self,x,y,radius,colour,facing):
self.x = x
self.y = y
self.radius = radius
self.facing = facing
self.colour = colour
self.vel = 8 * facing # speed of bullet * the direction (-1 or 1)
def draw(self,gameDisplay):
pygame.draw.circle(gameDisplay, self.colour , (self.x,self.y),self.radius) ## put a 1 after that to make it so the circle is just an outline
def redrawGameWindow():
for bullet in bullets: ## draw bullets
bullet.draw(gameDisplay)
pygame.display.update()
#mainloop
player1 = player(300,410,50,70) # moves the stuff from the class (when variables are user use player1.var)
bullets = []
run = True
while run == True:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.x < screenWidth and bullet.x > 0 and bullet.y < screenHeight and bullet.y > 0: ## makes sure bullet does not go off screen
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed() ## check if a key has been pressed
## red player movement
if keys[pygame.K_w] and player1.y > player1.vel: ## check if that key has been pressed down (this will check for w) and checks for boundry
player1.y -= player1.vel ## move the shape in a direction
player1.up = True
player1.down = False
if keys[pygame.K_a] and player1.x > player1.vel: ### this is for a
player1.x -= player1.vel
player1.left = True
player1.right = False
if keys[pygame.K_s] and player1.y < screenHeight - player1.height - player1.vel: ## this is for s
player1.y += player1.vel
player1.down = True
player1.up = False
if keys[pygame.K_d] and player1.x < screenWidth - player1.width - player1.vel: ## this is for d
player1.x += player1.vel
player1.right = True
player1.left = False
if keys[pygame.K_SPACE]: # shooting with the space bar
if player1.left == True: ## handles the direction of the bullet
facing = -1
else:
facing = 1
if len(bullets) < 5: ## max amounts of bullets on screen
bullets.append(projectile(player1.x + player1.width //2 ,player1.y + player1.height//2,6,black,facing)) ##just like calling upon a function
## level
gameDisplay.fill((0,255,0)) ### will stop the shape from spreading around and will have a background
pygame.draw.rect(gameDisplay,(red),(player1.x,player1.y,player1.width,player1.height)) ## draw player
pygame.display.update()
redrawGameWindow()
pygame.quit()
当我射出超过1发子弹时,我一次只想射出1发子弹(但不只是屏幕上的1发子弹),它们都成一个大团,粘在一起,所以我希望它们在不同的时间射出,我尝试过使用延迟时钟。勾选
,但这会让游戏变得非常滞后
我是pygame的新手,还不完全了解它。如果有任何帮助,将不胜感激。谢谢!
发射子弹的一般方法是将子弹的位置存储在列表中(bullet_list
)。当子弹发射时,将子弹的起始位置([start_x,start_y]
)添加到列表中。起始位置是发射子弹的物体(玩家或敌人)的位置。使用for
-循环遍历列表中的所有项目符号。在循环中移动每个子弹的位置。从离开屏幕的列表中删除一个项目符号(bullet_list.remove(bullet_pos)
)。因此,必须遍历列表(bullet_list[:]
)的副本(请参阅如何在迭代时从列表中删除项?)。使用另一个for
-循环到屏幕上剩余的项目符号:
bullet_list = []
while run == True:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet_list.append([start_x, start_y])
for bullet_pos in bullet_list[:]:
bullet_pos[0] += move_bullet_x
bullet_pos[1] += move_bullet_y
if not screen.get_rect().colliderect(bullet_image.get_rect(center = bullet_pos))
bullet_list.remove(bullet_pos)
# [...]
for bullet_pos in bullet_list[:]
screen.blit(bullet_image, bullet_image.get_rect(center = bullet_pos))
# [...]
另见射击子弹。
只要按住一个键,由pygame.key.get_pressed()
返回的状态就会被设置。这对玩家的移动很有用。只要按下一个键,玩家就会继续移动。
但是当你想发射子弹时,这与你的意图相矛盾。如果您想在按键按下时发射子弹,那么可以使用KEYDOWN
事件。该事件仅在按下某个键时发生一次:
while run == True:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if player1.left == True: ## handles the direction of the bullet
facing = -1
else:
facing = 1
if len(bullets) < 5: ## max amounts of bullets on screen
bx, by = player1.x + player1.width //2 ,player1.y + player1.height//2
bullets.append(projectile(bx, by, 6, black, facing))
# [...]
如果你想实施某种快速射击,事情就会变得更加棘手。如果您想使用pygame的状态。钥匙按下按钮()
,则会在每一帧中生成一个项目符号。那太快了。您必须实现一些超时
当子弹发射时,玩家通过pygame获取当前时间。时间获取刻度()
。定义到项目符号之间的延迟毫秒数。将dela添加到时间中,并在变量中说明时间(next\u bullet\u threshold
)。跳过项目符号,只要不超过时间:
next_bullet_threshold = 0
run = True
while run == True:
# [...]
current_time = pygame.time.get_ticks()
if keys[pygame.K_SPACE] and current_time > next_bullet_threshold:
bullet_delay = 500 # 500 milliseconds (0.5 seconds)
next_bullet_threshold = current_time + bullet_delay
if player1.left == True: ## handles the direction of the bullet
facing = -1
else:
facing = 1
if len(bullets) < 5:
bx, by = player1.x + player1.width //2 ,player1.y + player1.height//2
bullets.append(projectile(bx, by, 6, black, facing))
import pygame
pygame.init()
window = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()
tank_surf = pygame.Surface((60, 40), pygame.SRCALPHA)
pygame.draw.rect(tank_surf, (0, 96, 0), (0, 00, 50, 40))
pygame.draw.rect(tank_surf, (0, 128, 0), (10, 10, 30, 20))
pygame.draw.rect(tank_surf, (32, 32, 96), (20, 16, 40, 8))
tank_rect = tank_surf.get_rect(midleft = (20, window.get_height() // 2))
bullet_surf = pygame.Surface((10, 10), pygame.SRCALPHA)
pygame.draw.circle(bullet_surf, (64, 64, 62), bullet_surf.get_rect().center, bullet_surf.get_width() // 2)
bullet_list = []
max_bullets = 4
next_bullet_time = 0
bullet_delta_time = 200 # milliseconds
run = True
while run:
clock.tick(60)
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if len(bullet_list) < max_bullets and current_time >= next_bullet_time:
next_bullet_time = current_time + bullet_delta_time
bullet_list.insert(0, tank_rect.midright)
for i, bullet_pos in enumerate(bullet_list):
bullet_list[i] = bullet_pos[0] + 5, bullet_pos[1]
if bullet_surf.get_rect(center = bullet_pos).left > window.get_width():
del bullet_list[i:]
break
window.fill((224, 192, 160))
window.blit(tank_surf, tank_rect)
for bullet_pos in bullet_list:
window.blit(bullet_surf, bullet_surf.get_rect(center = bullet_pos))
pygame.display.flip()
pygame.quit()
exit()
我在我的应用程序中使用MutableLiveData进行基于事件的通信。我有单一活动两个片段架构。 在ViewModel的帮助下,我正在使用Fragment-1中的LiveData事件。但是,当我使用菜单栏用Fragment-2替换这个Fragment-1并最终返回Fragment-1时,LiveData的旧值再次被捕获。 如何避免这个问题?非常感谢任何帮助/建议!谢谢你。
当我调用也会被销毁,这是我不希望的。您可能建议为添加属性,但这不是我想要的。 提前谢了。
所以我在建模一条生产线(很简单,有5个流程,我将其建模为服务)。我模拟了一个月,在这一个月里,我的生产线大约停了50次(由于机器故障)。该停车可以持续3到60分钟,平均值=12分钟(取决于三角形概率)。我如何在模型中实现这一点?我正在尝试创建一个事件,但无法确定应该使用哪种类型的触发器。
问题内容: 我创建了一个电子邮件队列数据库表。我将把我的PHP应用程序需要发送的所有电子邮件插入此表。 然后,另一个PHP脚本将查找所有未发送的电子邮件并将其发送。 我使用cronjobs运行此脚本。不幸的是,cronjobs每分钟最多只能运行一次。因此,在最坏的情况下,用户必须等待一分钟,直到真正发送他的电子邮件为止。 我目前的解决方法是,使用附加的sleep参数调用脚本并复制cronjobs。
当且仅当相同的项目在最后x毫秒内发射时,我想防止发射发生。我已经查看了油门和防抖操作符,但我不确定它们是否可以在这里帮助我。我可以使用其他操作符吗,或者我可以以某种方式组合它们吗?
问题内容: 我想知道阻止一次性电子邮件地址在我的网站上注册的可能方法。 为简单起见,让我们以使用HTML和PHP完成网站注册表单的示例为例。 任何想法,解决方案或建议将不胜感激。 问题答案: 这很困难,因为白名单和黑名单都不是可选项。 通过将某些域列入白名单,您可以禁止具有您不知道(但可能完全有效)电子邮件域的人,而通过将其列入黑名单,则必须每天更新列入黑名单的域的列表,因为新的“ 10分钟电子邮
我尝试从这个问题设置服务器PHP Ubuntu使用gmail表单发送电子邮件localhost。所以我设置了(帐户)从: /etc/MSMTPC: 现在,当我尝试发送测试电子邮件(以root用户身份执行)时: 给出: 所以日志上写着<代码>
问题内容: 我有一个yeoman脚手架应用程序(全栈角度生成器)。 可以正常工作,但是会产生锁定内存的分布,这很可能是因为角度中的圆形引用。 我将angular升级到了。我得到的错误是: 升级之前,错误为: 调试非常困难,因为它仅在构建/缩小之后才发生。我所有的模块都是angular的数组格式,因此最小化DI应该不是问题,而是这样。 没有单个脚本会导致这种情况。它唯一消失的方法是,如果我不使用我的