我是pygame的新手,刚学会了什么类
我想让玩家射击子弹,但子弹没有画在屏幕上。
有4个python文件
所以我想的是先为每个子弹加载图像并制作表面, 第二个更新位置,然后在表面上绘制图像。如果子弹在目的地附近删除它。 每次按空格键来调用拍摄功能时都会运行此警告“libpng警告:iCCP:已知错误的sRGB配置文件”。我相信我可以忽略这一点。无论如何我无法找出为什么子弹图像没有显示出来。 有人可以帮我这个吗?谢谢。欢迎任何建议!
Game.py
import pygame as pyg
from pygame.locals import *
import sys
import player
import background, ect
#Screen size
s_width = 1280
s_height = 720
screen_size = (s_width,s_height)
#Player
p_speed = 5
#Initialization
pyg.init()
screen = pyg.display.set_mode(screen_size)
pyg.display.set_caption("Shoot")
#Set background
Background = background.Background("forest2.jpeg",(0,0))
#Set Player
Player= player.Player("Crab.png",100,100,640,360)
#Input
keys = {"right":False , "left":False , "up":False , "down":False , "bullet":False }
bullets = []
#Main Loop
running = True
while running:
#Get inputs
for event in pyg.event.get():
if event.type == QUIT:
pyg.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
keys['left'] = True
elif event.key == K_RIGHT:
keys['right'] = True
elif event.key == K_UP:
keys['up'] = True
elif event.key == K_DOWN:
keys['down'] = True
elif event.key == K_SPACE:
keys['bullet'] = True
elif event.key == K_q:
pyg.quit()
sys.exit()
elif event.type == KEYUP:
if event.key == K_LEFT:
keys['left'] = False
elif event.key == K_RIGHT:
keys['right'] = False
elif event.key == K_UP:
keys['up'] = False
elif event.key == K_DOWN:
keys['down'] = False
elif event.key == K_SPACE:
keys['bullet'] = False
#Get time
Player.clock.tick(60)
dt = Player.clock.get_time()
dt *= 0.001
#Update values
if keys['right']:
Player.x += p_speed
if keys['left']:
Player.x -= p_speed
if (keys['up'] and Player.on_ground):
Player.jump()
if keys['bullet']:
Player.shoot(bullets)
if Player.on_ground == False: #Gravity
Player.give_force(0,-750)
Player.update_pos(dt)
if Player.y >= 500:
Player.y = 500
Player.y_vel = 0
Player.y_accel = 0
Player.on_ground = True
for bullet in bullets:
bullet.update(dt)
if (bullet.desx - bullet.x)<0.3:
del bullet
#Draw
screen.fill([255,255,255])
screen.blit(Background.image,Background.rect)
screen.blit(Player.player_sprite,(Player.x,Player.y))
for bullet in bullets:
bullet.draw_bullet()
#Update game state
pyg.display.flip()
pyg.display.update()
player.py
#imports
import pygame as pyg
import sys, math
import ect
class Player(pyg.sprite.Sprite):
"""
This class represents the player image
and contains player actions
"""
def __init__(self, player_sprite_file, width, height, x, y):
pyg.sprite.Sprite.__init__(self)
#initialize variables(Size, Coordinate, clock)
self.width = width
self.height = height
self.x = x + width/2
self.y = y - height/2
self.on_ground = False
self.clock = pyg.time.Clock()
self.x_vel = 0
self.y_vel = 0
self.x_accel = 0
self.y_accel = 0
#create sprite(1. Load 2. Scale 3. Get rect 4. Create surface
self.player_sprite = pyg.image.load(player_sprite_file)
self.player_sprite = pyg.transform.scale(self.player_sprite, (width,height))
self.player_sprite_rect = self.player_sprite.get_rect()
self.player_sprite_rect.left ,self.player_sprite_rect.top = self.x, self.y
def move(self,x,y): #Moves player to destination
self.x = x + self.width/2
self.y = y - self.height/2
def give_force(self, x_accel, y_accel): #Input: A vector(direction, amount) for now lets keep the direction only up like a jump
self.x_accel += x_accel
self.y_accel += y_accel
def update_pos(self,dt): #Updates 1. time 2. velocity based on acceleration 3. position based on velocity
self.x_vel += self.x_accel*dt
self.y_vel += self.y_accel*dt
self.x += self.x_vel*dt
self.y -= self.y_vel*dt
def jump(self):
self.y_vel += 0
self.give_force(0,6000)
self.on_ground = False
def shoot(self,bullets):
bullets.append(ect.Bullet(self.x, self.y,self.x+300,self.y))
ect.py
#Imports
import random, math, pygame as pyg
from pygame.locals import *
class Bullet(pyg.sprite.Sprite):
"""
This class represents bullets
"""
def __init__(self,x,y,desx, desy):
pyg.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.desx = desx
self.desy = desy
h = math.sqrt(math.pow(desx-x,2) + math.pow(desy-y,2))
self.speed = 1 + random.randrange(-1,1)
self.speedx = self.speed*math.acos((desx - x)/h)
self.speedy = self.speed*math.asin((desy - y)/h)
self.surface = pyg.Surface((128,128))
self.image = pyg.image.load("bullet.png")
self.image = pyg.transform.scale(self.image, (128, 128))
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = x + 64, y -64
def update(self,dt):
self.x += (self.speedx*dt)
self.y -= (self.speedy*dt)
def draw_bullet(self):
self.surface.blit(self.image,(self.x -64,self.y + 64))
答案 0 :(得分:1)
您只需将self.image
子弹轻弹到self.surface
而不是屏幕表面。将screen
传递给draw_bullet
方法,然后将self.image
blit到其上。
# In the main while loop.
for bullet in bullets:
bullet.draw_bullet(screen)
#---------------------
# In the Bullet class.
def draw_bullet(self, screen):
screen.blit(self.image, (self.x -64, self.y + 64))