글 머리 기호를 만들기 위해 할당 된 공간이있는 곳에서 코드를 실행 한 다음 발사하고 화면에 업데이트하는 데 문제가있다. 뭐가 문제 야?파이 게임에서 발사하려고 할 때 글 머리 기호가 나타나지 않는다.
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을 사용하여 수동으로 업데이트했습니다.
저를 비난하는 대신, 어떻게해야하는지 알려주세요. 파이썬을 7 개월 동안 사용했습니다. – HKVariant
@HunterKepley 나는 당신을 비판하지 않고, 총알이 보이지 않는 이유에 대해 간단하게 대답하고 있습니다. 불행히도, 당신이 정확히 여기서 무엇을하려고하는지 이해하지 못합니다. 그래서 나는 다른 것을 제안하지 않았습니다. – MattDMo
스페이스 바를 누를 때 움직이는 글 머리 기호가 나타나게하려고합니다. – HKVariant