Crosshairs

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Crosshairs

Postby Hissing Cockroach » Mon Sep 30, 2013 7:56 pm

This is my first working pygame. It's
not advanced or anything, but it's a start.
Here is the code:


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


WHITE    = (255, 255, 255)
BLACK    = (  0,   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:
    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.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)
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Crosshairs

Postby Mekire » Tue Oct 01, 2013 10:00 am

Congratulations on your first Pygame.

A couple things. Please don't use:
Code: Select all
from pygame.locals import *
Yes, I know you see it everywhere, but it is not a good habit. See this thread:
http://python-forum.org/viewtopic.php?f=25&t=4822

Next, first pygame or not, you need to get a grasp of functions and classes before continuing. Again, there are numerous "tutorials" out there that have you immediately start coding pygame programs in the global namespace. They are all horrible.

Here's a quick rewrite to a more generalizable structure:
Code: Select all
import sys
import os
import pygame as pg


WHITE    = (255, 255, 255)
BLACK    = (0, 0, 0)
SIZE = (WIDTH, HEIGHT) = (640, 480)


class Control(object):
    def __init__(self):
        os.environ["SDL_VIDEO_CENTERED"] = '1'
        pg.init()
        pg.display.set_caption('Crosshairs')
        self.screen = pg.display.set_mode(SIZE)
        self.clock = pg.time.Clock()
        self.fps = 60
        self.keys = pg.key.get_pressed()
        self.done = False

    def game_loop(self):
        while not self.done:
            self.event_loop()
            self.render()
            pg.display.update()
            self.clock.tick(self.fps)

    def event_loop(self):
        for event in pg.event.get():
            self.keys = pg.key.get_pressed()
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
                self.done = True

    def render(self):
        mouse_x, mouse_y = pg.mouse.get_pos()
        self.screen.fill(WHITE)
        self.screen.fill(BLACK, (mouse_x, 0, 1, HEIGHT))
        self.screen.fill(BLACK, (0, mouse_y, WIDTH, 1))


if __name__ == "__main__":
    Control().game_loop()
    pg.quit()
    sys.exit()

This may appear more complicated to you now, but in the future this type of structuring will greatly simplify your life.

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


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests