so I was programming with Python, specifically Pygame. I came across a few problems, which I have fixed. But my latest problem, is that when I ran the game, the window is only black. when
My code so far is this:
import pgzrun
#import pgzero
from pgzero.builtins import Actor, animate, keyboard
import random as r
# settings
TITLE = "Tonk Game"
WIDTH = 800
HEIGHT = 600
enemyAmount = 5
wallpercent= 50
# actors
tank = Actor('tank_green.png')
tank.angle = 90
# variables
bulletH = 0
# bullets
bullets = []
# game variables
gameWin = False
gameLose = False
gameStart = False
# walls + enemy tank
walls = []
enemies = []
for i in range(enemyAmount):
enemy = Actor('tank_red.png')
enemy.x = i*200-10
enemy.y = 25
enemy.angle = 270
enemy.move_count = 0
enemies.append(enemy)
# explosion particle
explosion = Actor('explosion.png')
explosion.pos = 900, 700
for x in range(16):
for y in range(10):
if r.randint(0, 100) < wallpercent:
wall = Actor('wall.jpeg')
wall.x = x*50+25
wall.y = y*50+25+50
walls.append(wall)
# actors pos
tank.pos = 400, 575
# enemy bullets
enBullets = []
#buttons for main menu (settings)
#these are the button for enemy amount adjustment
diff1= Actor('tank_red.png')
diff2= Actor('tank_red.png')
diff3= Actor('tank_red.png')
diff1.pos=200,400
diff2.pos=400,400
diff3.pos=600,400
diff1.angle=270
diff2.angle=270
diff3.angle=270
#now for wall amount
wol1= Actor('wall.jpeg')
wol2= Actor('wall.jpeg')
wol3= Actor('wall.jpeg')
wol1.pos=200,200
wol2.pos=400,200
wol3.pos=600,200
#this is for the start button
start= Actor('bowl.png')
start.pos= 400,560
start.angle=180
# draw def
def draw():
screen.clear()
# these only happen when the game has started
if gameStart:
screen.blit('grass.png', (0, 0))
for wall in walls:
wall.draw()
tank.draw()
explosion.draw()
for enemy in enemies:
enemy.draw()
for bullet in bullets:
bullet.draw()
for enBullet in enBullets:
enBullet.draw()
screen.draw.text("Enemies remaining: " +str(len(enemies)), topleft=(10, 10))
if gameWin:
screen.fill('black')
screen.draw.text("You Win!", fontsize=80, topleft=(280, 250))
elif gameLose:
screen.fill('black')
screen.draw.text("You Lose!", fontsize=80, topleft=(280, 250))
while gameStart!= True:
screen.fill('black')
#settings title
screen.draw.text("Settings",topleft=(370, 50))
#for the enemy amount
diff1.draw()
diff2.draw()
diff3.draw()
screen.draw.text("3 Enemies",topleft=(160, 360))
screen.draw.text("5 Enemies",topleft=(360, 360))
screen.draw.text("8 Enemies",topleft=(560, 360))
#wall amount
wol1.draw()
wol2.draw()
wol3.draw()
screen.draw.text("25%",topleft=(185, 190))
screen.draw.text("50%",topleft=(385, 190))
screen.draw.text("75%",topleft=(585, 190))
#start button
start.draw()
screen.draw.text("Start",topleft=(382, 555))
def on_mouse_down(pos):
global enemyAmount
global wallpercent
#enemy amount setting
if diff1.collidepoint(pos):
enemyAmount= 3
print(enemyAmount)
elif diff2.collidepoint(pos):
enemyAmount= 5
print(enemyAmount)
elif diff3.collidepoint(pos):
enemyAmount= 8
print(enemyAmount)
#wall amount setting
if wol1.collidepoint(pos):
wallpercent= 75
print(wallpercent)
elif wol2.collidepoint(pos):
wallpercent= 50
print(wallpercent)
elif wol3.collidepoint(pos):
wallpercent=25
print(wallpercent)
#start button
if start.collidepoint(pos):
startGame= True
# update
def update():
global bullet
global tank
global bulletH
global enemy
global gameWin
global gameLose
global enemyAmount
# save original pos tank and enemy tank
orix = tank.x
oriy = tank.y
orinemx = enemy.x
orinemy = enemy.y
# thes ehappen on game start
while gameStart:
# arrow keys/wasd movement
if keyboard.right or keyboard.d:
tank.x = tank.x + 7
tank.angle = 0
elif keyboard.left or keyboard.a:
tank.x = tank.x - 7
tank.angle = 180
elif keyboard.up or keyboard.w:
tank.y = tank.y - 7
tank.angle = 90
elif keyboard.down or keyboard.s:
tank.y = tank.y + 7
tank.angle = 270
# tank (and enemy tank) go to original pos
if tank.collidelist(walls) != -1:
tank.pos = orix, oriy
# stop tanks from going outside
if tank.x > 800 or tank.x < 0 or tank.y > 600 or tank.y < 0:
tank.x = orix
tank.y = oriy
if bulletH == 0:
# bullet code
if keyboard.space:
bullet = Actor('bulletblue.png')
bullet.angle = tank.angle
bullet.x = tank.x
bullet.y = tank.y
bullets.append(bullet)
bulletH = 100
else:
bulletH -= 1
# bullet.angle=tank.angle
# move bullet
for bullet in bullets:
if bullet.angle == 0:
bullet.x += 9
elif bullet.angle == 180:
bullet.x -= 9
elif bullet.angle == 90:
bullet.y -= 9
elif bullet.angle == 270:
bullet.y += 9
# destroy walls
for bullet in bullets:
wallInd = bullet.collidelist(walls)
if wallInd != -1:
del walls[wallInd]
bullets.remove(bullet)
enInd = bullet.collidelist(enemies)
if enInd != -1:
explosion.pos = enemies[enInd].x, enemies[enInd].y
del enemies[enInd]
bullets.remove(bullet)
print("1 Enemy has died. Enemy remaining:"+str(enemyAmount))
enemyAmount -= 1
# player dies if hit with enemy bullet
# enemy bullet destroying walls
for enBullet in enBullets:
wallInd = enBullet.collidelist(walls)
if wallInd != -1:
del walls[wallInd]
enBullets.remove(enBullet)
# enemy movement
for enemy in enemies:
choice = r.randint(0, 2)
if enemy.move_count > 0:
orinemx = enemy.x
orinemy = enemy.y
enemy.move_count -= 1
if enemy.angle == 0:
enemy.x += 9
elif enemy.angle == 180:
enemy.x -= 9
elif enemy.angle == 90:
enemy.y -= 9
elif enemy.angle == 270:
enemy.y += 9
if enemy.x > 800 or enemy.x < 0 or enemy.y > 600 or enemy.y < 0:
enemy.x = orinemx
enemy.y = orinemy
elif enemy.collidelist(walls) != -1:
enemy.pos = orinemx, orinemy
elif choice == 0:
enemy.move_count = 20
print("move")
elif choice == 1:
enemy.angle = r.randint(0, 3)*90
print("turn", enemy.angle)
else:
enBullet = Actor('bulletred.png')
enBullet.angle = enemy.angle
enBullet.x = enemy.x
enBullet.y = enemy.y
enBullets.append(enBullet)
print("shoot")
for enBullet in enBullets:
if enBullet.angle == 0:
enBullet.x += 12
elif enBullet.angle == 180:
enBullet.x -= 12
elif enBullet.angle == 90:
enBullet.y -= 12
elif enBullet.angle == 270:
enBullet.y += 12
if enBullet.colliderect(tank):
gameLose = True
# turn on and off the gameWin variable
if enemyAmount == 0:
gameWin = True
pgzrun.go()
I have tried looking everywhere else, but nothing seems to be working. All the solutions I found are only applicable to a Pygame, whilst I am using Pygame Zero.