import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("game lupa, tank")
bg = pygame.image.load('images\\bg.jpg')
player = pygame.image.load('images\\tank_player_left.png')
player_left = pygame.image.load('images\\tank_player_left.png')
player_rigth = pygame.image.load('images\\tank_player_rigth.png')
player_down = pygame.image.load('images\\tank_player_down.png')
player_up = pygame.image.load('images\\tank_player_up.png')
patron1 = pygame.image.load('images\\patron1.png')
width = 60
height = 55
x = 250
y = 400
speed = 3
lastMove = 'right'
UDMove = 'up'
clock = pygame.time.Clock()
bullets = []
bullets1 = []
class snaryad():
def __init__(self, x, y, radius, color, facing):
self.x = x + 10
self.y = y
self.radius = radius - 1
self.facing = facing
self.color = color
self.vel = 10 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
def drawWindow():
for bullet in bullets:
bullet.draw(win)
runGame = True
while runGame:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if lastMove == 'right':
facing = 1
elif lastMove == 'left':
facing = -1
elif UDMove == 'up':
upfacing = 1
elif UDMove == 'down':
upfacing = -1
if len(bullets) < 10:
bullets.append(snaryad(round(x + width // 2), round(y + height // 2), 8, (0, 0, 255), facing))
if keys[pygame.K_LEFT] and x > 1:
x -= speed
player = player_rigth
lastMove = 'left'
elif keys[pygame.K_RIGHT] and x < 450:
x += speed
player = player_left
lastMove = 'right'
elif keys[pygame.K_UP] and y > 1:
y -= speed
player = player_up
UDMove = 'up'
elif keys[pygame.K_DOWN] and y < 450:
y += speed
player = player_down
UDMove = 'down'
win.blit(bg, (0, 0))
drawWindow()
win.blit(player, (x, y))
pygame.display.update()
pygame.quit()
1 ответ
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("game lupa, tank")
bg = pygame.image.load('images\\bg.jpg')
player = pygame.image.load('images\\tank_player_left.png')
player_left = pygame.image.load('images\\tank_player_left.png')
player_rigth = pygame.image.load('images\\tank_player_rigth.png')
player_down = pygame.image.load('images\\tank_player_down.png')
player_up = pygame.image.load('images\\tank_player_up.png')
patron1 = pygame.image.load('images\\patron1.png')
width = 60
height = 55
x = 250
y = 400
speed = 3
lastMove = 'right'
UDMove = 'up'
clock = pygame.time.Clock()
bullets = []
bullets1 = []
class snaryad():
def __init__(self, x, y, radius, color, facing, upf):
self.upf = upf
self.x = x + 10 if upf in ['left','right'] else x
self.y = y if upf in ['left','right'] else y + 10
self.radius = radius - 1
self.facing = facing
self.color = color
self.vel = 10 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
def drawWindow():
for bullet in bullets:
bullet.draw(win)
runGame = True
while runGame:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for bullet in bullets:
if bullet.upf in ['left','right'] and bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
elif bullet.upf in ['up','down'] and bullet.y < 500 and bullet.y > 0:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if lastMove == 'right':
facing = 1
elif lastMove == 'left':
facing = -1
elif lastMove == 'up':
facing = -1
elif lastMove == 'down':
facing = 1
if len(bullets) < 10:
bullets.append(snaryad(round(x + width // 2), round(y + height // 2), 8, (0, 0, 255), facing, lastMove))
if keys[pygame.K_LEFT] and x > 1:
x -= speed
player = player_rigth
lastMove = 'left'
elif keys[pygame.K_RIGHT] and x < 450:
x += speed
player = player_left
lastMove = 'right'
elif keys[pygame.K_UP] and y > 1:
y -= speed
player = player_up
lastMove = 'up'
elif keys[pygame.K_DOWN] and y < 450:
y += speed
player = player_down
lastMove = 'down'
win.blit(bg, (0, 0))
drawWindow()
win.blit(player, (x, y))
pygame.display.update()
pygame.quit()
Код можно немного сократить, но с этим думаю сами справитесь
-
Кстати, не рекомендую сильно увлекаться pygame) Захотите что-то стоящее сделать и появятся проблемы с оптимизацией, я так пару месяцев в пустую потратил)– М. Абдул26 мая 2020 в 18:52
-
стрелять сразу вверх, вниз, влево, вправо?
Допустим, у игрока есть одно оружие. Как вы себе представляете, что оно стреляет сразу в 4 стороны?clock = pygame.time.Clock()
), не инициализирован дисплей (pygame.init()
), не определён bullets и win, далее я не смотрел