Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python code not running
#1
why isn't it running Huh Cry Wall

import pygame
import random

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

class Ship:
  def __init__(self):
    self.instance = pygame.Surface((50, 70))
    self.rect = self.instance.get_rect(center=(400, 500))
    
    self.instance.fill('red')
  def frame(self):
    if keys[pygame.K_a]:
      self.rect.x -= 3
    if keys[pygame.K_d]:
      self.rect.x += 3
    
    for rock in rocks:
      if rock.rect.colliderect(self.rect):
        gameover()
        del self
    
    window.blit(self.instance, self.rect)
    
class Star:
  def __init__(self):
    self.instance = pygame.Surface((10, 10))
    self.rect = self.instance.get_rect(center=random.randint(0, 790), 0)
    
    self.instance.fill('white')
  def frame(self):
    self.rect.y += 2
    if self.rect.y > 600:
      del self
    
    window.blit(self.instance, self.rect)

class Rock:
  def __init__(self):
    self.instance = pygame.Surface((30, 30))
    self.rect = self.instance.get_rect(center=(random.randint(0, 770), 0))
    
    self.instance.fill('gray')
  def frame(self):
    self.rect.y += 3
    if self.rect.y > 600:
      del self
      
    window.blit(self.instance, self.rect)
    
stars = []
rocks = []
    
rocket = Ship()
    
while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      exit()
      
  window.fill('black')
      
  for rock in rocks:
    if rock.rect.y > 600:
      del rock
  for star in stars:
    if star.rect.y > 600:
      del star
      
  for rock in rocks:
    rock.frame()
  for star in stars:
    star.frame()
  rocket.frame()
      
  if random.randint(1, 25) == 1:
    stars.append(Star())
  if random.randint(1, 300) == 1:
    rocks.append(Rock())
    
  pygame.display.flip()
  pygame.time.Clock().tick(120)
Reply
#2
Because it is full of programming errors? If you promise to start using tabs that are 4 spaces and use 1 blank line between methods in a class and 2 blank lines between classes I will tell you what some of these errors are.

But here are 3 on credit:

This line has an error. You are missing a parenthesis(). Look at Ship or Rock to see the correct way to write.
self.rect = self.instance.get_rect(center=random.randint(0, 790), 0)
There is no function named gameover() in you code.

You try to blit Stars and Rocks after they are deleted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code not running even without errors Azdaghost 2 435 Apr-25-2025, 07:35 PM
Last Post: Azdaghost
  writing and running code in vscode without saving it akbarza 5 2,654 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 696 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  problem in running a code akbarza 7 2,500 Feb-14-2024, 02:57 PM
Last Post: snippsat
  the order of running code in a decorator function akbarza 2 1,435 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Code running many times nad not just one? korenron 4 2,309 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 6,939 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,477 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Why is this Python code running twice? mcva 5 7,346 Feb-02-2022, 10:21 AM
Last Post: mcva
  Python keeps running the old version of the code quest 2 6,418 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