Ich habe meinen Code auf das Notwendigste vereinfacht, um es zu verstehen, zu erleichtern. Alles, was passiert, ist, wenn ein bestimmter Bereich angeklickt wird, wird eine Kugel in diesem Bereich ausgelöst, der von einem weißen Quadrat ausgeht. Das Problem ist, dass ich nicht möchte, dass die Kugel in der Gegend klickt, sondern genau dort, wo sie geklickt wurde.Pygame genaue Aufnahme im Spiel
Hier ist mein Code:
import pygame, sys, time, random, math
from pygame.locals import *
background = (0, 0, 0)
entity_color = (255, 255, 255,255)
listLaser = pygame.sprite.Group()
player_x, player_y = 0, 0
move_player_x, move_player_y = 0, 0
all_sprites_list = pygame.sprite.Group()
class Entity(pygame.sprite.Sprite):
"""Inherited by any object in the game."""
def __init__(self, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
# This makes a rectangle around the entity, used for anything
# from collision to moving around.
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
class User(Entity):
"""
Player controlled or AI controlled, main interaction with
the game
"""
def __init__(self, x, y, width, height):
super(User, self).__init__(x, y, width, height)
self.image = pygame.Surface([20, 37])
self.image.fill(entity_color)
self.image.blit(self.image, (0, 0))
class Player(User):
"""The player controlled Character"""
def __init__(self, x, y, width, height):
super(Player, self).__init__(x, y, width, height)
pass
class Bullet(pygame.sprite.Sprite):
def __init__(self, mouse, player):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([4, 10])
self.image.fill(entity_color)
self.mouse_x, self.mouse_y = mouse[0], mouse[1]
self.player = player
self.rect = self.image.get_rect()
def update(self):
'''
Gets vector from the two points and gets a direction and sends the bullet that way using player and clicked points
'''
speed = -4.
range = 200
distance = [self.mouse_x - self.player[0], self.mouse_y - self.player[1]]
norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
direction = [distance[0]/norm, distance[1 ]/norm]
bullet_vector = [direction[0] * speed, direction[1] * speed]
self.rect.x -= bullet_vector[0]
self.rect.y -= bullet_vector[1]
pygame.init()
pygame.display.set_caption('Race')
window_width = 800
window_height = 700
screen = pygame.display.set_mode((window_width, window_height))
player = Player(20, window_height/2, 40, 37)
all_sprites_list.add(player)
while True: # the main game loop
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
bullet = Bullet(pygame.mouse.get_pos(), [player.rect.x, player.rect.y])
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
listLaser.add(bullet)
for ent in all_sprites_list:
ent.update()
screen.fill(background)
all_sprites_list.draw(screen)
pygame.display.flip()
pygame.display.update()
ich wirklich meinen Code helfen modifizieren müssen, die Kugel bekommen genau an dem geklickten Bereich kann
Scheint, dass die Richtung richtig berechnet wird. Was ist los mit dir? – MBo