私はxwingが銃を撃ち殺してゲームを作成しようとしています。私はスペースバーを押すと、xwingから撮影画像が表示されないだけです。私はこの質問が以前に尋ねられたことを知っていますが、私はそれを得ることができません。私はPythonとPygameにちょっと新しいです。ありがとう!パイゲでキャラクター撮影をするには?
import sys
import random
import pygame as pg
pg.init()
screen = pg.display.set_mode((1280, 800))
display_width = 1280
display_height= 800
black= (0,0,0)
white= (255,255,255)
red = (255,0,0)
blue_violet = (138,43,226)
bullets= []
xwingImg = pg.image.load('X-Wing.bmp').convert()
tieImg= pg.image.load('tiefighter.png').convert()
space=pg.image.load('space.jpg').convert()
xbullet = pg.image.load('bullet.png').convert()
BG_image = pg.image.load('space.jpg').convert()
def rot_center(image, angle):
"""rotate a Surface, maintaining position."""
loc = image.get_rect().center #rot_image is not defined
rot_sprite = pg.transform.rotate(image, angle)
rot_sprite.get_rect().center = loc
return rot_sprite
# or return tuple: (Surface, Rect)
# return rot_sprite, rot_sprite.get_rect()
def main():
clock = pg.time.Clock()
# Surfaces/images have a `get_rect` method which
# returns a rect with the dimensions of the image.
player_rect = xwingImg.get_rect()
player_rect.center = (640,400)
change_x = 0
change_y = 0
enemies = []
spawn_counter = 30
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == space:
shot.play()
for b in range(len(bullets)) :
bullets [b] [0] -=10
for bullet in bullets:
if bullet [0] <0:
bullet.remove(bullet)
bullets.append ([event.pos[0], 500])
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
change_x = 5
if event.key == pg.K_w:
change_y = -5
if event.key == pg.K_s:
change_y= 5
if event.key == pg.K_a:
change_x = -5
if event.type == pg.KEYUP:
if event.key == pg.K_d and change_x > 0:
change_x = 0
if event.key == pg.K_a and change_x < 0:
change_x = 0
if event.key == pg.K_w and change_y<0:
change_y=0
if event.key == pg.K_s and change_y>0:
change_y=0
# Spawn enemies if counter <= 0 then reset it.
spawn_counter -= 1
if spawn_counter <= 0:
# Append an enemy rect. You can pass the position directly as an argument.
enemies.append(tieImg.get_rect(topleft=(random.randrange(1280), -800)))
spawn_counter = 30
# Update player_rect and enemies.
player_rect.x += change_x
player_rect.y += change_y
for enemy_rect in enemies:
enemy_rect.y += 5
# Collision detection with pygame.Rect.colliderect.
if player_rect.colliderect(enemy_rect):
done = True
# Draw everything.
screen.blit(BG_image, (0,0))
for bullet in bullets:
screen.blit(xbullet, pg.Rect (bullet[0], bullet [1], 0, 0))
for enemy_rect in enemies:
screen.blit(tieImg, enemy_rect)
screen.blit(xwingImg, player_rect)
if player_rect.x >display_width:
player_rect.x = 0
if player_rect.x < 0:
player_rect.x= 1280
if player_rect.y>display_height:
player_rect.y = 0
if player_rect.y < 0:
player_rect.y= 800
pg.display.flip()
clock.tick(40)
if __name__ == '__main__':
main()
pg.quit()
sys.exit()
私はこれをしなかったが、私はPythonとpygameのに非常に新しいです。私は何をすべきかわからない。これを行う方法の例を私に教えてください。 – joe