[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Requesting a hand with a simple Death Counter



I spent some time with your counter and this is the final code i got. Regards!



# Death Counter
import pygame, sys
from pygame.locals import *

# Variables: In python you dont need to declare your variables, you can store any type of variable at any point,
# but you have to be careful to store what you want. Â
#deathcount = float(0)

pygame.display.init()
BLACK = (0,0,0)
WIDTH = 320
HEIGHT = 260
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)

while True:
  events = pygame.event.get()
  for event in events:
    if event.type==KEYDOWN:Â
      if event.key == K_1:
        with open("deathcounter.txt", "rt") as in_file:
          deathcount = int(in_file.read()) #store an integer
          deathcount = deathcount + 1
        with open("deathcounter.txt", "wt") as out_file:
          out_file.write(str(deathcount))
      if event.key == K_2:
        deathcount = 0
        with open("deathcounter.txt", "wt") as out_file:
          out_file.write(str(deathcount))
      print deathcount


  if event.type == QUIT:
    pygame.quit()
    sys.exit()

2014-10-27 6:36 GMT-05:00 Noel Garwick <noel.garwick@xxxxxxxxx>:

Not all events have a .key attribute (QUIT and MOUSEMOTION) for example. So instead, do something like this:

for event in events:
ÂÂÂ if event.type == pygame.KEYDOWN:
ÂÂÂÂÂÂÂ if event.key == K_1:
ÂÂÂÂÂÂÂÂÂÂÂ # logic here

On Oct 27, 2014 6:20 AM, "PBRGamer" <PBRGamerSNES@xxxxxxxxx> wrote:
Learning how to program in python with pygame and could use a hand. I'm
certain I'm doing a lot wrong.

Purpose of program.

While running in the background...
Press the '1' Key and it opens a file, takes the #, adds +1 to it, then
writes the new number in the file over the old number.
Press the '2' Key and it opens the file, and writes a 0 over the number in
the file.

Getting an error on line 19 while trying to modify a tutorial on something
else to meet my needs.

if event.key == pygame.K_1:
AttributeError: event member not defined

Here is the rest of the code.

# Death Counter
import pygame, sys
import pygame.locals

# Variables
deathcount = float(0)

pygame.init()
BLACK = (0,0,0)
WIDTH = 320
HEIGHT = 260
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)

while True:
  events = pygame.event.get()
  for event in events:
    if event.key == pygame.K_1:
      with open("deathcounter.txt", "rt") as in_file:
        deathcount = in_file.read()
        deathcount = deathcount + 1
      with open("deathcounter.txt", "wt") as out_file:
        out_file.write(deathcount)
    if event.key == pygame.K_2:
      deathcount = 0
      with open("deathcounter.txt", "wt") as out_file:
        out_file.write(deathcount)

pass
if event.type == QUIT:
  pygame.quit()
  sys.exit()

Thanks for your help =)





--
View this message in context: http://pygame-users.25799.x6.nabble.com/Requesting-a-hand-with-a-simple-Death-Counter-tp1481.html
Sent from the pygame-users mailing list archive at Nabble.com.