Help with "crosshairs" program

Help with "crosshairs" program

Postby Hissing Cockroach » Tue Oct 01, 2013 7:22 pm

I have made a program called crosshairs(http://python-forum.org/viewtopic.php?f=11&t=7273) and am
modifying it to where, when you click, it will create a small, black circle wherever the cursor is. I've tried it different
ways, such as adding

Code: Select all
    mouse_pos = pygame.mouse.get_pos()


to the loop, and

Code: Select all
class Shoot(object):
    if pygame.mouse.get_pressed():
        pygame.draw.circle(DISPLAYSURF, BLACK, (mouse_pos), 5)


to the end, but none of the ways I've tried it worked, they just
ignored that part of the code and went on.
Does anyone know how to do this?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help with "crosshairs" program

Postby dboxall123 » Tue Oct 01, 2013 7:35 pm

Post your entire code mate, let's see what's happening. It's hard to see what's going on here. Not wishing to be rude, but I suspect that you haven't used classes before. Classes don't usually start with an if statement.Post the entire thing and I'll see if I can help :)
Last edited by dboxall123 on Tue Oct 01, 2013 7:41 pm, edited 1 time in total.
dboxall123
 
Posts: 110
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with "crosshairs" program

Postby Hissing Cockroach » Tue Oct 01, 2013 7:40 pm

Code: Select all
import sys, pygame, math
from pygame.locals import *


WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED = (255, 0, 0)

BGCOLOR = WHITE

WINDOWWIDTH = 640
WINDOWHEIGHT = 480

FPS = 30








   
       
       

pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Crosshairs')



while True:
    mouse_pos = pygame.mouse.get_pos()
    mousex,mousey = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()


    DISPLAYSURF.fill(BGCOLOR)




    pygame.draw.circle(DISPLAYSURF, RED, (320,240), 50, 50)
    pygame.draw.circle(DISPLAYSURF, WHITE, (320,240), 40,40)
    pygame.draw.circle(DISPLAYSURF, RED, (320,240), 30,30)
    pygame.draw.circle(DISPLAYSURF, WHITE, (320,240), 20,20)
    pygame.draw.circle(DISPLAYSURF, RED, (320,240), 10,10)                   
    pygame.draw.line(DISPLAYSURF, BLACK, (mousex - 2000, mousey), (mousex + 2000, mousey))
    pygame.draw.line(DISPLAYSURF, BLACK, (mousex, mousey - 2000), (mousex, mousey + 2000))


    pygame.display.update()
    FPSCLOCK.tick(FPS)
   
class Shoot(object):
    if pygame.mouse.get_pressed():
        pygame.draw.circle(DISPLAYSURF, BLACK, (mouse_pos), 5)
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help with "crosshairs" program

Postby micseydel » Tue Oct 01, 2013 7:47 pm

Please explain what you think your class code is doing.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1441
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help with "crosshairs" program

Postby Hissing Cockroach » Tue Oct 01, 2013 7:52 pm

micseydel wrote:Please explain what you think your class code is doing.

It's making it so that wherever you click, it creates a black circle, isn't it?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help with "crosshairs" program

Postby dboxall123 » Tue Oct 01, 2013 7:53 pm

Ok, firstly, in your code you have not created an instance of the Shoot class. However, even if you had, that class would still not work. Ideally, you need to learn Python before trying to learn Pygame. Trust me, I made the same mistake. I tried making games before I had even a basic grasp of the Python language (although I'm still not an expert - far from it!). I had constant issues until I worked through a Python tutorial. Then I started to understand what is going on. I think you should work through a tutorial first - here is an excellent one for Python 3 - http://openbookproject.net/thinkcs/python/english3e/ or there is one by the same authors for Python 2 here http://www.greenteapress.com/thinkpython/html/index.html I will try to write you a working version of this code, may take me a little while though :)
dboxall123
 
Posts: 110
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with "crosshairs" program

Postby Hissing Cockroach » Tue Oct 01, 2013 7:57 pm

Thanks!
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help with "crosshairs" program

Postby dboxall123 » Tue Oct 01, 2013 9:49 pm

Ok, this isn't exactly great, (like I said, I'm no expert), but it works!
Code: Select all
import pygame as pg
import math
import sys

WHITE    = (255, 255, 255)
BLACK    = (  0,   0,   0)
RED = (255, 0, 0)

BGCOLOR = WHITE

WINDOWWIDTH = 640
WINDOWHEIGHT = 480

       
class Control(object):

    def __init__(self):
        self.bullet_holes = []
        self.screen = pg.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
        self.done = False
        self.clock = pg.time.Clock()

    def update(self):
        self.mousex,self.mousey = pg.mouse.get_pos()
        self.screen.fill(BGCOLOR)
        pg.draw.circle(self.screen, RED, (320,240), 50, 50)
        pg.draw.circle(self.screen, WHITE, (320,240), 40,40)
        pg.draw.circle(self.screen, RED, (320,240), 30,30)
        pg.draw.circle(self.screen, WHITE, (320,240), 20,20)
        pg.draw.circle(self.screen, RED, (320,240), 10,10)                   
        pg.draw.line(self.screen, BLACK, (self.mousex - 2000, self.mousey), (self.mousex + 2000, self.mousey))
        pg.draw.line(self.screen, BLACK, (self.mousex, self.mousey - 2000), (self.mousex, self.mousey + 2000))
        for bullet_pos in self.bullet_holes:
            pg.draw.circle(self.screen,BLACK,bullet_pos,7)

    def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
                self.done = True
            if pg.mouse.get_pressed()[0]:
                self.bullet_holes.append((self.mousex,self.mousey))
               
    def main_loop(self):
        while not self.done:
            self.update()
            self.event_loop()
            pg.display.flip()
            self.clock.tick(60)

if __name__ == '__main__':
    game = Control()
    game.main_loop()
    pg.quit()
    sys.exit()

I tried to make a seperate Shoot() class, but I can't work out how to assign circles to classes, because
Code: Select all
def __init__(self):
    self.image = pg.draw.circle(screen,black,pos,5)

won't work.I'm sure it's a lot more simple than I think - if someone wants to show me I'd be real grateful! Anyways, it works now. Enjoy!
dboxall123
 
Posts: 110
Joined: Fri Jul 12, 2013 5:28 pm

Re: Help with "crosshairs" program

Postby Hissing Cockroach » Tue Oct 01, 2013 10:44 pm

I really apreciate it! I've been
trying to do this for a long time.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Help with "crosshairs" program

Postby Mekire » Tue Oct 01, 2013 11:32 pm

dboxall123 wrote:
Code: Select all
def event_loop(self):
    for event in pg.event.get():
        if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
            self.done = True
        if pg.mouse.get_pressed()[0]:
            self.bullet_holes.append((self.mousex,self.mousey))

Within the event loop you should be processing pg.MOUSEBUTTONDOWN events and then looking at the attributes event.button and event.pos rather than using pygame.mouse.get_pressed() probably.

-Mek
User avatar
Mekire
 
Posts: 1023
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help with "crosshairs" program

Postby DrakeMagi » Wed Oct 02, 2013 2:52 am

here another option
Code: Select all
import pygame
import sys
pygame.init()
pygame.mouse.set_visible(False)

# version control optional
if sys.version_info[0] == 2:
    range_ = xrange
else:
    range_ = range

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)

