1

This is another question that is about a project from this page Pygame.transform.rotate scales istead rotation

I got the barell rotating but now I tried to make a bullet moving toward the mouse click position. So here is what I've done about it.

constants.py

pygame.init()

scr = pygame.display.list_modes()
resolution = (scr[0][0],scr[0][1])
angle = 0
barell = pygame.image.load("barell.png")
tux = pygame.image.load("tux.png")
tux2 = pygame.transform.scale(tux,(100,100))
cropped = pygame.Surface((10,30))
mouse_pos = rx,ry = 0,0
bullet = pygame.image.load("bullet.png")

faster_bullet.py

import pygame
import groups,math,constants

class Missile(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = constants.bullet
        self.rect = self.image.get_rect()
        self.rect.x = 750
        self.rect.y = 550
        self.speed = 1
        self.angle = 0

    def add_to_group(self):
        groups.all_sprite_group.add(self)
        groups.bullet_group.add(self)



    def move(self):
        self.rect.x += math.sin(self.angle) * self.speed
        self.rect.y -= math.cos(self.angle) * self.speed
        pygame.transform.rotate(constants.bullet,self.angle)

event.py

import pygame,sys,math,constants
from pygame.locals import *
from faster_bullet import *
import screen


class Event(object):
    def __init__(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                constants.mouse_pos = event.pos
                 b = Missile()
                 b.add_to_group()
                 dx = b.rect.x - constants.mouse_pos[0]
                 dy = b.rect.y - constants.mouse_pos[1]
                 b.angle = math.degrees(math.atan2(dx,dy))
                 print b.angle

            elif event.type == KEYDOWN and event.key == K_c:
                pygame.image.save(screen.screen,"screenshot.png")

But when I click somewhere on the screen it moves in one of these three positions and stop on the edges of the screen.

positions

I tried following these instructions to solve the problem but none worked for me.

How to make an enemy follow a player in pygame? - the answer at the bottom

But I think that my math knowledge is the real problem as I have no idea what sin,cosin mean as I just finished primary school. I just followed this tutorial for the movement http://www.petercollingridge.co.uk/pygame-physics-simulation/movement

Cœur
  • 37,241
  • 25
  • 195
  • 267
Marko Scekic
  • 105
  • 6
  • Have a look at this question: [Make a sprite move to the mouse click position step by step](http://stackoverflow.com/questions/16288905/make-a-sprite-move-to-the-mouse-click-position-step-by-step/16294710#16294710) – sloth Aug 12 '13 at 07:56
  • Interesting!I'll definetly have a look at it XD.Thank you. – Marko Scekic Aug 12 '13 at 15:12

0 Answers0