2015-01-12 2 views
-3

글 머리 기호를 만들기 위해 할당 된 공간이있는 곳에서 코드를 실행 한 다음 발사하고 화면에 업데이트하는 데 문제가있다. 뭐가 문제 야?파이 게임에서 발사하려고 할 때 글 머리 기호가 나타나지 않는다.

import pygame, random, os, sys 

pygame.init() 
pygame.mixer.pre_init(441100, -16, 2, 2048) 
pygame.mixer.init() 

width = 640 
height = 480 

black = ( 0, 0, 0) 
white = (255,255,255) 

playerimg = pygame.image.load("images/shipup.png") 

class player: # Class for player ship 
    def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc): 
     self.x = x 
     self.y = y 
     self.xc = xc 
     self.yc = yc 
     self.w = w 
     self.h = h 
     self.img = img 
     self.lives = lives 
     self.angle = angle 
     self.direc = direc 

    def update(self): 
     gameDisplay.blit(self.img, (self.x, self.y)) 
     self.x += self.xc 
     self.y += self.yc 

playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up") 

bulletxc = 0 
bulletyc = 0 

class bullet: # Class for shooting bullets 
    def __init__(self, x, y, xc, yc, w, h, img): 
     self.x = x 
     self.y = y 
     self.xc = xc 
     self.yc = yc 
     self.w = w 
     self.h = h 
     self.img = img 

    def update(self): 
     gameDisplay.blit(self.img, (self.x, self.y)) 
     self.x += self.xc 
     self.y += self.yc 
     if playership.direc == "right": # Determines what direction to shoot 
      bulletxc = 6 
      bulletyc = 0 
     if playership.direc == "left": 
      bulletxc = -6 
      bulletyc = 0 
     if playership.direc == "up": 
      bulletyc = -6 
      bulletxc = 0 
     if playership.direc == "down": 
      bulletyc = 6 
      bulletxc = 0 

bulletlist =() 

gameDisplay = pygame.display.set_mode((width, height)) 
pygame.display.set_caption("Asteroids", "Ast...") 
clock = pygame.time.Clock() 

def mainloop(): 
    global bulletlist 
    gameExit = False 

    while not gameExit: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

               # Basic movements V 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_w: 
        playership.yc = -3 
        playership.img = pygame.image.load("images/shipup.png") 

       if event.key == pygame.K_s: 
        playership.yc = 3 
        playership.img = pygame.image.load("images/shipdown.png") 

       if event.key == pygame.K_d: 
        playership.xc = 3 
        playership.img = pygame.image.load("images/shipright.png") 

       if event.key == pygame.K_a: 
        playership.xc = -3 
        playership.img = pygame.image.load("images/shipleft.png") 

       if event.key == pygame.K_SPACE: # Here is where the bullet fires. 
        for i in bulletlist: 
         bulletlist.append(bullet) 
         bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png")) 

      elif event.type == pygame.KEYUP: 
       if event.key == pygame.K_w: 
        playership.yc = 0 

       if event.key == pygame.K_s: 
        playership.yc = 0 

       if event.key == pygame.K_d: 
        playership.xc = 0 

       if event.key == pygame.K_a: 
        playership.xc = 0 

     gameDisplay.fill(black) 

     playership.update() 
     for i in bulletlist: 
      i.update() 

     if playership.x < 0: # Collisions against the sides of the screen. 
      playership.x = -1 

     if playership.y < 0: 
      playership.y = -1 

     if playership.x + playership.h > width: 
      playership.x = width - playership.h 

     if playership.y + playership.h > height: 
      playership.y = height - playership.h 

     pygame.display.update() 
     clock.tick(60) 


if __name__ == "__main__": 
    mainloop() 


pygame.quit() 
quit() 

문제점 : 글 머리 기호는 표시되지 않지만 오류는 없습니다.

시도 : K_SPACE if 문 내에서 gameDisplay.blit을 사용하여 수동으로 업데이트했습니다.

답변

0
import pygame, random, os, sys 

pygame.init() 
pygame.mixer.pre_init(441100, -16, 2, 2048) 
pygame.mixer.init() 

width = 640 
height = 480 

black = ( 0, 0, 0) 
white = (255,255,255) 

playerimg = pygame.image.load("images/shipup.png") 

class player: # Class for player ship 
    def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc): 
     self.x = x 
     self.y = y 
     self.xc = xc 
     self.yc = yc 
     self.w = w 
     self.h = h 
     self.img = img 
     self.lives = lives 
     self.angle = angle 
     self.direc = direc 

    def update(self): 
     gameDisplay.blit(self.img, (self.x, self.y)) 
     self.x += self.xc 
     self.y += self.yc 

playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up") 

bulletxc = 0 
bulletyc = 0 

