如何解决Pygame 如何让 Sprite 面对鼠标
我对编程很陌生,我设法编写了一些可以运行的代码
目前,我有一个 500,500 的游戏区,里面有一个精灵,它是一个从图片左上角射出子弹的军人,我希望他在屏幕上不断跟随我的鼠标光标,但这就是我的地方卡住了。
我曾尝试将其他人的代码分割成我的代码,但要么不起作用,要么我没有对其进行足够的更改,要么我对其进行了过多的更改
我听说我需要建立一个向量,但 IDK 如何
这是我的代码很抱歉,如果它一团糟
import pygame
pygame.init()
import math
clock = pygame.time.Clock()
still=pygame.image.load('pngs/pr1.png')
display = pygame.display.set_mode((500,500))
class player_object(object):
def __init__(self,x,y,radius,height):
self.x = x
self.y = y
self.radius = radius
self.height = height
self.vel = 5
def draw(self,display):
display.blit(still,(self.x,self.y))
class bullet(object):
def __init__(self,mouse_x,mouse_y):
self.x = x
self.y = y
self.mouse_x = mouse_x
self.mouse_y = mouse_y
self.lifetime = 50
self.speed = 15
self.angle = math.atan2(mouse_y-self.y,mouse_x-self.x)
self.x_vel = math.cos(self.angle) * self.speed
self.y_vel = math.sin(self.angle) * self.speed
self.radius = 5
def draw(self,draw):
self.x += int(self.x_vel)
self.y += int(self.y_vel)
pygame.draw.circle(display,(0,0),self.y),self.radius)
self.lifetime -= 1
class enemy_object(object):
def __init__(self,radius):
self.x = x
self.y = y
self.radius = radius
def draw(self,display):
pygame.draw.circle(display,255,self.radius)
player = player_object(100,100,50,30)
bullets = []
enemy = enemy_object(400,400,20)
run = True
while run:
display.fill((255,255))
x,y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
bullets.append(bullet(player.x,player.y,y))
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and player.y > player.vel:
player.y -= 5
if keys[pygame.K_s] and player.y < 500 - player.height - player.vel:
player.y += 5
if keys[pygame.K_a] and player.x > player.vel:
player.x -= 5
if keys[pygame.K_d] and player.x < 500 - player.radius - player.vel:
player.x += 5
for bullet_ in bullets:
if bullet_.lifetime <= 0:
bullets.pop(bullets.index(bullet_))
bullet_.draw(display)
player.draw(display)
enemy.draw(display)
clock.tick(60)
pygame.display.update()
另外,我对游戏的想法是像这样的自上而下的射击游戏 无尽的战争
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。