Pygame:画线 [英] Pygame: Drawing Lines

查看:90
本文介绍了Pygame:画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我之前的问题 Python 中的循环函数,我无法放置包含为刽子手游戏画线的命令的函数.它并没有完全划清界限,我首先怀疑是 for 循环或函数的问题.现在我意识到 Pygame 有点小问题.

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()

现在奇怪的事情来了.

当你按下 Blitted 到屏幕上时,它变成红色,就像它应该的那样,完美地绘制线条,但是当 B 消失时消失,我明白为什么.之后,我添加了额外的 if 代码.(请注意,pygame.draw.line(s) 是相同的),它在 shell 中打印 hi 和 yay,但不保留该行.无论如何要解决这个问题?

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?

推荐答案

在您调用 pygame.draw.line() 之后,您可能将屏幕重新绘制成完全白色,这将绘制线条并将其隐藏.不是像你那样画线,我会从中建立一个刽子手课程

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 添加了一个运行示例

#!/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()

这篇关于Pygame:画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