SIZE = pygame.Rect(0,0,800,600)

def refresh_background(surface):
    surface.fill(WHITE)
    for c in range_(90, 5, -10):
        color = [WHITE,RED][(c / 10) % 2]
        pygame.draw.circle(surface, color, SIZE.center, c)

def main():
    pygame.display.set_caption("Bullet Holes")
    screen = pygame.display.set_mode(SIZE.size)
    clock = pygame.time.Clock()
    # mouse position
    mpos = SIZE.center
    shot_tick = 0
    left_mouse_down = False
    background = pygame.Surface(SIZE.size)
    refresh_background(background)
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    left_mouse_down = True
                elif event.button == 3:
                    refresh_background(background)
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    left_mouse_down = False
            elif event.type == pygame.MOUSEMOTION:
                mpos = event.pos
       
        tick = pygame.time.get_ticks()
        # gun rate of fire
        if left_mouse_down:
            if tick > shot_tick:
                shot_tick = tick + 250
                pygame.draw.circle(background, BLACK, (mpos), 4)
           
        screen.blit(background, (0,0))
        pygame.draw.line(screen, BLACK, (0, mpos[1]), (SIZE.width, mpos[1]))
        pygame.draw.line(screen, BLACK, (mpos[0], 0), (mpos[0], SIZE.height))
        pygame.display.flip()
        clock.tick(60)
    pygame.quit()

if __name__ == '__main__':
    main()
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Help with "crosshairs" program

Postby dboxall123 » Wed Oct 02, 2013 10:19 am

Within the event loop you should be processing pg.MOUSEBUTTONDOWN events and then looking at the attributes event.button and event.pos rather than using pygame.mouse.get_pressed() probably.

To be honest Mek, I haven't really used the mouse for a LONG time, I just copied and pasted his code and built it up from there. Anyway, Hissing Cockroach, I Mek means something like this:
Code: Select all
def event_loop(self):
        for event in pg.event.get():
            if event.type ==  pg.QUIT or (event.type == pg.KEYUP and event.key == pg.K_ESCAPE):
                self.done = True
            elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
                self.bullet_holes.append(event.pos)

If you want, you could also condense your update() function down a little bit by doing something like this:
Code: Select all
def update(self):
        self.mousex,self.mousey = pg.mouse.get_pos()
        self.screen.fill(BGCOLOR)
        for i in range(3):
            pg.draw.circle(self.screen,RED,(320,240),i*20+10,10)
        pg.draw.line(self.screen, BLACK, (self.mousex - 2000, self.mousey), (self.mousex + 2000, self.mousey))
        pg.draw.line(self.screen, BLACK, (self.mousex, self.mousey - 2000), (self.mousex, self.mousey + 2000))
        for bullet_pos in self.bullet_holes:
            pg.draw.circle(self.screen,BLACK,bullet_pos,7)
dboxall123
 
Posts: 110
Joined: Fri Jul 12, 2013 5:28 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests