I am making a game in pygame for the first time and every time I click W, Up, or Space (Which I set to make the character jump) it closes the game. I don't know if I accidentally made it close the game or something I'm new to Python and Pygame. I followed a bit off a tutorial so maybe the tutorial was wrong or out of date. Does anyone know what's wrong?
import pygame
import random
import time
GRAY = (192, 192, 192)
RED = (255, 0, 0)
class Player(pygame.sprite.Sprite):
def __init__(self, color, x, y, width, weight):
super().__init__()
self.color = color
self.image = pygame.Surface([width, weight])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
self.rect.x += 5
if self.rect.x >= 950:
self.rect.x = 0
#main
def jump(self):
x = 363
self.rect.x =+ 50
x = 413
color_line = (0,0,0)
pygame.init()
screen = pygame.display.set_mode([1000, 500])
all_sprites_list = pygame.sprite.Group()
player_width = 50
player_weight = 50
player = Player(RED, 50, 413, 50, 50)
all_sprites_list.add(player)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#clicks
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
jump()
if keys[pygame.K_w]:
jump()
if keys[pygame.K_SPACE]:
jump()
#updates
all_sprites_list.update()
#draws
screen.fill((255, 255, 255))
pygame.draw.line(screen, color_line, (0, 500), (1000, 500), 75)
all_sprites_list.draw(screen)
pygame.display.flip()
clock.tick(30)
pygame.quit()
Well, your jump( self )
function should have been indented inside the player class. That was the main thing. Initialization values placed inside __init__
; made more sense there. Looked at this for reference.
https://github.com/AfoninZ/pybird/blob/6c92b68f196a32e23d77fda17c603c6e1200b1bb/flappybird.py
#! /usr/bin/env python3
## pip3 install pygame
## python3 -m pip install pygame
import pygame
import random
import time
pygame .init()
screensize = Width, Height = 1000, 500
screen = pygame .display .set_mode( screensize )
all_sprites_list = pygame .sprite .Group()
clock = pygame .time .Clock()
WHITE = 255, 255, 255
GRAY = 192, 192, 192
RED = 255, 0, 0
BLACK = 0, 0, 0
GROUND = 463
class Player( pygame .sprite .Sprite ):
def __init__( self, group ):
super() .__init__( group )
self .color = RED
self .width = 50
self .height = 50
self .image = pygame .Surface( [self .width, self .height] )
self .image .fill( self .color )
self .rect = self .image .get_rect()
self .rect .x = 50
self .rect .y = GROUND
self .jumping = False
def update( self ):
self .rect .x += 5 ## right
if self .rect .x >= 950: ## carriage return
self .rect .x = 0
if self .jumping:
self .rect .y -= 8 ## up
if self .rect .y <= GROUND -self .height *3:
self .jumping = False ## reached apogee, begin descent
else:
self .rect .y += 8 ## down
if self .rect .y >= GROUND -self .height:
self .rect .y = GROUND -self .height
def jump( self ): ## only jump when touching, or nearly touching ground
if not self .jumping and self .rect .y >= GROUND -self .height *1.1:
self .jumping = True
player = Player( all_sprites_list )
## main
running = True
while running:
for event in pygame .event .get():
if event .type == pygame .QUIT:
running = False
#clicks
keys = pygame .key .get_pressed()
if keys[ pygame .K_UP ]: player .jump()
if keys[ pygame .K_w ]: player .jump()
if keys[ pygame .K_SPACE ]: player .jump()
#updates
all_sprites_list .update()
#draws
screen .fill( WHITE )
pygame .draw .line( screen, BLACK, ( 0, Height ), ( Width, Height ), 75 )
all_sprites_list .draw( screen )
pygame .display .flip()
clock .tick( 30 )
pygame .quit()