class bullet: # Class for shooting bullets 
    def __init__(self, x, y, xc, yc, w, h, img): 
     self.x = x 
     self.y = y 
     self.xc = xc 
     self.yc = yc 
     self.w = w 
     self.h = h 
     self.img = img 

    def update(self): 
     gameDisplay.blit(self.img, (self.x, self.y)) 
     self.x += self.xc 
     self.y += self.yc 
     if playership.direc == "right": # Determines what direction to shoot 
      bulletxc = 6 
      bulletyc = 0 
     if playership.direc == "left": 
      bulletxc = -6 
      bulletyc = 0 
     if playership.direc == "up": 
      bulletyc = -6 
      bulletxc = 0 
     if playership.direc == "down": 
      bulletyc = 6 
      bulletxc = 0 

bulletlist = bullet(-33, -33, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png")) 

gameDisplay = pygame.display.set_mode((width, height)) 
pygame.display.set_caption("Asteroids", "Ast...") 
clock = pygame.time.Clock() 

def mainloop(): 
    global bulletlist 
    gameExit = False 

    while not gameExit: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

               # Basic movements V 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_w: 
        playership.yc = -3 
        playership.img = pygame.image.load("images/shipup.png") 
        bulletyc = -6 
        bulletxc = 0 

       if event.key == pygame.K_s: 
        playership.yc = 3 
        playership.img = pygame.image.load("images/shipdown.png") 
        bulletyc = 6 
        bulletxc = 0 

       if event.key == pygame.K_d: 
        playership.xc = 3 
        playership.img = pygame.image.load("images/shipright.png") 
        bulletxc = 6 
        bulletyc = 0 

       if event.key == pygame.K_a: 
        playership.xc = -3 
        playership.img = pygame.image.load("images/shipleft.png") 
        bulletxc = -6 
        bulletyc = 0 

       if event.key == pygame.K_SPACE: # Here is where the bullet fires. 
        bulletlist = bullet(playership.x + 15, playership.y + 15, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png")) 

      elif event.type == pygame.KEYUP: 
       if event.key == pygame.K_w: 
        playership.yc = 0 

       if event.key == pygame.K_s: 
        playership.yc = 0 

       if event.key == pygame.K_d: 
        playership.xc = 0 

       if event.key == pygame.K_a: 
        playership.xc = 0 

     gameDisplay.fill(black) 

     playership.update() 
     bulletlist.update() 

     if playership.x < 0: # Collisions against the sides of the screen. 
      playership.x = -1 

     if playership.y < 0: 
      playership.y = -1 

     if playership.x + playership.h > width: 
      playership.x = width - playership.h 

     if playership.y + playership.h > height: 
      playership.y = height - playership.h 

     pygame.display.update() 
     clock.tick(60) 


if __name__ == "__main__": 
    mainloop() 


pygame.quit() 
quit() 

튜플을 제거하고 제대로 작동합니다.

2

문제는 여기에 코드입니다 :

if event.key == pygame.K_SPACE: # Here is where the bullet fires. 
    for i in bulletlist: 
     bulletlist.append(bullet) 
     bulletlist = bullet(playership.x + 25, playership.y + 25, 
      bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png")) 

문제는 이전 당신이 bulletlist을 정의한다는 것입니다 : 빈 튜플로

bulletlist =() 

. 그러나 위의 코드에서 bulletlist 길이가 0이기 때문에 for 루프는 아무 것도하지 않으므로 i 값이 없습니다. 이 코드는 Python에서는 오류가 아니지만 아무것도 수행하지 않습니다.

그러나, 길이, 당신은 나쁜 생각이다 for 루프, 내 bullet의 인스턴스에 bulletlist 식별자를 재 할당되어 있었다 경우에도 마찬가지입니다. 나는 당신이 여기에서 성취하려는 것을 생각해 보시고 아마도 다른 방법을 생각해내는 것이 좋습니다.

+0

저를 비난하는 대신, 어떻게해야하는지 알려주세요. 파이썬을 7 개월 동안 사용했습니다. – HKVariant

+1

@HunterKepley 나는 당신을 비판하지 않고, 총알이 보이지 않는 이유에 대해 간단하게 대답하고 있습니다. 불행히도, 당신이 정확히 여기서 무엇을하려고하는지 이해하지 못합니다. 그래서 나는 다른 것을 제안하지 않았습니다. – MattDMo

+0

스페이스 바를 누를 때 움직이는 글 머리 기호가 나타나게하려고합니다. – HKVariant

-1
if event.key == pygame.K_SPACE: # Here is where the bullet fires. 
        for i in bulletlist: 
         bulletlist.append(bullet) 
         bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png")) 

여기에서;

bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png")) 

이 부분은 bulletlist 더 이상 튜플 아니다, 잘못된 것입니다. 봐, 당신의 bullet 클래스가 더 이상 그 문장을 고쳐야한다는 것을 기억하십시오.

또한이 방법으로 불을 피울 수 있다고 생각하지 않습니다. 파이 게임에 대한 자세한 정보를 확인하십시오. Here 꽤 멋진 튜토리얼입니다.

관련 문제