Python Forum
code not running even without errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code not running even without errors
#1
I'm using trinket to run my code but for some reason there is no errors but it's still not running

import pygame

window = pygame.display.set_mode((800, 600))
keys = pygame.key.get_pressed()

class Player:
  def __init__(self):
    self.instance = pygame.image.load('red-box.png')
    self.rect = self.instance.get_rect(center=(100, 300))
    
    self.vy = 0
    self.jump = bool()
  def frame(self):
    if any(keys) and not self.jump:
      self.vy = 3
      self.jump = True
    
    if self.jump:
      self.rect.y -= self.vy
      self.vy -= 0.2
    
    if self.rect.colliderect(ground.rect):
      self.jump = False
      self.vy = 0
      self.rect.y = 300

    for obstacle in obstacles:
      if obstacle.rect.colliderect(self.rect):
        pygame.quit()
        exit()
    
    window.blit(self.instance, self.rect)
    
class Obstacle:
  def __init__(self, name):
    self.instance = pygame.image.load('rocketship.gif')
    self.rect = self.instance.get_rect(center=(800, 300))
    self.name = name
    
    self.instance = pygame.transform.rotate(self.instance, 270)
  def frame(self):
    self.rect.x -= 3
    
    window.blit(self.instance, self.rect)
    
    if self.rect.x < 0:
      del obstacles[name]
      del self
      
player = Player()
obstacles = {}

while True:
  for event in pygame.event.get();
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()
      
  player.frame()
  
  if random.randint(1, 500) == 1:
    idselected = random.randint(1, 10)
    obstacles += {idselected: Obstacle(idselected)}
  for obstacle in obstacles:
    obstacle.frame()

  player.frame()
  
  pygame.display.flip()
  pygame.time.Clock().tick(120)
Reply
#2
There is a sytax error on line 28 that prevents the code from running.
for event in pygame.event.get();
There is nothing named ground in the program, so this crashes the program first time Obstacle.frame is called:
f self.rect.colliderect(ground.rect):
You have to call this function each time you check which keys are pressed. Not just once.
keys = pygame.key.get_pressed()
You are still using non-standard indexing. It makes it painful to look at your code in an editor that knows how python is supposed to be formatted. Please change you indent from 2 spaces to 4 spaces.

Obstacle and Player should be pygame sprites. The code will be shorter and the program will run better.
Reply
#3
i'm using a trinket editor because I can't download .exe files on a Chromebook (school property)
I don't know how to make it 4 spaces :(
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python code not running Azdaghost 1 217 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 5 2,427 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  problem in running a code akbarza 7 2,366 Feb-14-2024, 02:57 PM
Last Post: snippsat
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 887 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  the order of running code in a decorator function akbarza 2 1,363 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Code running many times nad not just one? korenron 4 2,228 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 6,777 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 2,420 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Why is this Python code running twice? mcva 5 7,198 Feb-02-2022, 10:21 AM
Last Post: mcva
  Python keeps running the old version of the code quest 2 6,254 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020