Animation Loop not working when button is held down - python

I've been working with Pygame and I encountered this problem.
I have made 2 animation lists. One called rightImages which contains images of my character walking to the right, and one called leftImages which is the opposite. The animation works when I press the D key over (My program's controls are A is left, D is right, etc) and over again, but when I hold it down the animations do not run. Hopefully, you understand and if you don't please run the program and then maybe you will get what I am saying. Here is my code:
def Main():
x_change = 0
y_change = 0
x = 400
y = 400
counter = 0
counter2 = 0
player = pygame.image.load('knight_left.png')
while True:
rightImages = ['knight_right.png','knight_right1.png','knight_right2.png']
leftImages =['knight_left.png', 'knight_left1.png', 'knight_left2.png']
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change += 5
counter += 1
if counter >= len(rightImages):
counter = 0
player = pygame.image.load(rightImages[counter])
elif event.key == pygame.K_a:
x_change -= 5
counter2 += 1
if counter2 >= len(leftImages):
counter2 = 0
player = pygame.image.load(leftImages[counter2])
elif event.key == pygame.K_w:
y_change -= 5
elif event.key == pygame.K_s:
y_change += 5
elif event.type == pygame.KEYUP:
x_change = 0
y_change = 0
x = x+x_change
y = y+y_change
gameDisplay.fill(white)
gameDisplay.blit(player,(x,y))
pygame.display.update()
clock.tick(30)
Main()
This is the code that runs through the animation loop when walking to the right.
counter += 1
if counter >= len(rightImages):
counter = 0
player = pygame.image.load(rightImages[counter])
This is for when walking to the left.
counter2 += 1
if counter2 >= len(rightImages):
counter2 = 0
player = pygame.image.load(rightImages[counter2])
If you could please tell me how to cycle through the animation lists even when the key is held down that would be awesome!
Note: I did not include all of my code.

I usually do something similar to this: First load the images before the main loop starts, because reading from the hard disk is slow.
Assign the currently selected images to a variable active_images and swap them out if the player presses a key, e.g. active_images = right_images.
In the main loop you just have to check if the player moves and then increment the counter (or a timer variable) and assign the current image to the player variable: player = active_images[counter].
import pygame as pg
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
white = pg.Color('white')
x_change = 0
y_change = 0
x = 400
y = 400
# Load the images before the main loop starts. You could also
# do this in the global scope.
right_images = []
for img_name in ['knight_right.png','knight_right1.png','knight_right2.png']:
right_images.append(pg.image.load(img_name).convert_alpha())
left_images = []
for img_name in ['knight_left.png', 'knight_left1.png', 'knight_left2.png']:
left_images.append(pg.image.load(img_name).convert_alpha())
# This variable refers to the currently selected image list.
active_images = left_images
counter = 0 # There's no need for a second counter.
player = active_images[counter] # Currently active animation frame.
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
x_change = 5
active_images = right_images # Swap the image list.
elif event.key == pg.K_a:
x_change = -5
active_images = left_images # Swap the image list.
elif event.key == pg.K_w:
y_change -= 5
elif event.key == pg.K_s:
y_change += 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and x_change > 0:
x_change = 0
counter = 0 # Switch to idle pose.
player = active_images[counter]
elif event.key == pg.K_a and x_change < 0:
x_change = 0
counter = 0 # Switch to idle pose.
player = active_images[counter]
elif event.key == pg.K_w and y_change < 0:
y_change = 0
elif event.key == pg.K_s and y_change > 0:
y_change = 0
if x_change != 0: # If moving.
# Increment the counter and keep it in the correct range.
counter = (counter+1) % len(active_images)
player = active_images[counter] # Swap the image.
x += x_change
y += y_change
screen.fill(white)
screen.blit(player, (x, y))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()

Try the following code.
def Main():
x = 400
y = 400
counter = 0
counter2 = 0
player = pygame.image.load('knight_left.png')
rightImages = ['knight_right.png','knight_right1.png','knight_right2.png']
leftImages =['knight_left.png', 'knight_left1.png', 'knight_left2.png']
while True:
x_change = 0
y_change = 0
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_d]:
x_change += 5
counter += 1
if counter >= len(rightImages):
counter = 0
player = pygame.image.load(rightImages[counter])
if keys[pygame.K_a]:
x_change -= 5
counter2 += 1
if counter2 >= len(leftImages):
counter2 = 0
player = pygame.image.load(leftImages[counter2])
x = x+x_change
gameDisplay.fill(white)
gameDisplay.blit(player,(x,y))
pygame.display.update()
clock.tick(30)
Main()

Related

-805306369 (0xCFFFFFFF) error in python what can i do

I'm quite new to python and have recently started game dev with pygame. I wanted to know why my game just freezes and the exitcode -805306369 (0xCFFFFFFF) appears. Do i have errors in my programm? It looks like this:
import pygame
import random
import math
pygame.init()
window = pygame.display.set_mode((1000, 600))
caption = pygame.display.set_caption(
'Test your reaction speed. Shoot the target game!') # sets a caption for the window
run = True
PlayerImg = pygame.image.load('F:\PythonPortable\oscn.png')
PlayerX = 370
PlayerY = 420
PlayerX_change = 0
PlayerY_change = 0
def player():
window.blit(PlayerImg, (PlayerX, PlayerY))
aim_sight = pygame.image.load('F:\PythonPortable\ktarget.png')
aim_sightX = 460
aim_sightY = 300
aim_sight_changeX = 0
aim_sight_changeY = 0
def aim_sight_function(x, y):
window.blit(aim_sight, (x, y))
targetedImg = pygame.image.load('F:\PythonPortable\ktargetedperson.png')
targetedX = random.randint(0, 872)
targetedY = random.randint(0, 200)
def random_target():
window.blit(targetedImg, (targetedX, targetedY))
def iscollision(targetedX, targetedY, aim_sightX, aim_sightY):
distance = math.sqrt((math.pow(targetedX - aim_sightX, 2)) + (math.pow(targetedY - aim_sightY, 2)))
if distance < 70:
return True
else:
return False
while run:
window.fill((255, 255, 255))
random_target()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
aim_sight_changeX = -2
PlayerX_change = -2
elif event.key == pygame.K_RIGHT:
aim_sight_changeX = 2
PlayerX_change = 2
elif event.key == pygame.K_UP:
aim_sight_changeY = -2
PlayerY_change = -2
elif event.key == pygame.K_DOWN:
aim_sight_changeY = 2
PlayerY_change = 2
score = 0
while score < 10:
collision = iscollision(targetedX, targetedY, aim_sightX, aim_sightY)
if collision and event.key == pygame.K_SPACE:
print("HIT") # Just for me to acknowledge that collision is true and space bar was pressed in the right spot
score = score + 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
aim_sight_changeX = 0
PlayerX_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
aim_sight_changeY = 0
PlayerY_change = 0
aim_sightX += aim_sight_changeX
if aim_sightX <= 46.5:
aim_sight_changeX = 0
elif aim_sightX >= 936:
aim_sight_changeX = 0
aim_sightY += aim_sight_changeY
if aim_sightY <= 0:
aim_sight_changeY = 0
elif aim_sightY >= 400:
aim_sight_changeY = 0
PlayerX += PlayerX_change
if PlayerX <= -50:
PlayerX_change = 0
elif PlayerX >= 850:
PlayerX_change = 0
player()
aim_sight_function(aim_sightX, aim_sightY)
pygame.display.update()
I think there is a mistake in this area as my program runs well without it.
while score < 10:
collision = iscollision(targetedX, targetedY, aim_sightX, aim_sightY)
if collision and event.key == pygame.K_SPACE:
print("HIT") # Just for me to acknowledge that collision is true and space bar was pressed in the right spot
score = score + 1
I've checked other questions but they are mostly for java and other languages.
You have an application loop. You do not need an additional loop to control the game. The loop that increments the score is an infinite loop. Change the loop to a selection (change while to if).
Furthermore, the score has to be initialized before the application loop:
score = 0
while run:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
aim_sight_changeX = -2
PlayerX_change = -2
elif event.key == pygame.K_RIGHT:
# [...]
elif event.key == pygame.K_SPACE:
if score < 10:
collision = iscollision(targetedX, targetedY, aim_sightX, aim_sightY)
if collision:
print("HIT")
score = score + 1

Adding boundaries in Pygame with movable characters/images

I have been creating a game where an image moves according to player input with Keydown and Keyup methods. I want to add boundaries so that the user cannot move the image/character out of the display (I dont want a game over kind of thing if boundary is hit, just that the image/character wont be able to move past that boundary)
import pygame
pygame.init()#initiate pygame
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
display_width = 1200
display_height = 800
display = pygame.display.set_mode((display_width,display_height))
characterimg_left = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg_left.png')
characterimg_right = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg_right.png')
characterimg = characterimg_left
def soldier(x,y):
display.blit(characterimg, (x,y))
x = (display_width * 0.30)
y = (display_height * 0.2)
pygame.display.set_caption('No U')
clock = pygame.time.Clock()#game clock
flip_right = False
x_change = 0
y_change = 0
bg_x = 0
start = True
bg = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/bg.png').convert()
class player:
def __init__(self, x, y):
self.jumping = False
p = player(x, y)
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
start = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change += -4
if flip_right == True:
characterimg = characterimg_left
flip_right = False
x += -150
elif event.key == pygame.K_RIGHT:
x_change += 4
if flip_right == False:
characterimg = characterimg_right
flip_right = True
x += 150
elif event.key == pygame.K_UP:
y_change += -4
elif event.key == pygame.K_DOWN:
y_change += 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change += 4
elif event.key == pygame.K_RIGHT:
x_change += -4
elif event.key == pygame.K_UP:
y_change += 4
elif event.key == pygame.K_DOWN:
y_change += -4
x += x_change
y += y_change
display.fill(white)
soldier(x,y)
pygame.display.update()
clock.tick(120)#fps
pygame.quit()
I have tried several times including switching to the key pressed method but they all failed. Help please, thank you.
Basically you want to limit the player's movement.
So everytime you want to "move" the player (I'm guessing this is "x_change" / "y_change") you need to check whether they would still be inside your boundaries after the move.
Example: Your display x boundary is 0 pixels on the left of your screen and 500 to the right. I only allow the actual movement if the result of the movement is within my boundaries.
boundary_x_lower = 0
boundary_x_upper = 500
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if boundary_x_lower < (x_change - 4):
# I allow the movement if I'm still above the lower boundary after the move.
x_change -= 4
elif event.key == pygame.K_RIGHT:
if boundary_x_upper > (x_change +4):
# I allow the movement if I'm still below the upper boundary after the move.
x_change += 4
PS: I am confused by your code as you subtract when you move to the right... I am used to 2D games where you increment the player's position if you move to the right... and subtract if you go to the left.
Feel free to adapt the code to fit your project. The basic principle applies also to the y-axis movement: with boundary_y_lower & _y_upper. if you have further questions, just ask!
Just clamp the x and y values between 0 and the display width and height.
# In the main while loop after the movement.
if x < 0:
x = 0
elif x + image_width > display_width:
x = display_width - image_width
if y < 0:
y = 0
elif y + image_height > display_height:
y = display_height - image_height
I also recommend checking out how pygame.Rects work. You could define a rect with the size of the display,
display_rect = display.get_rect()
and a rect for the character which will be used as the blit position:
rect = characterimg_left.get_rect(center=(x, y))
Then move and clamp the rect in this way:
rect.move_ip((x_change, y_change))
rect.clamp_ip(display_rect)
display.fill(white)
# Blit the image at the `rect.topleft` coordinates.
display.blit(characterimg, rect)

List index is out of range and I do not understand why?

I've been getting the error :
line 102, in <module>
shot[2] += shoty
IndexError: list index out of range
In my code where I am trying to code a dagger to be thrown, and deleted if it is out of bounds. I understand what the list error means however; I am unsure of how it is affecting my code and any help would be greatly appreciated!
This is the part of my code which is relevant to the problem:
daggers = []
daggerPlayer = pygame.image.load(daggerImage.png)
for shot in daggers:
index = 0
shotx = math.cos(shot[0])*10
shoty = math.sin(shot[0])*10
shot[1] += shotx
shot[2] += shoty
if (shot[1] < -64) or (shot[1] > 900) or (shot[2] < -64) or (shot[2]> 600):
daggers.pop(index)
index +=1
for shoot in daggers:
daggerOne = pygame.transform.rotate(daggerPlayer, 360 - shoot[0]*57.29)
screen.blit(daggerOne, (shoot[1], shoot[2]))
EDIT: Here is the entirety of my code
#Load Images
peterPlayer = pygame.image.load('pixelPirateOne.png)'
nightBackground = pygame.image.load ('NightSky.png')
daggerPlayer = pygame.image.load('daggerImage.png')
#Settting Variables for Moving Character
xPlayer = 200
yPlayer = 275
dxPlayer = 0
dyPlayer = 0
playerPosition = (200,275)
accuracyShot = [0,0]
daggers = []
def quitGame():
pygame.quit()
sys.exit()
go = True
while go:
#Blit The Background
screen.blit(nightBackground, (0,0))
#Learned about atan2 from --> https://docs.python.org/2/library/math.html
#Allows To Rotate Player With Mouse
mousePosition = pygame.mouse.get_pos()
angle = math.atan2(mousePosition[1]-(yPlayer+32),mousePosition[0]-(xPlayer+26))
playerRotate = pygame.transform.rotate(peterPlayer, 360-angle*57.29)
playerPositionNew = (xPlayer-playerRotate.get_rect().width/2, yPlayer-playerRotate.get_rect().height/2)
screen.blit(playerRotate, playerPositionNew)
pygame.display.update()
#Quit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitGame()
#Move Player- Note: Make this portion fluid; it's choppy as of now
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dxPlayer -= 15
elif event.key == pygame.K_RIGHT:
dxPlayer += 15
elif event.key == pygame.K_UP:
dyPlayer -= 15
elif event.key == pygame.K_DOWN:
dyPlayer += 15
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
dxPlayer = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
dyPlayer = 0
xPlayer = xPlayer + dxPlayer
yPlayer = yPlayer + dyPlayer
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
mousePosition = pygame.mouse.get_pos()
accuracyShot[1] += 1
daggers.append([math.atan2(mousePosition[1]-(playerPositionNew[1]+32), mousePosition[0]-(playerPositionNew[0]+26)), playerPositionNew[1]+32])
#Learned about cos and sin in python from --> https://docs.python.org/2/library/math.html
#Learned about .pop from --> https://docs.python.org/2/tutorial/datastructures.html
for shot in daggers:
index = 0
shotx = math.cos(shot[0])*10
shoty = math.sin(shot[0])*10
shot[1] += shotx
shot[2] += shoty
if (shot[1] < -64) or (shot[1] > 900) or (shot[2] < -64) or (shot[2]> 600):
daggers.pop(index)
index +=1
for shoot in daggers:
daggerOne = pygame.transform.rotate(daggerPlayer, 360 - shoot[0]*57.29)
screen.blit(daggerOne, (shoot[1], shoot[2]))
The list you append to daggers has only two elements, but by calling shot[2] you are trying to access the third, that's why you get that error. The two elements should be shot[0] and shot[1]. I believe you make the same mistake in screen.blit(daggerOne, (shoot[1], shoot[2])) as well.

Detecting whether the player has collided w/ the bottom of an object (rectangles)

I'm trying to create a bird's-eye-view game (e.g. hotline miami) in pygame. I've gotten stuck at the collision detection. I'm trying to to see if the player hits a block on the left, right, in front or behind . I've gotten only to doing the 'in front' collision but that doesn't work; the player will hit the block 'head first', stop, but is then able to move through the block if he spams the arrow key. I know what the problem is (in the if else statement in the collision detection) but I can't for the life of me find the solution. Everything I tried doesn't seem to work. Help! Any help would be greatly appreciated, thanks in advance! (btw this is my first time on stack overflow, so sorry if I'm not asking the question well) (I removed some of the unnecessary code like the pygame.init() and variables)
def drawBlock(block_width,block_height,x,y):
pygame.draw.rect(gameDisplay, red, [x,y,block_width,block_height])
def gameLoop(x,y):
while not gameExit:
level = ["W","W","W","W","W","W","W","W","W","W","W","W",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","P","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","P","N","N","L",
"N","N","N","N","P","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L","S"]
#EVENT HANDLING
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if canMoveUp:
if event.key == pygame.K_UP:
y_change = -player_movement
if canMoveDown:
if event.key == pygame.K_DOWN:
y_change = player_movement
if event.key == pygame.K_LEFT:
x_change = -player_movement
if event.key == pygame.K_RIGHT:
x_change = player_movement
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
pX += x_change
pY += y_change
gameDisplay.fill(black)
#CALCULATING THE SIZE OF THE BLOCKS SO THEY FIT W/ A VARIETY OF RESOLUTIONS
if calculateBlockX:
for element in level:
if element == "W":
W_count += 1
if element == "S":
block_width = (display_width/W_count)
calculateBlockX = False
if calculateBlockY:
for element in level:
if element == "L":
L_count += 1
if element == "S":
block_height = (display_height/L_count)
calculateBlockY = False
#COUNTING HOW MANY BLOCKS THERE ARE IN THE LEVEL ARRAY (P)
if P_countFunction:
for element in level:
if element == "P":
P_count += 1
if element == "S":
print(P_count)
P_countFunction = False
#ALL THE X AND Ys OF ALL THE BLOCKS IN THE LEVEL, AND ACTUALLY DRAWING THEM
blockXY = []
for element in level:
if element == "N":
x += block_width
if element == "L":
y += block_height
x = 0
if element == "P":
drawBlock(block_width,block_height,x,y)
blockXY.append(x)
blockXY.append(y)
if appendBlockXY:
if len(collisionArray) > P_count:
del(collisionArray[P_count])
print(collisionArray)
appendBlockXY = False
collisionArray.append(blockXY)
blockXY = []
x += block_width
if element == "S":
y = 0
#COLLISION DETECTION
for XnY in collisionArray:
if pX >= XnY[0] and pX <= XnY[0] + block_width and pY + playerSize <= XnY[1] or pX + playerSize >= XnY[0] and pX <= XnY[0] + block_width:
if pY - block_height == XnY[1]:
canMoveUp = False
y_change = 0
if pY - block_height != XnY[1]:
canMoveUp = True
else:
if pY - block_height >= XnY[1]:
canMoveUp = True
#gameDisplay.blit(img,[pX,pY])
pygame.draw.rect(gameDisplay, green, [pX,pY,playerSize,playerSize])
clock.tick(60)
pygame.display.update()
pygame.quit()
quit()
gameLoop(x,y)
pygame.display.update()
Your collision detection is written in a way that allows the last iteration to override all earlier checks. Instead of that, have canMoveUp set to True before the loop, and the loop should only set it to False or leave it alone.

What is the best way to create multiple bullets in Python using Pygame?

I just started studying Python two days ago, so my code is sloppy...But I'm just trying to get something that works. I'm working on a simple shmup. I'm trying to get bullets to fire by holding down on the Z key...The problem I'm facing at the moment is getting multiple bullets to work correctly. No matter what I do, after five are shot, I can't shoot anymore. When I only had one bullet, I couldn't shoot it anymore if I was still holding down the Z key when the object state changed to remove it. Here's my code...
#!/usr/bin/python
import sys, pygame
pygame.init()
windowSize = width, height = 640, 480
screen = pygame.display.set_mode((windowSize))
pygame.display.set_caption("rzrscm")
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background = background.convert()
image = pygame.image.load("image.png")
Font = pygame.font.Font("font.ttf",12)
text = Font.render("PIXELFUCKER",1,(255,255,255))
textpos = text.get_rect(centerx=background.get_width()/2)
pygame.mixer.music.load("music.xm")
pygame.mixer.init(44100, -16, 1, 1024)
pygame.mixer.music.play(-1,0.0)
quit = False
eX = 0
eY = 50
dotState = 1
bullet1 = 0
bullet2 = 0
bullet3 = 0
bullet4 = 0
bullet5 = 0
shot = 0
wait = 0
x = 300
y = 300
pX = 0
pY = 0
while quit == False:
background.fill((0,0,0))
x += pX
if x < 0 or x > 640:
x -= pX
if x == eX and y == eY:
x -= pX
y += pY
if y < 0 or y > 480:
y -= pY
wait = wait + 1
if shot == 1:
if bullet1 == 0 and bullet5 == 0:
bullet1 = 1
wait = 0
if bullet1 == 1 and bullet2 == 0 and wait == 25:
bullet2 = 1
wait = 0
if bullet2 == 1 and bullet3 == 0 and wait == 25:
bullet3 = 1
wait = 0
if bullet3 == 1 and bullet4 == 0 and wait == 25:
bullet4 = 1
wait = 0
if bullet4 == 1 and bullet5 == 0 and wait == 25:
bullet5 = 1
wait = 0
if dotState != 3:
background.set_at((eX, eY),(255,255,255))
if eX == 640:
dotState = 2
if eX == 0:
dotState = 1
if dotState == 1:
eX = eX + 1
if eX == x and eY == y:
eX = eX - 1
if dotState == 2:
eX = eX - 1
if eX == x and eY == y:
eX = eX + 1
if bullet1 == 0:
bX = x
bY = y
if bullet1 == 1:
bY = bY - 5
background.set_at((bX, bY),(255,255,255))
if bY == 0:
bullet1 = 0
if bY == eY and bX == eX:
bullet1 = 0
dotState = 3
if bullet2 == 0:
bX2 = x
bY2 = y
if bullet2 == 1:
bY2 = bY2 - 5
background.set_at((bX2, bY2),(255,255,255))
if bY2 == 0:
bullet2 = 0
if bY2 == eY and bX2 == eX:
bullet2 = 0
if bullet3 == 0:
bX3 = x
bY3 = y
if bullet3 == 1:
bY3 = bY3 - 5
background.set_at((bX3, bY3),(255,255,255))
if bY3 == 0:
bullet3 = 0
if bY3 == eY and bX3 == eX:
bullet3 = 0
dotState = 3
if bullet4 == 0:
bX4 = x
bY4 = y
if bullet4 == 1:
bY4 = bY4 - 5
background.set_at((bX4, bY4),(255,255,255))
if bY4 == 0:
bullet4 = 0
if bY4 == eY and bX4 == eX:
bullet4 = 0
dotState = 3
if bullet5 == 0:
bX5 = x
bY5 = y
if bullet5 == 1:
bY5 = bY5 - 5
background.set_at((bX5, bY5),(255,255,255))
if bY5 == 0:
bullet5 = 0
if bY5 == eY and bX5 == eX:
bullet5 = 0
dotState = 3
background.set_at((x, y),(255,255,255))
background.blit(text,textpos)
screen.blit(background,(0,0))
pygame.display.flip()
clock.tick(250)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
pX -= 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
pX += 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
pY -= 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
pY += 2
if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
pX += 2
if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
pX -= 2
if event.type == pygame.KEYUP and event.key == pygame.K_UP:
pY += 2
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
pY -= 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_z:
shot = 1
if event.type == pygame.KEYUP and event.key == pygame.K_z:
shot = 0
Your code was a little hard to follow for me -- I'm fairly new to programming myself. However, I do have a few suggestions that might help a little.
First, you seem to be hardcoding in the coordinates of your bullets (ie bullet1, bullet2, and bX1, bY1 etc.). Because you seem to be unable to fire more then 5 bullets, I'm assuming that you're not resetting your bullets' x and y coordinates, along with other variables related to them to zero after it moves off the screen or something.
Also, try implementing your bullets as a class and put all your bullets in a list so you can have more then 5. This way, you can neatly encapsulate all the data you need for a single bullet so you can do things like bullet1.x = 3 or bullets_array[1].y = 3 instead of bX1 = 3.
(btw, I see you started learning Python just recently. I highly suggest you learn about lists, objects, and object-oriented programming (often abbreviated OOP). And dictionaries. They'll be your new best friends. OOP might be a little difficult to grok at first (at least, it was for me), but it'll be worth it.)
For example,
# snip initialization, etc.
class Bullet():
def __init__(self, surface, x_coord, y_coord):
self.surface = surface
self.x = x_coord
self.y = y_coord
return
def update(self, y_amount=5):
self.y += y_amount
self.surface.set_at((self.x, self.y),(255,255,255))
return
bullets_array = []
# snip
while quit == false: # Inside the main loop...
for event in pygame.event.get():
#snip
if event.type == pygame.KEYDOWN and event.key == pygame.K_z:
bullets_array.append(background, player_x, player_y)
#snip
for bullet in bullets_array:
bullet.update()
# ...and check if the bullet is off the screen.
# If so, remove it from the array.
If you want something a bit more complicated, try using Pygame's Sprite and Group classes.
http://pygame.org/docs/ref/sprite.html
Basically, instead of creating the Bullet class yourself, you base it on pygame.sprite.Sprite, implement whatever methods you need, then add it to a group (pygame.sprite.Group). That way would probably be more flexible.

Categories

Resources