Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I don't what happened, it was working few minutes ago, but now it's not. It's crashing every time.
import pygame, sys
WIDTH, HEIGHT = 800, 700
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Qix")
clock = pygame.time.Clock()
# Player Class
class Player:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.rect = pygame.Rect(self.x, self.y, 32, 32)
self.color = (250, 120, 60)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4
self.enterPush = False
while self.enterPush is True:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
self.x += self.velX
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def push(self):
if self.rect.x>760 or self.rect.y>560:
self.enterPush = True
def update(self):
self.velX = 0
self.velY = 0
while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
player = Player(WIDTH / 2, HEIGHT / 2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
win.fill((12, 24, 36))
player.draw(win)
player.update()
pygame.display.flip()
clock.tick(60)
pygame.display.update()
I have to fix this in one hour.
I read other answers related to this but nothing seems to work.
Just adding details
Just adding details Just adding details
Just adding details
Just adding details
Just adding details
Here You go, fixed the code:
import pygame
WIDTH, HEIGHT = 800, 700
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Qix")
clock = pygame.time.Clock()
# Player Class
class Player:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.rect = pygame.Rect(self.x, self.y, 32, 32)
self.color = (250, 120, 60)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4
self.enterPush = False
while self.enterPush is True:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
self.x += self.velX
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def push(self):
if self.rect.x>760 or self.rect.y>560:
self.enterPush = True
def update(self):
self.velX = 0
self.velY = 0
# while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
player = Player(WIDTH / 2, HEIGHT / 2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
win.fill((12, 24, 36))
player.draw(win)
player.update()
# pygame.display.flip()
clock.tick(60)
pygame.display.update()
First of about formatting (suggestion according to PEP 8): 1 newline between class method definitions, import like in the code I edited (tho this is acceptable: from module import class, func, etc).
Also I don't see the point of both .flip() and .update() since they basically do the same.
About Your poblem:
while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
this was Your issue: the while loop
solution: not using it:
def update(self):
self.velX = 0
self.velY = 0
# while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
Related
I was programming player movement for my game and I'm not getting errors but the code isn't working correctly. The game is just presenting a black screen, if your able to help, please do that would be greatly appreciated! I've tried resetting my browser and such, but nothings working anyone able to help? (I'm using repl.it)
import pygame, sys
WIDTH, HEIGHT = 400, 400
TITLE = "DarkRoom"
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
class Player:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 32, 32)
self.x = int(x)
self.y = int(y)
self.color = (255, 255, 255)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def update(self):
self.velX = 0
self.velY = 0
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed:
self.velY = -self.speed
if self.down_pressed and not self.up_pressed:
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(self.x, self.y, 32, 32)
player = Player(WIDTH/2, HEIGHT/2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
win.fill((12, 24, 36))
player.draw(win)
player.update()
pygame.display.flip()
clock.tick(120)
It is just a matter of Indentation. You have to draw the player and update the display in the application loop, but not after the application loop:
while True:
for event in pygame.event.get():
# [...]
# INDENTATION
#->|
player.update()
pygame.display.flip()
clock.tick(120)
In my program, when the keys are pressed, the image of the ship is flipped. I decided to bind the direction of the bullets to the flags of the ship's position. But I ran into the problem that all bullets take the direction of the ship when they are already fired. I need to keep the coordinate of the sprite when it is drawn.
import sys
import pygame
from settings import Settings
from rocket import Rocket
from bullet import Bullet
class Cosmos:
"""Главный класс игры Cosmos"""
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('Cosmos')
self.fon_image = pygame.image.load('image/Fon.png')
self.fon = self.fon_image.get_rect()
self.settings = Settings()
self.rocket = Rocket(self.screen, self.settings)
self.bullets = pygame.sprite.Group()
def key_event(self):
"""Обрабатывает нажатие клавиш"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_d:
self.rocket.move_RIGHT = True
elif event.key == pygame.K_a:
self.rocket.move_LEFT = True
elif event.key == pygame.K_w:
self.rocket.move_UP = True
elif event.key == pygame.K_s:
self.rocket.move_DOWN = True
elif event.key == pygame.K_SPACE:
self.fire_bullet()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
self.rocket.move_RIGHT = False
elif event.key == pygame.K_a:
self.rocket.move_LEFT = False
elif event.key == pygame.K_w:
self.rocket.move_UP = False
elif event.key == pygame.K_s:
self.rocket.move_DOWN = False
def fire_bullet(self):
"""Создание нового снаряда и включение его в группу"""
new_bullet = Bullet(self.screen, self.settings, self.rocket)
self.bullets.add(new_bullet)
def run_game(self):
"""Обновляет события игры"""
while True:
self.screen.blit(self.fon_image, self.fon)
self.key_event()
self.rocket.update_rocket()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.bullets.update(bullet)
self.rocket.draw_rocket()
pygame.display.flip()
game = Cosmos()
game.run_game()
from pygame.sprite import Sprite
class Bullet(Sprite):
"""Класс для управления снарядами, выпущенными кораблем"""
def __init__(self, screen, settings, rocket):
super().__init__()
self.screen = screen
self.settings = settings
self.rocket = rocket
self.color = self.settings.bullets_color
self.rect = pygame.Rect(0, 0, self.settings.bullets_width, self.settings.bullets_height)
self.rect.midtop = rocket.rect.midtop
self.y = float(self.rect.y)
self.x = float(self.rect.x)
def update(self, bullet):
"""Перемещение снаряда"""
if self.rocket.image == self.rocket.image_right:
bullet.x += 2
if self.rocket.image == self.rocket.image_left:
bullet.x -= 2
if self.rocket.image == self.rocket.image_up:
bullet.y -= 2
if self.rocket.image == self.rocket.image_down:
bullet.y += 2
self.rect.x = self.x
self.rect.y = self.y
def draw_bullet(self):
"""Рисует снаряд"""
pygame.draw.rect(self.screen, self.color, self.rect)
import pygame
class Rocket:
"""Класс для управления кораблём"""
def __init__(self, screen, settings):
self.screen = screen
self.screen_rect = screen.get_rect()
self.settings = settings
self.image = pygame.image.load('image/pixil-frame-0.png')
self.rect = self.image.get_rect()
self.rect.center = self.screen_rect.center
self.x = float(self.rect.x)
self.y = float(self.rect.y)
self.move_RIGHT = False
self.move_LEFT = False
self.move_UP = False
self.move_DOWN = False
self.image_right = pygame.transform.rotate(self.image, -90)
self.image_left = pygame.transform.rotate(self.image, 90)
self.image_up = pygame.transform.rotate(self.image, 0)
self.image_down = pygame.transform.rotate(self.image, 180)
def update_rocket(self):
"""Флаги управления кораблём"""
if self.move_RIGHT and self.rect.right < self.screen_rect.right:
self.image = self.image_right
self.x += self.settings.rocket_speed
if self.move_LEFT and self.rect.left > self.screen_rect.left:
self.image = self.image_left
self.x -= self.settings.rocket_speed
if self.move_UP and self.rect.top > self.screen_rect.top:
self.image = self.image_up
self.y -= self.settings.rocket_speed
if self.move_DOWN and self.rect.bottom < self.screen_rect.bottom:
self.image = self.image_down
self.y += self.settings.rocket_speed
self.rect.x = self.x
self.rect.y = self.y
def draw_rocket(self):
"""Обновляет позицию корабля"""
self.screen.blit(self.image, self.rect)
The problem here is that in Bullet.update, you use the current image of the rocket to determine in which direction the bullet is going. This means that any change of this image results in a change of direction. Instead of doing this, you should save the current direction upon creating a new Bullet instance and then use this saved direction to determine the direction of the rocket.
A possible implementation:
class Bullet(Sprite):
"""Класс для управления снарядами, выпущенными кораблем"""
def __init__(self, screen, settings, rocket):
super().__init__()
self.screen = screen
self.settings = settings
self.rocket = rocket
self.color = self.settings.bullets_color
self.rect = pygame.Rect(0, 0, self.settings.bullets_width, self.settings.bullets_height)
self.rect.midtop = rocket.rect.midtop
self.y = float(self.rect.y)
self.x = float(self.rect.x)
if self.rocket.image == self.rocket.image_right:
self.direction = "right"
elif self.rocket.image == self.rocket.image_left:
self.direction = "left"
elif self.rocket.image == self.rocket.image_up:
self.direction = "up"
elif self.rocket.image == self.rocket.image_down:
self.direction = "down"
def update(self, bullet):
"""Перемещение снаряда"""
if self.direction == "right":
bullet.x += 2
if self.direction == "left":
bullet.x -= 2
if self.direction == "up":
bullet.y -= 2
if self.direction == "down":
bullet.y += 2
self.rect.x = self.x
self.rect.y = self.y
def draw_bullet(self):
"""Рисует снаряд"""
pygame.draw.rect(self.screen, self.color, self.rect)
When I run my code, it instantly opens then closes the pygame window, however when I delete the player.update() code it works. I can't seem to find what is wrong with the update function in my player class
"""Dot Game"""
#Imports
import pygame, sys
from game import Game
g = Game()
while g.running:
g.curr_menu.display_menu()
g.game_loop()
#Constants
WIDTH, HEIGHT = 1280, 720
TITLE = "Dot."
background = pygame.image.load('BG.png')
background = pygame.transform.scale(background, (WIDTH, HEIGHT))
#pygame initialization
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
#fps
FPS = 60
fpsclock = pygame.time.Clock()
#colors
bgc = (247, 226, 222)
pc = (152, 193, 217)
pc2 = (61, 90, 128) #dark
ec = (119, 2, 26) #dark
ec2 = (220, 66, 73)
#accel
x_change = 0
y_change = 0
accel_x = 0
accel_y = 0
max_speed = 10
display_width, display_height = pygame.display.get_surface().get_size()
x = display_width * 0.45
y = display_height * 0.8
#Player Class
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.x = int(x)
self.y = int(y)
self.radius = 32
self.width = 2
self.color = pc
self.color2 = pc2
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.a_pressed = False
self.d_pressed = False
self.w_pressed = False
self.s_pressed = False
self.attack_animation = False
self.sprites = []
self.sprites.append(pygame.image.load('0000.png'))
self.sprites.append(pygame.image.load('0001.png'))
self.sprites.append(pygame.image.load('0002.png'))
self.sprites.append(pygame.image.load('0003.png'))
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
self.rect = self.image.get_rect()
self.rect.topleft = [x,y]
self.acceleration = 0.4
self.friction = 0.92 # = 1 is no friction
def attack(self):
self.attack_animation = True
def update(self,speed):
if self.attack_animation == True:
self.current_sprite += speed
if int(self.current_sprite) >= len(self.sprites):
self.current_sprite = 1
self.attack_animation = False
self.image = self.sprites[int(self.current_sprite)]
if self.left_pressed:
if self.velX > -max_speed:
self.velX -= self.acceleration
if self.right_pressed:
if self.velX < max_speed:
self.velX += self.acceleration
if self.up_pressed:
if self.velY > -max_speed:
self.velY -= self.acceleration
if self.down_pressed :
if self.velY < max_speed:
self.velY += self.acceleration
self.x += self.velX
self.y += self.velY
self.velX *= self.friction
self.velY *= self.friction
player.attack()
class Enemy:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.radius = 32
self.color = ec
self.color2 = ec2
self.vel = 3
self.hitbox = (self.x -20, self.y -20, 40, 40)
self.points = [(self.x -10, self.y +14), (self.x, self.y -6), (self.x +10, self.y +14)]
self.points2 = [(self.x -20, self.y +20), (self.x, self.y -20), (self.x +20, self.y +20)] # L U R
def draw(self, win):
pygame.draw.polygon(win, self.color, self.points2)
pygame.draw.polygon(win, self.color2, self.points)
self.hitbox = (self.x -20, self.y -20, 40, 40)
#pygame.draw.rect(win, ec2, self.hitbox, 2)
#Player Initialization
moving_sprites = pygame.sprite.Group()
player = Player(WIDTH/2, HEIGHT/2)
enemy = Enemy(WIDTH/2, HEIGHT/2)
moving_sprites.add(player)
#Main Loop
collide = False
while not collide:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
elif event.key == pygame.K_a:
player.left_pressed = True
elif event.key == pygame.K_RIGHT:
player.right_pressed = True
elif event.key == pygame.K_d:
player.right_pressed = True
elif event.key == pygame.K_UP:
player.up_pressed = True
elif event.key == pygame.K_w:
player.up_pressed = True
elif event.key == pygame.K_DOWN:
player.down_pressed = True
elif event.key == pygame.K_s:
player.down_pressed = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
elif event.key == pygame.K_a:
player.left_pressed = False
elif event.key == pygame.K_RIGHT:
player.right_pressed = False
elif event.key == pygame.K_d:
player.right_pressed = False
elif event.key == pygame.K_UP:
player.up_pressed = False
elif event.key == pygame.K_w:
player.up_pressed = False
elif event.key == pygame.K_DOWN:
player.down_pressed = False
elif event.key == pygame.K_s:
player.down_pressed = False
x_change += accel_x # Accelerate.
if abs(x_change) >= max_speed: # If max_speed is exceeded.
# Normalize the x_change and multiply it with the max_speed.
x_change = x_change/abs(x_change) * max_speed
y_change += accel_y # Accelerate.
if abs(y_change) >= max_speed: # If max_speed is exceeded.
# Normalize the x_change and multiply it with the max_speed.
y_change = y_change/abs(y_change) * max_speed
#Draw
win.blit(background,(0,0))
enemy.draw(win)
moving_sprites.draw(win)
#update
#player.update()
pygame.display.update()
moving_sprites.update(0.12)
fpsclock.tick(FPS)
The update method has a speed argument.
def update(self,speed):
Therefore you need to pass the speed to the update method:
player.update()
speed = 1 # just for example
player.update(speed)
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.x = 32
self.y = 32
self.yspeed = 0
self.xspeed = 0
self.image = pg.draw.rect(screen, yellow, (self.x, self.y, tilesize, tilesize))
self.rect = pg.Rect(self.x, self.y, 32, 32)
def moveRight(self):
self.xspeed = 5
self.yspeed = 0
def moveLeft(self):
self.xspeed = -5
self.yspeed = 0
def moveUp(self):
self.xspeed = 0
self.yspeed = -5
def moveDown(self):
self.xspeed = 0
self.yspeed = 5
playing = True
while playing:
p = Player()
for event in pg.event.get():
if event.type == pg.QUIT:
playing = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
p.moveRight()
if event.key == pg.K_a:
p.moveLeft()
if event.key == pg.K_w:
p.moveUp()
if event.key == pg.K_s:
p.moveDown()
p.x += p.xspeed
p.y += p.yspeed
screen.fill(black)
p.__init__()
drawGrid()
pg.display.update()
clock.tick(60)
Confused as to why my player is not moving? Just making a simple game loop where the player continually goes in one direction until pressed otherwise 888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
Big mistake: don't create p = Player() inside while playing because it recreates Player() in every loop so it reset all information.
Create Player() before while
And don't use p.__init__() because it reset informations too.
To draw it you need
screen.blit(p.image, p.rect)
But I see another mistake
self.image = pg.draw.rect(screen, yellow, (self.x, self.y, tilesize, tilesize))
pygame.draw.rect() doesn't create image, it draws directly on screen and returns Rect().
It needs more changes.
EDIT: Code could look like this:
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.x = 32
self.y = 32
self.yspeed = 0
self.xspeed = 0
def draw(self, screen):
pg.draw.rect(screen, yellow, (self.x, self.y, tilesize, tilesize))
def moveRight(self):
self.xspeed = 5
self.yspeed = 0
def moveLeft(self):
self.xspeed = -5
self.yspeed = 0
def moveUp(self):
self.xspeed = 0
self.yspeed = -5
def moveDown(self):
self.xspeed = 0
self.yspeed = 5
p = Player()
playing = True
while playing:
for event in pg.event.get():
if event.type == pg.QUIT:
playing = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
p.moveRight()
if event.key == pg.K_a:
p.moveLeft()
if event.key == pg.K_w:
p.moveUp()
if event.key == pg.K_s:
p.moveDown()
p.x += p.xspeed
p.y += p.yspeed
screen.fill(black)
p.draw()
drawGrid()
pg.display.update()
clock.tick(60)
Hello I am currently working on a survival shooter game and I am currently getting frustrated at a few things. One being my player animation not working. I have the code for the player class right here:
import pygame
from zombie import *
# Player
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, gravity):
# Player dimensions and position
# Player image and animation
self.images = []
self.images.append(pygame.image.load('images/Sprites/player.png'))
self.images.append(pygame.image.load('images/Sprites/player2.png'))
#~ self.images.append(pygame.image.load('ball1.png'))
#~ self.images.append(pygame.image.load('ball2.png'))
self.maxImage = len(self.images)
self.currentImage = 0
#~ self.rect = pygame.Rect(x, y, 80, 80)
self.rect = self.images[0].get_rect()
self.rect.x = x
self.rect.y = y
self.timeTarget = 10
self.timeNum = 1
self.velX = 0
self.velY = 0
self.health = 100
# Jump and gravity
self.jumping = False
self.on_ground = False
self.origJumpVel = 15
self.jumpVel = self.origJumpVel
self.gravity = 0.5
# Jump inputs
def do_jump(self):
if self.jumping and not self.on_ground:
self.velY = -self.jumpVel
self.jumpVel -= self.gravity
if self.on_ground:
self.jumping = False
self.jumpVel = self.origJumpVel
self.velY = 0
self.on_ground = True
def handle_events(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if not self.jumping:
self.jumping = True
self.on_ground = False
elif event.key == pygame.K_a:
self.velX = -5
elif event.key == pygame.K_d:
self.velX = +5
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_a, pygame.K_d):
self.velX = 0
# PLayer updates
def update(self):
# Animations
if self.timeNum == self.timeTarget:
self.currentImage += 1
if self.currentImage >= self.maxImage:
self.currentImage = 0
self.timeNum = 1
# Health
#if self.rect.x == zombie.rect.x:
#self.health -= 10
# Screen wrap
if self.rect.right > 1280:
self.rect.left = 0
elif self.rect.left < 0:
self.rect.right = 1280
# Player rendering
def render(self, surface):
surface.blit(self.images[self.currentImage], self.rect)
If you want to know about any of my other problems/frustrations just say so. Any help will be greatly appreciated. Thank you.
You have
if self.timeNum == self.timeTarget:
# ...
self.timeNum += 1
but self.timeNum is 1 and self.timeTarget is 10 so it can't be run and it can't change self.timeNum
You probably need
if self.timeNum == self.timeTarget:
# ...
# inside of `if`
self.timeNum = 0
# outside of `if`
self.timeNum += 1