Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame: Drawing Lines

In my previous question For Loop Functions in Python, I had trouble with putting functions that contained a command to draw a line for a hangman game. It didn't exactly draw the line, and I first suspected it was a problem with the for loop or the functions. Now I realize there is somewhat a glitch with Pygame.

I have tried solving the problem by using this code in the country, CANADA:

b2 = font.render(str(letters[1]), True, (red))
screen.blit(b2, (bPosition))
if hangman1x == -500 and hangman1y == -500:
    hangman1x = (775, 250)
    hangman1y = (775, 50)
    pygame.draw.line(screen, black, (hangman1x), (hangman1y), (5))
    pygame.display.flip()
    time.sleep(0.5)
    bPosition = -500, -500
    b1.x, b1.y = -500, -500
if hangman1x == (775, 250) and hangman1y == (775, 50):
    print 'hi'
    width = 6
    pygame.draw.line(screen, black, (hangman1x), (hangman1y), (5))
    print 'yay'
    pygame.display.flip()

Now here's the weird thing.

When you press the B blitted onto the screen, it turns red, like its meant to, draws the line perfectly fine, but disappears, when the B disappears, and I understand why. After that, I added that extra if code. (Notice that both pygame.draw.line(s) are the same), It prints hi and yay in the shell, but it does not keep the line. Anyway to solve this?

like image 538
JaredCubilla Avatar asked Sep 16 '26 21:09

JaredCubilla


1 Answers

After you are calling pygame.draw.line() you are probably redrawing your screen completely white, this will draw over the line and hide it. Instead of drawing lines like you are, I would build a hangman class draw from that

class Hangman():
  def __init__(self):
    self.lines = 0 #Number of lines to be drawn

  def draw(self,screen):
    #TODO draw to screen based on self.lines

#More code setting up pygame

drawlist = []
myMan = Hangman()
drawlist.append(myMan)
#mainloop
while 1:
  screen.fill('#000000')
  for item in drawlist:
    item.draw(screen)

This way you are redrawing you hangman every frame, and thus he is always being showed

EDIT Added a running example

#!/usr/bin/python
import pygame
pygame.init()

class Hangman():
  def __init__(self):
    self.lines = 0 #Number of lines to be drawn

  def hang(self):
    self.lines += 1

  def draw(self,screen):
    for x in range(self.lines):
      coord1 = (x*10,20)
      coord2 = (x*10,50)
      pygame.draw.line(screen,(0,0,0),coord1,coord2)

size = screenWidth,screenHeight = 200,70
screen = pygame.display.set_mode(size)
pygame.display.flip()

myman = Hangman()

drawlist = []
drawlist.append(myman)
#mainloop
running = True
while running:
  #EVENT HANDLING#
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False
    if event.type == pygame.KEYDOWN:
      if event.key == 32: #Spacebar
        myman.hang()

  #DRAWING#
  screen.fill((255,255,255))
  for item in drawlist:
    item.draw(screen)
  pygame.display.flip()
like image 87
Nmorrison72 Avatar answered Sep 18 '26 09:09

Nmorrison72



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!