如何用太空棒射击子弹?
这是我的代码:
import pygame, os
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
win = pygame.display
d = win.set_mode((1200, 600))
class player:
def __init__(self, x, y, height, width):
self.x = x
self.y = y
self.height = height
self.width = width
self.speed = 2
def draw(self):
pygame.draw.rect(d, (0, 0, 0), (self.x, self.y, self.width, self.height))
def move_left(self):
self.x -= self.speed
def move_right(self):
self.x += self.speed
class bullet:
def __init__(self):
self.radius = 10
self.speed = 20
def shoot(self):
x = p.x
y = p.y
self.shooting = True
while self.shooting:
d.fill((98, 98, 98))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
y -= self.speed
pygame.draw.circle(d, (255, 0, 0), (x, y), self.radius)
pygame.time.Clock().tick(100)
win.update()
if y <= 0:
self.shooting = False
b = bullet()
p = player(600, 500, 50, 30)
while True:
d.fill((98, 98, 98))
p.draw()
for event in pygame.event.get():
pass
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
b.shoot()
if event.key == pygame.K_LEFT:
p.move_left()
if event.key == pygame.K_RIGHT:
p.move_right()
win.update()
这是经过几次试用和错误后我可以提出的,但这确实是无效的。首先,当我按太空栏时,玩家消失了。我想这很明显,因为我有不同的射击和射击循环,但是我不知道如何绕过它,并在同一循环中实现射击和播放器。
我遇到的第二个问题是在self.shoot.shooting时打破:
循环。当y到达某个点时,我尝试将其打破:
if y <= 0:
self.shooting = False
但这不会破裂。相反,它再次重新启动循环。
我遇到的另一个奇怪的问题是,每次我移动鼠标(稍微快)或一次按下一堆按钮时,它会在self.sely.shoot.shoot.shooting 循环时打破。
Here’s my code:
import pygame, os
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
win = pygame.display
d = win.set_mode((1200, 600))
class player:
def __init__(self, x, y, height, width):
self.x = x
self.y = y
self.height = height
self.width = width
self.speed = 2
def draw(self):
pygame.draw.rect(d, (0, 0, 0), (self.x, self.y, self.width, self.height))
def move_left(self):
self.x -= self.speed
def move_right(self):
self.x += self.speed
class bullet:
def __init__(self):
self.radius = 10
self.speed = 20
def shoot(self):
x = p.x
y = p.y
self.shooting = True
while self.shooting:
d.fill((98, 98, 98))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
y -= self.speed
pygame.draw.circle(d, (255, 0, 0), (x, y), self.radius)
pygame.time.Clock().tick(100)
win.update()
if y <= 0:
self.shooting = False
b = bullet()
p = player(600, 500, 50, 30)
while True:
d.fill((98, 98, 98))
p.draw()
for event in pygame.event.get():
pass
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
b.shoot()
if event.key == pygame.K_LEFT:
p.move_left()
if event.key == pygame.K_RIGHT:
p.move_right()
win.update()
This is what I could come up with after a few trial and errors, but it is really ineffective. Firstly, the player disappears when I press the space bar. I guess this is obvious as I have a different loops for shooting and player, but I don’t know how to get around it and implement both shooting and player in the same loop.
The second problem I am having is breaking the while self.shooting:
loop. I tried breaking it when y reaches a certain point by doing this:
if y <= 0:
self.shooting = False
But this doesn’t break. Instead, it restarts the loop all over again.
Another weird problem I am having is that every time I move the mouse (slightly fast) or press a bunch of buttons at once, it breaks the while self.shooting
loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发射子弹的一般方法是将子弹的位置存储在列表中(
bullet_list
)。发射子弹时,将子弹的起始位置([start_x,start_y]
)添加到列表中。起始位置是发射子弹的物体(玩家或敌人)的位置。使用 - 环的遍历列表中的所有子弹。在循环中移动每个子弹的位置。从列表中删除子弹(
-loop到bullet_list.remove(bullet_pos)
)。因此,必须通过列表的副本(bullet_list [:]
)进行(请参阅在迭代时如何从列表中删除项目?)。使用另一个blit
屏幕上的子弹:另请参阅射击子弹。
请注意,班级名称通常应使用capwords公约。
(请参阅 python代码的样式指南 - 类名称 - 类名称)
这意味着它必须是
player
和bullet
而不是player
和bullet
您有一个应用程序循环,因此请使用它。在每个帧中,在主应用程序循环中连续更新和绘制所有对象。
类
子弹
不需要任何循环。构造函数必须具有该位置的参数(x
,y
)。此外,它需要改变位置的方法,并绘制子弹的绘制:使用
Bullets
的列表。按下 space 时创建一个新的子弹。在每个帧中移动子弹(update
),子弹如果不在窗口之外。在每个帧中绘制剩余的子弹:
此外使用
完整示例:
The general approach to firing bullets is to store the positions of the bullets in a list (
bullet_list
). When a bullet is fired, add the bullet's starting position ([start_x, start_y]
) to the list. The starting position is the position of the object (player or enemy) that fires the bullet. Use afor
-loop to iterate through all the bullets in the list. Move position of each individual bullet in the loop. Remove a bullet from the list that leaves the screen (bullet_list.remove(bullet_pos)
). For this reason, a copy of the list (bullet_list[:]
) must be run through (see How to remove items from a list while iterating?). Use anotherfor
-loop toblit
the remaining bullets on the screen:See also Shoot bullet.
Please note, that class names should normally use the CapWords convention.
(See Style Guide for Python Code - Class names)
That means it has to be
Player
andBullet
rather thanplayer
andbullet
You have an application loop, so use it. All the objects are continuously updated and drawn in the main application loop, in each frame.
The class
Bullet
do not need any loop. The constructor has to have parameters for the position (x
,y
). Further it needs on method which changes the position and one which draws the bullet:Use a list of
bullets
. Create a new bullet when space is pressed. Move the bullets (update
) in every frame anremove
a bullet if it is out of the window. Draw the remaining bullets in every frame:Furthermore use
pygame.key.get_pressed()
use to get the state of the keys in every frame and to update the position of the player:Complete example: