Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a few things you need to change to make it work. I've included a somewhat lengthy explanation behind all of these changes, so that you will be able to understand them and apply them correctly in other parts of your game. If you do not need the extra explanation, you can skip to working code at the bottom of this answer. I've placed comments everywhere I made a change.</p> <h2>Event handling</h2> <p>Pygame keeps a list of events that happen in the game, such as keypresses. When you call <code>pygame.event.get()</code>, a list of all events that have happened <em>since the last call</em> is returned. This means that in your <code>Attack()</code> function, none of your code is being run because it happens for each event in <code>pygame.event.get()</code>, but <code>pygame.event.get()</code> returns an empty list because you just called it in the while loop. Generally speaking, you should only call <code>pygame.event.get()</code> once per loop, exactly like you have it in the bottom of your code. You should delete the for loop in <code>Attack()</code> because it has no purpose there.</p> <h2>Quitting</h2> <p>A small inconvenience with pygame is that the close button doesn't work by default, which means you have to forcefully close your game. To fix this, you should check for QUIT events in your event loop, and close the game. Here's an example of how the code would look inserted into your game:</p> <pre><code>while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_SPACE: var.Attack() </code></pre> <h2>Colors</h2> <p>Sadly, Pygame has no built-in Colors, so <code>BLUE</code> in <code>pygame.draw.circle(surface, BLUE, amntTuple, 20, 300)</code> will not work. Instead, replace <code>BLUE</code> with <code>pygame.Color(0,0,255)</code> to create blue yourself, using the RGB color model.</p> <h2>Drawing</h2> <p>Drawing is done in two steps in Pygame. First, objects are drawn to a virtual surface, which can not be seen. Then, when you call <code>pygame.display.update()</code>, the virtual surface is drawn to the screen, which the user can see. This has technical advantages such as allowing the computer to draw smoothly without screen flickering, but it also means that as long as you don't call <code>pygame.display.update()</code>, nothing you draw will ever be seen.<code>pygame.display.update()</code>, nothing you draw will ever be visible. However, if you call it too many times, your game may flicker or act in unexpected ways. Because of this, you will generally want to call <code>pygame.display.update()</code> exactly once per gameloop (just like with <code>pygame.event.get()</code>). Generally, <code>pygame.display.update()</code> can be the very last line of code in your while loop, with everything else coming before it.</p> <p>There are different ways you can do to draw to the screen in Pygame. One way is to use built-in functions like <code>pygame.draw.circle.</code> You were close in your use of it, but made two mistakes. First, BLUE doesn't exist, as I mentioned before. Second, the width parameter is the width of the line used to draw the circle, not the width of the circle itself. So you generally want it to be a small number. If you set the width to 0, the circle will be filled instead of drawn as an outline. Here's a fixed version of your draw.circle: <code>pygame.draw.circle(surface, pygame.Color(0,0,255), amntTuple, 20, 2)</code></p> <p>Another way to draw to the screen is with blitting. Everything in Pygame must be drawn to a Surface, which is like a virtual canvas. When you load an image using pygame.image.load(), the image is loaded as a Surface. And once you have a surface, you use blitting to draw it to the screen. For example:</p> <pre><code>man = pygame.image.load("/images/man.png") surface.blit (man, (0,0)) </code></pre> <p>would be used to draw an image of a man to the screen at pixels 0,0. This will be useful later in your game development process. However, the blit you currently have right after drawing your circle does nothing and causes errors because <code>self.space</code> and <code>self.spaceRect</code> do not exist, so you should delete it for now.</p> <h2>The Working Code</h2> <pre><code>import math import pygame, sys, time from pygame.locals import * pygame.init() WINDOW_WIDTH = 1000 WINDOW_HEIGHT = 600 surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT),0,32) pygame.display.set_caption('follow mouse') surface.fill((255,255,255)) class Hero(): def Attack(self): surface.fill ((255,255,255)) amntTuple = pygame.mouse.get_pos() #Use pygame.Color to make a color. #The last parameter is linewidth, and can be set to 0 for filled circles. pygame.draw.circle(surface, pygame.Color(0,0,255), amntTuple, 20, 2) #The blit was deleted because it did nothing and the broke code. var = Hero() while True: for event in pygame.event.get(): #Add a quit event so you can close your game normally. if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_SPACE: var.Attack() #Update once at the end of each gameloop. pygame.display.update() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload